Esc key

Apr 20, 2012 at 2:30pm
Hello everybody

I just want to know how to implement the Esc key from the keyboard in a console program
in which if the Esc key pressed at any time in the program, (by any time I mean at inputting, outputting, calculating, or just looping ) it makes a call for a specific function.
How to do That?

Note :
Operating system (windows 7 )
Compiler : QT creator 4.8

Please help
Thanks
Last edited on Apr 23, 2012 at 2:34pm
Apr 20, 2012 at 3:24pm
While such thing could be hacked together, console is not created for that. Just start making proper windowed programs. It's not that hard. If you want to make a game, see SFML or SDL, if not, try Qt or GTK (in each pair, the first is more productive, but may be harder to set up).
Apr 20, 2012 at 4:57pm
The options you have are to unbuffer your input and handle all individual keypresses yourself, or to use a keyboard hook.

Google "msdn SetWindowsHook" for information on the second method.

Good luck!
Apr 23, 2012 at 2:37pm
@ Duoas

How to unbuffer the input?

*I can't use the second option you mentioned because I'm working in qt not visual studio.
Apr 23, 2012 at 3:14pm
@Baso

If your checking for key presses with GetKeyState, then try adding
1
2
3
4
if(GetKeyState('\x1B')<0)//Pressing 'ESC' key
{
  cout << "ESC key pressed.." << endl;// Put here what is to happen when this key pressed
}
Last edited on Apr 23, 2012 at 4:07pm
Apr 23, 2012 at 4:03pm
Qt doesn't prevent you from using the Windows API directly, and setting a keyboard hook doesn't do anything that will modify your program's normal operation.

Though, I do believe Qt has slots for keyboard hooks... I has been a long time since I've messed with it and I'm not interested in browsing the documentation myself right now...
Topic archived. No new replies allowed.