28

What's the difference between getting a key press with:

  • GetKeyState()
  • GetAsyncKeyState()
  • getch()?

When should I use one over the other?

Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
  • 4
    Why the downvote? I can't find any results on this from stackoverflow, not even if I ignore `getch()` and search for `GetKeyState() vs. GetAsyncKeyState()`. – Markus Meskanen Jul 21 '13 at 09:27

2 Answers2

26

GetKeyState() and GetAsyncKeyState() are Windows specific APIs, while getch() works on other non-Windows-specific C compilers.

GetKeyState() gets the key status returned from the thread's message queue. The status does not reflect the interrupt-level state associated with the hardware.

GetAsyncKeyState() 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().

What I've seen in practice is that if you hold a key pressed and assign a behavior when the key is pressed, if you use GetKeyState(), the behavior will be called more times than if you'd have used GetAsyncKeyState().

In games, I prefer using GetAsyncKeyState().

(You can also check for more details on the MSDN blog).

Community
  • 1
  • 1
Iosif Murariu
  • 2,019
  • 1
  • 21
  • 27
5

Think what async means.

  • GetAsyncKeyState() gets the key state asynchronously, i.e., without waiting for anything, i.e. NOW.

  • GetKeyState() gets the key state synchronously, it is the key state of the key that you are about to read with getch(). It is queued in the keyboard buffer along with the keypresses themselves.

As an example, imagine the following has been typed, but hasn't yet been read:

  • h
  • i
  • shift+1
  • ctrl(held down)

GetAsyncKeyState() will return ctrl pressed

GetKeyState() will returnH presseduntil you callgetch()`

GetKeyState() will then return I pressed until you call getch()

GetKeyState() will then return shift pressed, 1 pressed until you call getch(), which will return ! (result from pressing shift+1)

GetKeyState() will then return ctrl pressed

Wyck
  • 10,311
  • 6
  • 39
  • 60
user2624417
  • 67
  • 1
  • 1
  • Thanks for the edit, never knew you could do key symbols. – user2624417 Sep 08 '19 at 22:59
  • The signature of those functions are "SHORT GetAsyncKeyState( [in] int vKey ); from the api documentation: "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." So this answer is misleading, there is no return value like "i pressed" – Jedzia Jan 13 '22 at 09:37
  • See https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate – Jedzia Jan 13 '22 at 09:41
  • And because I have also problems to make it clear want I mean: With this function you "ASK" for if a key was pressed, that you SPECIFIED beforehand. This does NOT return THE key that was pressed( so not like the behavior of the also mentioned getch(), which probably makes this answer so confusing). This works like non-blocking, for example with the escape key: "if ((GetAsyncKeyState(VK_ESCAPE) & 0x01)) { ... " – Jedzia Jan 13 '22 at 09:56
  • Sorry, but even the asynchronously/synchronously explanation here is wrong. "SHORT GetKeyState( [in] int nVirtKey );" (The one without the ASYNC) does NOT BLOCK! GetKeyState returns the virtual key state, that is your input queue. GetAsyncKeyState directly detects hardware interrupts on the keyboard. See: https://devblogs.microsoft.com/oldnewthing/20041130-00/?p=37173 and https://topic.alibabacloud.com/a/the-usage-and-difference-of-getkeystate-and-getasynckeystate-and-getkeyboardstate-functions_8_8_31199734.html – Jedzia Jan 13 '22 at 10:21