基于C#實現(xiàn)熱鍵注冊工具類
寫在前面
介紹一個驗證過的熱鍵注冊工具類,使用系統(tǒng)類庫user32.dll中的RegisterHotkey函數(shù)來實現(xiàn)全局熱鍵的注冊。
代碼實現(xiàn)
[Flags]
public enum KeyModifiers
{
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8,
NoRepeat = 0x4000
}
public static class HotKeyHelper
{
[DllImport("user32", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private static int _id = 0;
private static volatile MessageWindow _wnd;
private static volatile IntPtr _hwnd;
private static ManualResetEvent _windowReadyEvent = new ManualResetEvent(false);
static HotKeyHelper()
{
Thread messageLoop = new Thread(delegate ()
{
Application.Run(new MessageWindow());
});
messageLoop.Name = "MessageLoopThread";
messageLoop.IsBackground = true;
messageLoop.Start();
}
public static event EventHandler<HotKeyEventArgs> HotKeyPressed;
public static int RegisterHotKey(Keys key, KeyModifiers modifiers)
{
_windowReadyEvent.WaitOne();
int id = System.Threading.Interlocked.Increment(ref _id);
_wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key);
return id;
}
public static void UnregisterHotKey(int id)
{
_wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id);
}
delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key);
delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id);
private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key)
{
RegisterHotKey(hwnd, id, modifiers, key);
}
private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id)
{
UnregisterHotKey(_hwnd, id);
}
private static void OnHotKeyPressed(HotKeyEventArgs e)
{
if (HotKeyHelper.HotKeyPressed != null)
{
HotKeyHelper.HotKeyPressed(null, e);
}
}
private class MessageWindow : Form
{
public MessageWindow()
{
_wnd = this;
_hwnd = this.Handle;
_windowReadyEvent.Set();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_HOTKEY)
{
HotKeyEventArgs e = new HotKeyEventArgs(m.LParam);
HotKeyHelper.OnHotKeyPressed(e);
}
base.WndProc(ref m);
}
protected override void SetVisibleCore(bool value)
{
// Ensure the window never becomes visible
base.SetVisibleCore(false);
}
private const int WM_HOTKEY = 0x312;
}
}
public class HotKeyEventArgs : EventArgs
{
public readonly Keys Key;
public readonly KeyModifiers Modifiers;
public HotKeyEventArgs(Keys key, KeyModifiers modifiers)
{
this.Key = key;
this.Modifiers = modifiers;
}
public HotKeyEventArgs(IntPtr hotKeyParam)
{
uint param = (uint)hotKeyParam.ToInt64();
Key = (Keys)((param & 0xffff0000) >> 16);
Modifiers = (KeyModifiers)(param & 0x0000ffff);
}
}調(diào)用示例
void RegisterHotKeyTest()
{
HotKeyHelper.RegisterHotKey(Keys.B, KeyModifiers.Alt);
HotKeyHelper.HotKeyPressed += new EventHandler<HotKeyEventArgs>(OnHotKeyPressed);
}
void OnHotKeyPressed(object sender, HotKeyEventArgs e)
{
MessageBox.Show("Alt + B");
}執(zhí)行結(jié)果

如果需要注冊成Windows服務(wù)在后臺運行,需要開啟Service的允許與桌面交互選項。
到此這篇關(guān)于基于C#實現(xiàn)熱鍵注冊工具類的文章就介紹到這了,更多相關(guān)C#熱鍵注冊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#入門之checked和unchecked的區(qū)別實例解析
這篇文章主要介紹了C#中checked和unchecked的區(qū)別,是學(xué)習(xí)C#必須要牢固掌握的,需要的朋友可以參考下2014-08-08
C# Environment.CurrentDirectory 靜態(tài)屬性的實現(xiàn)
本文主要介紹了C# Environment.CurrentDirectory 靜態(tài)屬性的實現(xiàn),它返回當(dāng)前應(yīng)用程序的工作目錄路徑,具有一定的參考價值,感興趣的可以了解一下2024-02-02
利用C#實現(xiàn)SSLSocket加密通訊的方法詳解
這篇文章主要給大家介紹了關(guān)于如何利用C#實現(xiàn)SSLSocket加密通訊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
unity中實現(xiàn)Edge瀏覽器鼠標(biāo)手勢的功能思路詳解
這篇文章主要介紹了unity中實現(xiàn)Edge瀏覽器鼠標(biāo)手勢的功能思路詳解,實現(xiàn)起來其實并不復(fù)雜,涉及的技術(shù)點有pc端和移動端屏幕拖動事件,二維向量的相關(guān)運算,手勢匹配算法,事件系統(tǒng)設(shè)計模式,需要的朋友可以參考下2023-12-12

