What I need to do :
I need to capture all shortcut key presses such as Ctrl+S from a specific application. Any key combo, even if it's not a shortcut for that application.
Then my midway application that capture these keys need to validate those key combination and check if another of our apps that are running can respond to that key, and if it can send the command to it.
What I have so far :
Since we wrote the other apps we can easily send the keys to be processed that's not a problem. I can get the window handle of the application. I need to capture the shortcut keys. This app I know it's build in C++. I am looking to find a way to capture an equivalent of the following event in Winforms :
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
Right now everything is working as expected on the forward key part. I am only missing the capture of keys on that handle. I really don't want to have to adventure myself in hooking the whole keyboard and detect whether the key stroke is done in my apps or not and do I need to cancel the key or continue the process. I would prefer also getting key combination events only. I would prefer not receiving all letter the guy press when he type in a textbox or anything. I'm really looking for anything starting with CTRL, ALT, SHIFT or any combination of them
Example of what I want to do :
uncontrolled application : Notepad.exe my midway app : ShortcutHandler.exe my target applications : A.exe, B.exe
ShortcutHandler.exe will listen to Notepad.Exe shortcuts then forward them to A.exe and B.exe
Situation :
1 - in Notepad.exe press CTRL+H for replace
2 - ShortcutHandler.exe detect CTRL+H pressed on Notepad.exe
3 - ShortcutHandler.exe Analyse CTRL+H and knows it need to do some task
4 - ShortcutHandler.exe call Save on A.exe in reaction to CTRL+H in Notepad.exe
5 - ShortcutHandler.exe call Print report in B.exe in reaction to CTRL+H in Notepad.exe
Some time ago I have to do something like you, so I found this article: A Simple C# Keyboard Hook, and with this I was able to do what I need it.
But this is a complex code and as you said so, you don't want do get all key down. For my program I created a
KeyboardHook
class that make easy to work with the code obtained of previous article.So there is a snippet code of what you can do with the
KeyboardHook
class:PS: If you put this on your ShortcutHandler app the app will still get the keys.
And below is the
KeyboardHook
code: