WPF實(shí)現(xiàn)監(jiān)聽(tīng)快捷鍵的方式分享
1.調(diào)用Win32 API
優(yōu)先級(jí)最高,全局監(jiān)聽(tīng), 支持最小化失焦等情況
那么,假如我要在一個(gè)WPF程序監(jiān)聽(tīng)CTRL+5按鍵,首先在主窗口程序添加以下代碼:
/// <summary> /// CTRL+5事件Id /// </summary> private const int Ctrl5KeyEventId = 9000; [DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id); protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); var handle = new WindowInteropHelper(this).Handle; var source = HwndSource.FromHwnd(handle); source?.AddHook(HwndHook); //真正注冊(cè)快捷鍵監(jiān)聽(tīng)處理: 同時(shí)注冊(cè)數(shù)字鍵和小鍵盤(pán)的CTRL+5 RegisterHotKey(handle, Ctrl5KeyEventId, (uint)ModifierKeys.Control, (uint)KeyInterop.VirtualKeyFromKey(Key.D5)); RegisterHotKey(handle, Ctrl5KeyEventId, (uint)ModifierKeys.Control, (uint)KeyInterop.VirtualKeyFromKey(Key.NumPad5)); } private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { const int wmHotkey = 0x0312; switch (msg) { case wmHotkey: switch (wParam.ToInt32()) { case Ctrl5KeyEventId: Debug.WriteLine("Win32監(jiān)聽(tīng)CTRL+5成功"); break; } break; } return IntPtr.Zero; } protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); var handle = new WindowInteropHelper(this).Handle; //關(guān)閉窗口后取消注冊(cè) UnregisterHotKey(handle, Ctrl5KeyEventId); }
2.監(jiān)聽(tīng)WPF的KeyDown事件
不夠清真,可選擇,最小化失焦等情況監(jiān)聽(tīng)失效
public MainWindow() { InitializeComponent(); KeyDown += MainWindow_KeyDown; } private void MainWindow_KeyDown(object sender, KeyEventArgs e) { if (Keyboard.Modifiers == ModifierKeys.Control && (e.Key == Key.D5 || e.Key == Key.NumPad5)) { Debug.WriteLine("WPF的KeyDown事件監(jiān)聽(tīng)CTRL+5成功"); ; e.Handled = true; } }
3.XAML綁定命令方式
WPF當(dāng)然優(yōu)先選中命令綁定啦,清真,最小化失焦等情況監(jiān)聽(tīng)失效
以下為Window主窗體的XAML代碼
<Window.CommandBindings> <CommandBinding Command="{x:Static local:Commands.Ctrl5Command}" Executed="Ctrl5Command_OnExecuted"/> </Window.CommandBindings> <Window.InputBindings> <KeyBinding Modifiers="Control" Key="D5" Command="{x:Static local:Commands.Ctrl5Command}" /> <KeyBinding Modifiers="Control" Key="NumPad5" Command="{x:Static local:Commands.Ctrl5Command}" /> </Window.InputBindings>
在Window主窗體后臺(tái)代碼創(chuàng)建命令對(duì)應(yīng)的Executed方法
private void Ctrl5Command_OnExecuted(object sender, ExecutedRoutedEventArgs e) { Debug.WriteLine("WPF的XAML綁定命令監(jiān)聽(tīng)CTRL+5成功"); }
新增命令相關(guān)的靜態(tài)類(lèi):
public static class Commands { public static ICommand Ctrl5Command { get; } = new RoutedCommand(); }
4.細(xì)節(jié)
三個(gè)監(jiān)聽(tīng)方案的優(yōu)先級(jí)
其中Win32 > XAML綁定命令 = KeyDown事件,假如同時(shí)監(jiān)聽(tīng)的話,其中會(huì)只處理高優(yōu)先級(jí)的,以上面的例子,假如
我同時(shí)監(jiān)聽(tīng)三個(gè),只會(huì)處理win32的
Win32監(jiān)聽(tīng)CTRL+5成功
全局監(jiān)聽(tīng)問(wèn)題
其中win32支持全局監(jiān)聽(tīng)鍵盤(pán),也就是窗口在失焦情況下,例如最小化,也能監(jiān)聽(tīng)得到,其中XAML綁定命令和KeyDown事件不支持失焦情況,最小化等情況也就監(jiān)聽(tīng)不到了,因此,要按業(yè)務(wù)選擇方案
5.DEMO
以上就是WPF實(shí)現(xiàn)監(jiān)聽(tīng)快捷鍵的方式分享的詳細(xì)內(nèi)容,更多關(guān)于WPF監(jiān)聽(tīng)快捷鍵的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解C#中Dictionary<TKey,TValue>的存儲(chǔ)結(jié)構(gòu)
無(wú)論是實(shí)際的項(xiàng)目中,還是在我們學(xué)習(xí)的過(guò)程中,都會(huì)重點(diǎn)的應(yīng)用到Dictionary<TKey,?TValue>這個(gè)存儲(chǔ)類(lèi)型,所以本文就來(lái)為大家介紹一下這一存儲(chǔ)結(jié)構(gòu)的相關(guān)知識(shí),希望對(duì)大家有所幫助2023-11-11C#對(duì)稱(chēng)加密(AES加密)每次生成的結(jié)果都不同的實(shí)現(xiàn)思路和代碼實(shí)例
這篇文章主要介紹了C#對(duì)稱(chēng)加密(AES加密)每次生成的結(jié)果都不同的實(shí)現(xiàn)思路和代碼實(shí)例,每次解密時(shí)從密文中截取前16位,這就是實(shí)現(xiàn)隨機(jī)的奧秘,本文同時(shí)給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-07-07C#學(xué)習(xí)基礎(chǔ)概念二十五問(wèn)續(xù)2
C#學(xué)習(xí)基礎(chǔ)概念二十五問(wèn)續(xù)2...2007-04-04C#實(shí)現(xiàn)多線程的幾種方式小結(jié)
多線程是C#中一個(gè)重要的概念,多線程指的是在同一進(jìn)程中同時(shí)運(yùn)行多個(gè)線程的機(jī)制,多線程適用于需要提高系統(tǒng)并發(fā)性、吞吐量和響應(yīng)速度的場(chǎng)景,可以充分利用多核處理器和系統(tǒng)資源,提高應(yīng)用程序的性能和效率,本文介紹了C#實(shí)現(xiàn)多線程的幾種方式,需要的朋友可以參考下2024-07-07