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 ?
Each individual keystroke generates separate OnKeyDown
, OnKeyPress
, and OnKeyUp
events. So you have three choices:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With