Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect non modifier key press combination?

I have the following problem in Delphi (but it might be taken as a general programming question).

I would like to handle somehow a key press event for more than one non-modifier key combination, for example for shortcuts like A+D or D+F or D+F+G.

I know how to handle shortcuts with modifier keys like for instance CTRL+D or CTRL+ALT+D or ALT+D, but how can I detect non-modifier key press combination ?

like image 938
Domenico Angri Avatar asked Nov 15 '12 02:11

Domenico Angri


1 Answers

Each individual keystroke generates separate OnKeyDown, OnKeyPress, and OnKeyUp events. So you have three choices:

  1. keep track of each key that is currently held down. For each key you receive, set a flag for it in the OnKeyDown event and clear the flag for it in the corresponding OnKeyUp event. When you get an OnKeyDown event for D, check if you already flagged A, F, G, etc. The OnKeyDown and OnKeyUp events will also tell you the state of the CTRL, ALT, and SHIFT keys.

  2. use the Win32 API GetKeyboardState(), GetKeyState() or GetAsyncKeyState() functions. When you get an OnKeyDown event for D, ask the OS if A, F, G, CTRL, etc are currently held down.

  3. for some sequences, you might use RegisterHotKey() and let the OS track the keys for you. When a registered sequence is detected, you will receive a WM_HOTKEY message.

like image 145
Remy Lebeau Avatar answered Sep 27 '22 23:09

Remy Lebeau