I’m working on an C# application which monitors keyboard and mouse events and reacts if a preset combination of key or mouse button presses occur.
The hotkey detection is already working by using keyboard and mouse hooks. However I run into trouble when the application’s answer is sending a new key combo with SendInput().
Let’s say the app waits for “Alt+RMB“m suppresses it and sends “Shift+A” instead. When I start to physically press “Alt+RMB” the combo is detected and a virtual “Shift+A” is sent.
However that fake keypress instantly mixes with the original keys so the app now detects the “Alt+Shift+A+RMB” combination which is not “Alt+RMB” anymore so “Shift+A” is stopped being sent. After the virtual keypress clears the system my real hotkey press is detected again and the cycle starts over. (Setups where “A” needs to be replaced by “B” while “B” with “A” would lead to an infinite loop.)
So it seems that my main problem is telling apart real and virtual key/mouse events. Any suggestions?
(Yes, the task is indeed similar to what Autohotkey does. However I have custom business logic between the hotkey detection and the optional virtual input sending steps. Using Autohotkey.dll might be an option but I’m wary of the performance impact of a custom script interpreter running in there.)
As it turns out the proper term for virtual key/mouse events is “injected” and the low level keyboard hook does provide a related flag in the KBDLLHOOKSTRUCT.
Now I modified the hook related event management so it lets through any events with the “LLKHF_INJECTED” flag, without suppressing or informing the main logic about it.