Sign in to follow this  
Fixxer

Detecting A Keypress in WinAPI

Recommended Posts

Fixxer    100
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
Evil Steve    2017
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
Fixxer    100
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);
TextOut(hdc,0,0,buffer,strlen(buffer));

sprintf(buffer,"Key State = 0X%X ",key_state);
TextOut(hdc,0,16,buffer,strlen(buffer));

// release DC
ReleaseDC(hwnd,hdc);


// take what ever action

}break;

Share this post


Link to post
Share on other sites
Evil Steve    2017
From The MSDN:
Quote:

lParam
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
    Evil Steve    2017
    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
    Fixxer    100
    I have this code from my book.

    would this be ok?

    code
    #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

    example
    KEYDOWN('A')

    Share this post


    Link to post
    Share on other sites
    Evil Steve    2017
    Yup, that'll do the exact same. I'd prefer a function though, since they're "Better" than macros (Type safe, etc). An inline function will perform exactly the same as a macro.

    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