-2

How I can determine which key is pressed on keyboard in C++ using WINAPI?

I try to use GetAsyncKeyState, but it work just for exactly one key, and I need to choose which one, but I need to get keyсode to it be send over tcp.

Riv Shiell
  • 31
  • 6

1 Answers1

1

Loop over every character and try each one:

for (char i = 32; i < 127; i++)
    if (GetAsyncKeyState (i))
         /* Do whatever you want with that character */;

In the example I loop just from ' ' to '~', modify it as you want.

Mattia F.
  • 1,720
  • 11
  • 21
  • 1
    It's `GetAsyncKeyState`, with a capital `G`. But you don't need to call it in a loop anyway, since there is [GetKeyboardState](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646299.aspx) that returns the key states for the entire keyboard. – IInspectable Jun 15 '16 at 10:14
  • @IInspectable Yes, but `GetKeyboardState` matches `GetKeyState`, which has different semantics from `GetAsyncKeyState`. As to which semantics the asker needs, I don't think that we can be sure. – David Heffernan Jun 15 '16 at 10:23