1. Declare the system API:
[DllImport ("user32.dll", EntryPoint = "RegisterHotKey", SetLastError = true, ExactSpelling = true, CharSet = CharSet. Auto, CallingConvention = CallingConvention. StdCall)]
Public static extern bool RegisterHotKey (IntPtr hWnd, int id, int fsModifiers, int vlc );
[DllImport ("user32.dll", EntryPoint = "UnregisterHotKey", SetLastError = true, ExactSpelling = true, CharSet = CharSet. Auto, CallingConvention = CallingConvention. StdCall)]
Public static extern bool UnregisterHotKey (IntPtr hWnd, int id );
Reload OnLoad, registration hotkey:
Protected override void OnLoad (EventArgs e)
{
Base. OnLoad (e );
WinApi. RegisterHotKey (this. Handle, 225, 0, (int) Keys. F10 );
WinApi. RegisterHotKey (this. Handle, 226, 0, (int) Keys. F11 );
}
Deregister the hotkey in the FormClosing event:
Private void Form_Main_FormClosing (object sender, FormClosingEventArgs e)
{
WinApi. UnregisterHotKey (this. Handle, 225 );
WinApi. UnregisterHotKey (this. Handle, 226 );
}
Reload WndProc function processing hotkey
Protected override void WndProc (ref Message m)
{
Base. WndProc (ref m );
Switch (m. Msg)
{
Case 0x0312: // This is the registered hotkey message defined by the window message.
If (m. WParam. ToString (). Equals ("225") // F11
{
This. buttonItem_ToolBar _ grid monitoring _ Click (null, null );
}
Else if (m. WParam. ToString (). Equals ("226") // F12
{
This. panel1.Visible =! This. panel1.Visible;
}
Break;
}
}