Thread: How to check if a key is down?

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    How to check if a key is down?

    Hi, I want to check if a keyboard key is down or up. How can I do that? For example, while ctrl is down I want to lock the position of the mouse cursor and only check the movement of the mouse, and when ctrl is up the user should be able to move the mouse cursor freely. If the implementation is platform dependent, the platform used is Windows.

    Note: I want to do this as fast as the program starts, and I would rather want to know if there is any key down from the beginning.
    Last edited by TriKri; 05-03-2010 at 02:29 AM.
    Come on, you can do it! b( ~_')

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    I assume you are talking about GUI programming?

    This really depends on the platform you are running on. If you are programming a win32 application, inside the windowproc function you will provide a case WM_KEYDOWN.

    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
       switch (msg)
       {
       case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
       case WM_KEYDOWN:
          // check the key code for the ctrl button against wParam
          break;
       }
       return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    There is also third party libraries you can use in order to make the program more portable. For example, QT is widely used and the code is reusable on windows / unix / linux (as long as the QT library is installed on that machine).

    Also wxWidgets is a good one, which supports GDK, Windows and MacOS.

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    I am actually using Qt; is there any function there to check if a key is down?

    All I have found are events that occur when a key is pressed or released. But an event driven approach is not good enough, since then I won't know if the key was down when the program was started or not. Besides, I will get no key events when the program is out of focus, will I?

    I have found a function in windows.h called GetKeyState, I'm using it for so long, although it does make the application platform dependent.
    Come on, you can do it! b( ~_')

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Reading keystrokes is platform dependent. There is no ANSI C way to do it, because C doesn't care about keyboards.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Sorry, I'm not aware of any asynchronous method to get the current key state on QT.

  6. #6
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    It seems like GetKeyState in windows.h is working quite fine, so I'll just stick to that. Then I guess that different libraries (SDL, OpenGL, Qt (?)) may have their own version of this function, so that the programmer still can write platform independent code.
    Come on, you can do it! b( ~_')

  7. #7
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by hawaiian robots View Post
    Sorry, I'm not aware of any asynchronous method to get the current key state on QT.
    Ok, this time I won't need it. Since I know that the program is going to run on a windows laptop, I can just use GetKeyState. It is going to steer a robot via bluetooth for a project in school.
    Come on, you can do it! b( ~_')

  8. #8
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    What I have found out using this method is that my keyboard is a little bit buggy. Using this code and a loop that calls check_keys:

    Code:
    #define  KEY_DOWN  0x8000
    
    inline uint8_t get_key_down(uint8_t v_key)
    {
        return !!(GetKeyState(v_key) & KEY_DOWN);
    }
    
    void user_interface::check_keys()
    {
        uint8_t new_direction;
    
        new_direction = ED_NO_DIRECTION + (3 * (get_key_down(VK_UP   ) - get_key_down(VK_DOWN)) + 
                                           1 * (get_key_down(VK_RIGHT) - get_key_down(VK_LEFT)) );
        if (new_direction != curr_engines_direction) {
            curr_engines_direction = new_direction;
            send_message(engines_direction_messages[new_direction]);
        }
    }
    I calculate an exact direction of the robot. When curr_engines_direction changes value, a message is sent to the robot containing the new direction. All messages that are sent are also written to a text box, so I can monitor everything that is transferred. Hence I can see whenever curr_engines_direction changes; it should change as fast as I press down or release one of the arrow keys.

    However, if I hold down the left and up arrow keys at the same time, curr_engines_direction won't change when I press down yet another arrow key, and when I do that, every other key seem to freeze in it's current state until I release one of the arrow keys. At first, I thought this was a bug or a limitation in the win32 api, but when I tried this and pressing caps lock, I saw on my keyboard that it didn't toggle.
    Come on, you can do it! b( ~_')

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you remembering the repeat feature of the keyboard? After a very brief pause, the repeat feature begins on held down keys.

    Or perhaps that API just won't handle it, but I seriously doubt your keyboard is "buggy".

  10. #10
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Hm, maybe you're right. I tried with another keyboard as well and it was the same thing. I don't think it has to do with the API though, since the keyboard is also affected by it (pressing caps lock doesn't turn the LED on/off).
    Last edited by TriKri; 05-03-2010 at 12:43 PM.
    Come on, you can do it! b( ~_')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  2. "press any key to continue" without the enter key
    By chickenandfries in forum C Programming
    Replies: 1
    Last Post: 03-29-2008, 09:56 PM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  4. Replies: 3
    Last Post: 05-25-2005, 01:50 PM
  5. BCB Key press problem
    By Death_Wraith in forum Game Programming
    Replies: 0
    Last Post: 05-30-2004, 03:13 PM

Tags for this Thread