So, I already know that by using if (GetAsyncKeyState())
I can check if the user presses a specific key. But what I need is to check if the user presses any key and to store that key that the user pressed inside a variable. Is there any easy way to do that?
Asked
Active
Viewed 303 times
-2

I Like Potato
- 1
- 3
-
1Do you mean like [GetKeyboardState](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeyboardstate)? Also, typically in Windows applications, we register for windows events and handle WM_KEYDOWN. Are you opposed to that? – Wyck Jan 11 '21 at 17:03
-
I think we should remove the codeblocks tag from this question. Any objection to that? – Wyck Jan 11 '21 at 17:05
-
@Wyck.....Done.... – I Like Potato Jan 11 '21 at 17:09
-
@Wyck Anything will do as long as I can store it inside a variable. – I Like Potato Jan 11 '21 at 17:10
-
Handle WM_KEYDOWN in your wndproc and store wParam in a variable as you wish. – Wyck Jan 11 '21 at 20:56
2 Answers
0
First, _getch could read a single character from the console without echoing the character. But it has limitations, it cannot read all keys.
Then, GetKeyboardState
can get the status of the 256 virtual keys. But it cannot detect the key event between two calls.
You could register key down event with RawInput or LowLevelKeyboard hook, Check this sample, you will get the Virtual-Key in input.data.keyboard.VKey
. You could create a thread to wait and get the key event, instead of polling to check key status.

Drake Wu
- 6,927
- 1
- 7
- 30
-1
I'm guessing you're trying to get input without the user pressing 'Enter', in that case see this post's answer.

seanhungerford
- 87
- 1
- 6