GetAsyncKeyState() function in C++

GetAsyncKeyState stands for Get Asynchronous Key State in C++.

This function gives information about the key, whether the key was pressed up or down at the time when the function is called. In simple words, it will check whether a key is pressed or not.

Whenever the GetAsyncKeyState function is called and runs successfully after compilation, if the MSB is set then the key is down, and if LSB is set then the key is up. {Here MSB refers to most significant bit and LSB refers to least significant bit}

This Function will return Zero in the following cases given below:

  • The desktop is not active and
  • When the thread working in the foreground of the desktop belongs to other processes of the system and the desktop is not allowing to record.

Prototype:

short GetAsynKeyState(int key);

Example-

At the time of executing this statement, it will tell if the up arrow is being pressed. Virtual-key code constants are used to find the state of the pressed SHIFT, CONTROL, or ALTERNET keys. Some of the virtual- key codes are VK_SHIFTVK_CONTROL, and VK_MENU. But, It will not differentiate between the left and right keys. You can also use these key codes to differentiate between the left or right key.

These are some vkey for distinguishing between left and right.

CODE                                    –                                    MEANING

   VK_LSHIFT                                  Left-shift key.

   VK_RSHIFT                                  Right-shift key.

   VK_LCONTROL                                Left-control key.

   VK_RCONTROL                                Right-control key.

   VK_LMENU                                   Left-menu key.

   VK_RMENU                                   Right-menu key.

if(GetAsyncKeyState(VK_UP))
{
printf("Up Arrow key is Pressed");
}

I hope this helps!

Thank You…….

More on codespeedy

C++ program to swap bits in a number

Phone Directory Implementation in C++

Leave a Reply

Your email address will not be published. Required fields are marked *