Thread: responsive keyboard/mouse

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    61

    responsive keyboard/mouse

    I am writing a program and I want to check for messages (keypresses etc...). I need to use GetKeyState() for it. So The mainloop of my program looks something like this :
    Code:
    while(!done){
    	check_for_events();
    	handle_events();
    	draw();
    }
    - check_for_events() uses GetKeyState() to check if a key or mouse button is pressed or released.
    - handle_events() : the code that handles those events
    - draw() : the drawing code

    So my question is : Will the keyboard (and mouse etc...) be responsive enough when handle_events() and draw() take alot of time ? If not, is there a better solution ?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'm guessing that this is for a game or animation of some kind. Keep in mind that most games draw at least 30 frames per second. If your applications draws 30 frames per second, then that means it also polls the mouse/keyboard 30 times a second as well. This is definitely responsive enough. Now if your draw() function takes a whole second to complete, then you will not only have problems with your responsiveness, but also your animation will look like crap.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    Thanks for the answer.

    what I am actually making is my own GUI. It will be as optimised as possible by not drawing anything to much, but indeed when it is used to make a game or an animation, it will draw certain parts of the window at least at a decent framerate.

    The thing I was was wondering about is this : Say I run a game at 30fps. check_for_events() has just returned, and then I push a key during 25 ms. It won't catch the event then because it will take 33ms to get back to check_for_events().

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    take advantage of the fact that a human keypress is quite slow,
    get a stopwatch and try triggering it twice within 10ms - difficult.
    thats with trying, so the typical relaxed user response is much
    lower. also, couldnt you call the check for events code more than
    once every loop? that would minimise the time between checks
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    take advantage of the fact that a human keypress is quite slow,
    get a stopwatch and try triggering it twice within 10ms - difficult.
    thats with trying, so the typical relaxed user response is much
    lower. also, couldnt you call the check for events code more than
    once every loop? that would minimise the time between checks
    That actually sounds like a good idea. Thanks.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It won't catch the event then because it will take 33ms to get back to check_for_events().
    It depends on what you are implementing. If a user is typing something in, then polling for keypresses is a horrible way to go. In this situation you would watch for keypress messages to make sure you dont miss anything. If the keypresses are being used for a game (Like watching the wasd keys for movement), then polling the keyboard is the way to go because you don't care about keypresses, instead all you care about is whether a key is pressed at the time of the current frame being drawn.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    Quote Originally Posted by bithub
    It depends on what you are implementing. If a user is typing something in, then polling for keypresses is a horrible way to go. In this situation you would watch for keypress messages to make sure you dont miss anything. If the keypresses are being used for a game (Like watching the wasd keys for movement), then polling the keyboard is the way to go because you don't care about keypresses, instead all you care about is whether a key is pressed at the time of the current frame being drawn.
    Well, it should be able to be used by both (games/typing). Isn't there a solution that is good for both. btw : The reason that I want to use GetKeyState() is the following :

    I need to be able to do weird keypresses like this :
    - you start pressing a key
    - it immediately sends a keyPress event
    - 100ms later, if the key is still pressed, it sends another keyPress event
    - 75ms later, if the key is still pressed, it sends another keyPress event
    - 45ms later, if the key is still pressed, it sends another keyPress event
    - 50ms later, if the key is still pressed, it sends another keyPress event
    - 10ms later, if the key is still pressed, it sends another keyPress event
    - 10ms later, if the key is still pressed, it sends another keyPress event
    - 10ms later, if the key is still pressed, it sends another keyPress event
    - etc...

    I don't know how I could do this with the Windows messages though (in a simple way), since messages are only sent the moment that a key is pressed (or released). With getKeyState() I know how to do it in a clean way.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You have to use both windows messages and the GetKeyState() function.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    Thanks, I'll try that.
    I already have something in mind.

Popular pages Recent additions subscribe to a feed