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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
GetKeyStateorGetAsyncKeyState, but that won’t give you keydown/keyup events. It will only tell you what keys are currently down.So if you really need to get the keydown/keyup events, you could install a hook.
A Console window has a window handle that is owned by code in Windows and a message pump, also owned by code in Windows.
You can get the window handle of of the console window by using GetConsoleWindowThen install a
WH_CALLWNDPROChook using SetWindowsHookEx to listen in on messages send to the console window.You might try a
WH_MSGFILTERhook instead. I don’t know if this works for console windows, but it would generate less messages to be ignored if it does work.