Thread: User Input - Key 'Bouncing' = Bad

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    User Input - Key 'Bouncing' = Bad

    I have a little old system of managing user input that just detects WM_KEYUP and WM_KEYDOWN. I use this little keyboard thingy:

    Code:
    struct keyboard
    {
    
        keyboard( void );
        ~keyboard( void );
    
        operator BYTE *();
        operator const BYTE *() const;
    
        const BYTE * begin() const;
        BYTE * begin();
    
        const BYTE * end() const;
        BYTE * end();
    
    private:
    
        BYTE keys[256];
    
    };
    And when I recieve a WM_KEYDOWN or WM_KEYUP, I toggle my my_keyboard[wParam]value. It would appear as though when I am in a rush and sort of hitting the spacebar a lot, then it gets locked thinking it's down. Is there a better way of collecting user input?

    Also peculiar is that I have the a 'keyboard' associated with each instance of a window. I have multiple instances of some window at once right now, but this seems kind of peculiar if I were to move this class over to a plain GUI application.

    Any better way of doing this? This is for a game, but it is related to windows, so whatever.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    One possible approach might be to switch off the state of the particular key once you've done whatever you're doing with it, rather than wait for the corresponding WM_KEYUP to do the switching.

    For example, if your game fires a 'DoSpaceBar' function in response to a WM_KEYDOWN from a spacebar press, then make 'DoSpaceBar' toggle the spacebar's 'pressed' state.

    I suspect the lack of synchronicity may be due to using PeekMessage rather than GetMessage in your message loop.

    Not sure if that will help. The other option is to use Direct Input, if it still exists in whatever the latest version of directx is, or use some analogue of direct input consistent with whatever version of directx you're using.

    If you want only one keyboard c++ class instance per application then perhaps making that class a singleton might be appropriate.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  2. Truncating user input
    By CS_Student8337 in forum C Programming
    Replies: 10
    Last Post: 03-19-2009, 12:34 AM
  3. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:59 PM
  4. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  5. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM