Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ console keyboard events

Is there any way to get key events in a Windows console? I need a way to get keydown and keyup events quickly without a GUI. I've tried using getch(), but it doesn't get keyups and waits until a key has been pressed to return.

like image 921
Cassidy Laidlaw Avatar asked Jan 14 '10 22:01

Cassidy Laidlaw


1 Answers

Use ReadConsoleInput() API. Watch for events of kind KEY_EVENT. This won't work for all keydown events (Ctrl-key, shift-key, Pause-key cannot be read), but most can be read.

Use GetNumberOfConsoleInputEvents to avoid blocking.

like image 106
Seva Alekseyev Avatar answered Oct 06 '22 13:10

Seva Alekseyev