Registration System hotkey
System hotkeys are very useful for applications such as pop-up killer. Ctrl + Shift + J is the default hotkey.
Speaking of implementation, we continue to use RegisterHotkey (HWND hWnd, int id, UINT fsModifiers, UINT vkey). The Code is as follows:
Public void SetHotKey (Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows)
{
M_hotkey = c;
M_ctrlhotkey = bCtrl;
M_shifthotkey = bShift;
M_althotkey = bAlt;
M_winhotkey = bWindows;
// Update hotkey
NativeWIN32.KeyModifiers modifiers = NativeWIN32.KeyModifiers. None;
If (m_ctrlhotkey)
Modifiers | = NativeWIN32.KeyModifiers. Control;
If (m_shifthotkey)
Modifiers | = NativeWIN32.KeyModifiers. Shift;
If (m_althotkey)
Modifiers | = NativeWIN32.KeyModifiers. Alt;
If (m_winhotkey)
Modifiers | = NativeWIN32.KeyModifiers. Windows;
NativeWIN32.RegisterHotKey (Handle, 100, modifiers, m_hotkey); // Keys. J );
}
Generally, it takes a few steps to register the hotkey.
/* ------- Using HOTKEYs in a C # application -------
-- Code snippet by James J Thompson --
In Form load: Ctrl + Shift + J
Bool success = RegisterHotKey (Handle,
100,
KeyModifiers. Control | KeyModifiers. Shift,
Keys. J );
In form closing:
UnregisterHotKey (Handle, 100 );
How to handle hotkeys:
Protected override void WndProc (ref Message m)
{
Const int WM_HOTKEY = 0x0312;
Switch (m. Msg)
{
Case WM_HOTKEY:
MessageBox. Show ("Hotkey pressed ");
ProcessHotkey ();
Break;
}
Base. WndProc (ref m );
}
Public class NativeWIN32
{
[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
}
}
------- Using HOTKEYs in a C # application -------*/
After pressing the hot key, the process is as follows: first, use HWND GetForegroundWindow () to obtain the form, and then capture the Form title, GetWindowText (HWND hwnd,/* out */LPTSTR lpString, int nMaxCount ). the details are as follows:
Protected void ProcessHotkey ()
{
IntPtr hwnd = NativeWIN32.GetForegroundWindow ();
If (! Hwnd. Equals (IntPtr. Zero ))
{
NativeWIN32.STRINGBUFFER sWindowTitle;
NativeWIN32.GetWindowText (hwnd, out sWindowTitle, 256 );
If (sWindowTitle. szText. Length> 0)
AddWindowTitle (sWindowTitle. szText); // add to the ListView (Form)
}
}
Code download: http://www.codeproject.com/useritems/popupkiller/popupkiller_src_update.zip
Demo: http://www.codeproject.com/useritems/popupkiller/popupkiller_demo_update.zip