I'm wondering how I can handle a registered hot key in a way other then when it's pressed.
The following will continually fire the HotKeyPressed event if the hot key is held down.
I want to know if there is a way to only fire the HotKeyPressed event when the hot key is first pressed, like a HotKeyDown event, and also I wouldn't mind having a HotKeyUp event.
So if anyone knows how to make it only fire once per press, please let me know.
The following will continually fire the HotKeyPressed event if the hot key is held down.
Code:
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.