Thread: keyboard event

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    keyboard event

    Hi:
    I was using KEY_DOWN() or KEY_UP() functions(self-defined functions) to judge whether the key had been pressed or not,for example, if I press the ENTER key and something happens, I though I did press once, but inside the programming, the key has been pressed maybe more than 10 times, because KEY_DOWNs or KEY_UP() evens happens so fast, this would cause unimagined problems, how can I prevent this, thank you.
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    This is what I always do
    Code:
    bool enterPressed, aPressed, (whatever else);
    then under WM_KEYDOWN, you put:
    Code:
    switch(lParam [or whatever holds the key code])
    {
         case VK_ENTER:
              enterPressed = true;
              break;
    
         case 'A':
              aPressed = true;
              break;
    
    //whatever other keys
    }
    and under WM_KEYUP you set the bools to false. Then in KeyDown() you check if the bools are true. Then KeyDown() would know the key has been pressed down and is still down.

    Or you could make another bool, to see if KeyDown() had been called yet. When KeyDown() gets called, set it to true, and set the bool for the key being checked to false, and under WM_KEYDOWN check to make sure that KeyDown() hasn't been called yet before setting the key's bool to true. Under WM_KEYUP, set the key's bool to false, AND make the bool for KeyDown() to false. That would make sure KeyDown() only thinks the key is pressed once when you press the key and hold it.

    Is that what you wanted?
    *I thought most of that up on the spot, so there MIGHT be bugs.
    **Try not to make run-on sentences, because those are hard to understand.. besides, your L.A. teacher will strangle you if he/she sees them.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    Thank you, need time to try this
    Don't laugh at me,I am just a SuperNewbie.

  4. #4
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490
    You also try this:

    bool *bKey[];

    case WM_KEYDOWN:
    bKey[wParam] = TRUE;
    break;
    case WM_KEYUP:
    bKey[wParam] = FALSE;
    break;
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    In this case, wParam is the virtual-key code right? That would work fine but... how big would the bool array have to be? And would there be some wasted memory holding extra bools, or is every number (up to the highest one) a key-code?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM