Focus
-
Hello.. i have a problem.. i have an application, and i want it to do smth when a key-combination is pressed... the problem is that i want it to do that even when it doesn't have focus.. i need smth like a "global hotkey", or i need to set up an event listener, that can listen for events even when my application doesn't have focus.. can anybody help? pls...
-
Hello.. i have a problem.. i have an application, and i want it to do smth when a key-combination is pressed... the problem is that i want it to do that even when it doesn't have focus.. i need smth like a "global hotkey", or i need to set up an event listener, that can listen for events even when my application doesn't have focus.. can anybody help? pls...
Ok in your app import the User32.dll function RegisterHotKey
[DllImport("user32.dll", SetLastError=true)] public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options Keys vk // virtual-key code );
Whilst Your at it - may as well import the Unregister one too[DllImport("user32.dll", SetLastError=true)] public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window int id // hot key identifier );
In your MainForm constructor - register the hot keyRegisterHotKey(Handle, 100, KeyModifiers.Control | KeyModifiers.Shift, Keys.L);
In this case it's ctrl, shift and L - anything you want really. Then override the WndProc for the main form and handle the eventprotected override void WndProc(ref Message m) { // Const for the WM_HOTKEY message const int WM_HOTKEY = 0x0312; // Switch on the message switch(m.Msg) { case WM_HOTKEY: { // Process the hot key press here break; } } // Pass message onto base base.WndProc (ref m); }
Remember to also unregister the hot key in the Form.OnClosed methodprivate void Form1_Closed(object sender, System.EventArgs e) { // Unregister the hot key UnregisterHotKey(Handle, 100); }
That should do what you want. Mark. :) -
Ok in your app import the User32.dll function RegisterHotKey
[DllImport("user32.dll", SetLastError=true)] public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options Keys vk // virtual-key code );
Whilst Your at it - may as well import the Unregister one too[DllImport("user32.dll", SetLastError=true)] public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window int id // hot key identifier );
In your MainForm constructor - register the hot keyRegisterHotKey(Handle, 100, KeyModifiers.Control | KeyModifiers.Shift, Keys.L);
In this case it's ctrl, shift and L - anything you want really. Then override the WndProc for the main form and handle the eventprotected override void WndProc(ref Message m) { // Const for the WM_HOTKEY message const int WM_HOTKEY = 0x0312; // Switch on the message switch(m.Msg) { case WM_HOTKEY: { // Process the hot key press here break; } } // Pass message onto base base.WndProc (ref m); }
Remember to also unregister the hot key in the Form.OnClosed methodprivate void Form1_Closed(object sender, System.EventArgs e) { // Unregister the hot key UnregisterHotKey(Handle, 100); }
That should do what you want. Mark. :) -
Ok in your app import the User32.dll function RegisterHotKey
[DllImport("user32.dll", SetLastError=true)] public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options Keys vk // virtual-key code );
Whilst Your at it - may as well import the Unregister one too[DllImport("user32.dll", SetLastError=true)] public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window int id // hot key identifier );
In your MainForm constructor - register the hot keyRegisterHotKey(Handle, 100, KeyModifiers.Control | KeyModifiers.Shift, Keys.L);
In this case it's ctrl, shift and L - anything you want really. Then override the WndProc for the main form and handle the eventprotected override void WndProc(ref Message m) { // Const for the WM_HOTKEY message const int WM_HOTKEY = 0x0312; // Switch on the message switch(m.Msg) { case WM_HOTKEY: { // Process the hot key press here break; } } // Pass message onto base base.WndProc (ref m); }
Remember to also unregister the hot key in the Form.OnClosed methodprivate void Form1_Closed(object sender, System.EventArgs e) { // Unregister the hot key UnregisterHotKey(Handle, 100); }
That should do what you want. Mark. :)still, i have a problem....:( "KeyModifiers fsModifiers" the program throws an error when i attempt to build it.. " The type or namespace name 'KeyModifiers' could not be found (are you missing a using directive or an assembly reference?)" it's a common error.. pls help, the rest of the code is perfect:)
-
still, i have a problem....:( "KeyModifiers fsModifiers" the program throws an error when i attempt to build it.. " The type or namespace name 'KeyModifiers' could not be found (are you missing a using directive or an assembly reference?)" it's a common error.. pls help, the rest of the code is perfect:)
-
[Flags()]
public enum KeyModifiers
{
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8 //Not sure about the value 8...
}Hope this helps you out! :cool: