What is GetAsyncKeyState's return value?

Viewed 478

I trying to use GetAsyncKeyState in a project I'm working on. The only problem is I don't know what value it returns, and in turn how to check for it. According to the MSN documentation:

Type: SHORT

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

I know that type SHORT is a number, but I've seen a lot of different answers across StackOverflow, and the internet, but what is GetAsyncKeyState's return value when it evaluates to true, does it return 0 or 0x8001?

1 Answers

The return value can be one of 4 possible values:

  • 0x0000
  • 0x0001
  • 0x8000
  • 0x8001

Use & 0x8000 (or alternatively < 0 since SHORT is a signed type) to check if "the most significant bit is set" (which makes a signed type negative).

Use & 0x0001 to check if "the least significant bit is set".

Related