I need to capture a key press when my program is not in focus. (ie. Ctrl+Alt+J) and trigger an event in my program.
Thus far i have found this dll that appears to be the correct path"
[DllImport("user32.dll")]private static extern int RegisterHotKey(IntPtr hwnd, int id,int fsModifiers, int vk);
[DllImport("user32.dll")] private static extern int UnregisterHotKey(IntPtr hwnd, int id);
Please note that this code will not trigger events in console application projects. You have to use WinForms project for events to fire.
This is the correct code:
to use (i had to edit the modifier keys to cast them (modifier)1 (modifier)2 etc
Here is a working vb.net port of original answer:
KeyboardHook.vb
Form1.vb
Tasks:
Application1
below with name of your application (it can be seen as root of tree in Visual Studio Solution Explorer window).AddGlobalHotkeySupport()
into initialization stage of your application.RemoveGlobalHotkeySupport()
into finalization stage of your application.Code: