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

后端 未结 2 1838
予麋鹿
予麋鹿 2021-02-04 01:29

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

  • GetKeyState()
  • GetAsyncKeyState()
  • getch()?
2条回答
  •  -上瘾入骨i
    2021-02-04 01:52

    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

提交回复
热议问题