Sign in to follow this  

Detecting A Keypress in WinAPI

Recommended Posts

I am trying to make a header of defines that place a word in place of the keys code. I made a program that tells me the code of the key being pressed, but it gives me two different ones! 0X1E0001 Example: when I press "a", I get the code 0X1E0001 but if I hold it down for a few seconds, it gives me the code 0X401E0001. Which one do I use?

Share this post

Link to post
Share on other sites
What function are you using? GetAsyncKeyState()? If so, the value returned is a bitfield, not an actual value. I suspect that whatever function you're using is the same idea, it's not a value returned, it's a bitfield.

Share this post

Link to post
Share on other sites
I am using this code:

case WM_CHAR:
char ascii_code = wparam;
unsigned int key_state = lparam;

char buffer[255];

// get graphics context
hdc = GetDC(hwnd);

// set foreground color to green
SetTextColor(hdc, RGB(0,255,0));

// set background color to black
SetBkColor(hdc, RGB(0,0,0));

// set color mode to opaque
SetBkMode(hdc, OPAQUE);

// print ascii code and key state
sprintf(buffer,"WM_CHAR: Character = %c ", ascii_code);

sprintf(buffer,"Key State = 0X%X ",key_state);

// release DC

// take what ever action


Share this post

Link to post
Share on other sites
From The MSDN:

Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.

  • 0-15
    Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
  • 16-23
    Specifies the scan code. The value depends on the OEM.
  • 24
    Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
  • 25-28
    Reserved; do not use.
  • 29
    Specifies the context code. The value is 1 if the ALT key is held down while the key is pressed; otherwise, the value is 0.
  • 30
    Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.
  • 31
    Specifies the transition state. The value is 1 if the key is being released, or it is 0 if the key is being pressed.

  • If you just want to know the ASCII code of the key, use the char in wParam. If you want to know any of the above information, test those bits in lParam.

    Share this post

    Link to post
    Share on other sites
    If you get a WM_CHAR notification, then the key is pressed. If you want to check from various points in your code, use GetAsyncKeyState(). Example:

    bool IsKeyPressed(int nVirtKey)
    return (GetAsyncKeyState(nVirtKey)&0x8000) ? true : false;

    // Example usage:
    if(IsKeyPressed(VK_LSHIFT) && IsKeyPressed('A'))
    // Left shift + A is pressed

    Share this post

    Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    Sign in to follow this