Use windows api to implement the system hotkey function!
The main categories are designed as follows:
Using System;
Using System. Windows. Forms; // for Key namespace
Using System. Runtime. InteropServices;
System hotkeys implemented by namespace
{
/// <Summary>
/// WIN32 Windows API-assisted operation class.
/// </Summary>
Public class NativeWIN32
{
Public NativeWIN32 ()
{}
/* ------- Using WIN32 Windows API in a C # application -------*/
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Static public extern IntPtr GetForegroundWindow ();//
[StructLayout (LayoutKind. Sequential, CharSet = CharSet. Auto)]
Public struct STRINGBUFFER
{
[Financialas (UnmanagedType. ByValTStr, SizeConst = 256)]
Public string szText;
}
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern int GetWindowText (IntPtr hWnd, out STRINGBUFFER ClassName, int nMaxCount );
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern int SendMessage (IntPtr hWnd, int msg, int wParam, int lParam );
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern IntPtr SendMessage (IntPtr hWnd, int msg, int wParam, IntPtr lParam );
Public const int WM_SYSCOMMAND = 0x0112;
Public const int SC _CLOSE = 0xF060;
Public delegate bool EnumThreadProc (IntPtr hwnd, IntPtr lParam );
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern bool EnumThreadWindows (int threadId, EnumThreadProc pfnEnum, IntPtr lParam );
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern IntPtr find1_wex (IntPtr parent, IntPtr next, string sClassName, IntPtr sWindowTitle );
/* ------- Using HOTKEYs in a C # application -------
In form load:
Bool success = RegisterHotKey (Handle, 100, KeyModifiers. Control | KeyModifiers. Shift, Keys. J );
In form closing:
UnregisterHotKey (Handle, 100 );
Protected override void WndProc (ref Message m)
{
Const int WM_HOTKEY = 0x0312;
Switch (m. Msg)
{
Case WM_HOTKEY:
MessageBox. Show ("Hotkey pressed ");
Break;
}
Base. WndProc (ref m );
}
------- Using HOTKEYs in a C # application -------*/
[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
);
[DllImport ("user32.dll", SetLastError = true)]
Public static extern bool UnregisterHotKey (IntPtr hWnd, // handle to window
Int id // hot key identifier
);
[Flags ()]
Public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
}
}