The following will continually fire the HotKeyPressed event if the hot key is held down.
Expand|Select|Wrap|Line Numbers
- public partial class Form1 : System.Windows.Forms.Form
- {
- [System.Runtime.InteropServices.DllImport("user32.dll")]
- private static extern bool RegisterHotKey(System.IntPtr hWnd, int id, int fsModifiers, int vk);
- [System.Runtime.InteropServices.DllImport("user32.dll")]
- private static extern bool UnregisterHotKey(System.IntPtr hWnd, int id);
- public delegate void HotKeyPressedHandler();
- public event HotKeyPressedHandler HotKeyPressed;
- public enum Modifiers : uint
- {
- ALT = 0x0001,
- CTRL = 0x0002,
- SHIFT = 0x0004,
- WIN = 0x0008,
- }
- public Form1()
- {
- InitializeComponent();
- this.HotKeyPressed += new HotKeyPressedHandler(Form1_HotKeyPressed);
- RegisterHotKey(this.Handle, 0, (int)Modifiers.CTRL, (int)System.Windows.Forms.Keys.Q);
- }
- void Form1_HotKeyPressed()
- {
- System.Console.WriteLine("Hot Key Is Pressed");
- }
- protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
- {
- UnregisterHotKey(this.Handle, 0);
- base.OnClosing(e);
- }
- protected override void WndProc(ref System.Windows.Forms.Message m)
- {
- if (m.Msg == 0x312 && this.HotKeyPressed != null) this.HotKeyPressed();
- base.WndProc(ref m);
- }
- }
So if anyone knows how to make it only fire once per press, please let me know.