Using GetKeyState()

I would like to have a boolean event toggle when a key is pressed. Specifically, the ‘s’ key. I have been pointed to the function GetKeyState(), which supposedly works under the Win32 API. I understand the ASCII code for the letter ‘s’ is 115, and so my code is as follows:

if (GetKeyState(115) == 1)
{
<EVENT>
}

However, this does not work. Why? Here is the MSDN reference: http://msdn.microsoft.com/en-us/library/ms646301%28v=vs.85%29.aspx … “If the low-order bit is 1, the key is toggled”

Solution #1:

From what I understand you need to do:

if( GetKeyState(115) & 0x8000 )
{
    <EVENT>
}

The highest bit tells if key is pressed. The lowest tells if key is toggled (like, if caps lock is turned on).

Respondent: Piotr Praszmo

Solution #2:

Since SHORT is signed, high-order bit equals sign bit.

Therefore to test if a given key is pressed, simply test if the value returned by GetKeyState() is negative:

if (GetKeyState('S') < 0) {
    // The S key is down.
} else {
    // The S key is up.
}

Besides, 115 is ASCII code for ‘s’. I believe, you should use capital case 83 to test the ‘S’ key.

Respondent: Simon Rozman

Solution #3:

I use a global variable bool altgr

Example:

void Editor::HandleKey(char car) {

    bool shift = false;
    //bool altgr = false;
    bool printable = false;

    if (car == 27) SendMessage(hwnd, WM_DESTROY, 0, 0);

    if ((GetKeyState(VK_CAPITAL) & 0x0001) == 1) shift = true;
    if ((GetKeyState(VK_SHIFT) & 0x8000) == 0x8000) shift = true;
    // if(GetKeyState(VK_RMENU) & 0x80000000 == 0x80000000) altgr = true;
    if (car == 18) altgr = true; 

Solution #4:

Sometimes you want to use a combination of keys.

To avoid the situations when a combination of keys (eg: VK_SHIFT && VK_LEFT) satisfies two conditions:

std::cout << "Shift RIGHT pressed" << std::endl;
std::cout << "LEFT key pressed" << std::endl;

just use Sleep(...); and GetAsyncKeyState(VK_...)

GetKeyState() vs. GetAsyncKeyState() vs. getch()?

#include <windows.h>  

    ...

    while (1)
    {
        if ((GetKeyState(VK_SHIFT) & 0x8000) && (GetAsyncKeyState(VK_LEFT) & 0x8000))
        {
            Sleep(200);
            std::cout << "Shift LEFT pressed" << std::endl;
        }

        if ((GetKeyState(VK_SHIFT) & 0x8000) && (GetAsyncKeyState(VK_RIGHT) & 0x8000))
        {
            Sleep(200);
            std::cout << "Shift RIGHT pressed" << std::endl;
        }

        if (GetAsyncKeyState(VK_RIGHT))
        {
            std::cout << "RIGHT key pressed" << std::endl;
        }

        if (GetAsyncKeyState(VK_LEFT))
        {
            std::cout << "LEFT key pressed" << std::endl;
        }
   }
Respondent: T.I.Adrian

Solution #5:

Bit late for this but the high order bit is 0x80000000 not 0x8000, change this and it will work fine.

The other bit – useful for nothing – it acts as if you pressed CAPS LOCK when you pressed LSHIFT.

Respondent: Laurie

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.