diff --git a/Control/UserControls/MotionControl.xaml.cs b/Control/UserControls/MotionControl.xaml.cs index 3698ceb..87bb813 100644 --- a/Control/UserControls/MotionControl.xaml.cs +++ b/Control/UserControls/MotionControl.xaml.cs @@ -1,5 +1,7 @@ using GeekDesk.Util; using GeekDesk.ViewModel; +using GlobalHotKey; +using HandyControl.Data; using Microsoft.Win32; using System; using System.Collections.Generic; @@ -11,6 +13,7 @@ using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; +using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; @@ -140,11 +143,15 @@ namespace GeekDesk.Control.UserControls ConfigWindow cw = (ConfigWindow)Window.GetWindow(this); try { - Hotkey.Regist(cw.mainWindow, appConfig.HotkeyModifiers, appConfig.Hotkey, () => + if (cw.mainWindow.hotKeyId != -1) + { + Hotkey.UnRegist(new WindowInteropHelper(cw.mainWindow).Handle, Hotkey.keymap[cw.mainWindow.hotKeyId]); + } + cw.mainWindow.hotKeyId = Hotkey.Regist(cw.mainWindow, appConfig.HotkeyModifiers, appConfig.Hotkey, () => { if (cw.mainWindow.Visibility == Visibility.Collapsed) { - ShowApp(cw.mainWindow); + cw.mainWindow.ShowApp(); } else { @@ -153,7 +160,7 @@ namespace GeekDesk.Control.UserControls }); } catch (Exception) { - HandyControl.Controls.Growl.ErrorGlobal("热键注册失败,当前热键已被其它程序占用!"); + HandyControl.Controls.Growl.WarningGlobal("当前快捷键已被其它程序占用(" + appConfig.HotkeyStr + ")!"); } } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 93acadd..b4aa8a9 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -4,6 +4,7 @@ using GeekDesk.Control; using GeekDesk.Util; using GeekDesk.ViewModel; using GlobalHotKey; +using HandyControl.Data; using SharpShell.SharpContextMenu; using System; using System.Collections.ObjectModel; @@ -26,6 +27,8 @@ namespace GeekDesk { public static AppData appData = CommonCode.GetAppDataByFile(); + public int hotKeyId = -1; + public HotKeyManager hkm = new HotKeyManager(); public MainWindow() { LoadData(); @@ -53,20 +56,47 @@ namespace GeekDesk { this.Visibility = Visibility.Collapsed; } - //加载完毕注册热键 - Hotkey.Regist(this, HotkeyModifiers.MOD_CONTROL | HotkeyModifiers.MOD_ALT, Key.Y, ()=> + try + { + HotKey hk = hkm.Register(Key.Y, ModifierKeys.Control); + hkm.KeyPressed += DisplayWindowHotKeyPress; + ////加载完毕注册热键 + //hotKeyId = Hotkey.Regist(this, appData.AppConfig.HotkeyModifiers, appData.AppConfig.Hotkey, () => + //{ + // if (this.Visibility == Visibility.Collapsed) + // { + // ShowApp(); + // } + // else + // { + // this.Visibility = Visibility.Collapsed; + // } + //}); + } catch (Exception) + { + HandyControl.Controls.Growl.WarningGlobal("启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!"); + } + + } + + private void DisplayWindowHotKeyPress(object sender, KeyPressedEventArgs e) + { + if (e.HotKey.Key == Key.Y) { if (this.Visibility == Visibility.Collapsed) { ShowApp(); - } else + } + else { this.Visibility = Visibility.Collapsed; } - }); + } + } + void MainWindow_Resize(object sender, System.EventArgs e) { if (this.DataContext != null) @@ -148,7 +178,7 @@ namespace GeekDesk { ShowApp(); } - private void ShowApp() + public void ShowApp() { if (appData.AppConfig.FollowMouse) { diff --git a/Util/HotKey.cs b/Util/HotKey.cs index 60d0eb6..e9ea296 100644 --- a/Util/HotKey.cs +++ b/Util/HotKey.cs @@ -30,7 +30,7 @@ namespace GeekDesk.Util /// 组合键 /// 快捷键 /// 回调函数 - public static bool Regist(Window window, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack) + public static int Regist(Window window, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack) { var hwnd = new WindowInteropHelper(window).Handle; var _hwndSource = HwndSource.FromHwnd(hwnd); @@ -40,8 +40,8 @@ namespace GeekDesk.Util var vk = KeyInterop.VirtualKeyFromKey(key); keymap[id] = callBack; - - return RegisterHotKey(hwnd, id, fsModifiers, (uint)vk); + if (!RegisterHotKey(hwnd, id, fsModifiers, (uint)vk)) throw new Exception("RegisterHotKey Failed"); + return id; } /// @@ -77,7 +77,7 @@ namespace GeekDesk.Util const int WM_HOTKEY = 0x312; static int keyid = 10; - static Dictionary keymap = new Dictionary(); + public static Dictionary keymap = new Dictionary(); public delegate void HotKeyCallBackHanlder(); } diff --git a/ViewModel/AppConfig.cs b/ViewModel/AppConfig.cs index 542064f..181e054 100644 --- a/ViewModel/AppConfig.cs +++ b/ViewModel/AppConfig.cs @@ -37,9 +37,9 @@ namespace GeekDesk.ViewModel private int pannelCornerRadius = 4; //面板圆角 默认4 [field: NonSerialized] private ObservableCollection selectedMenuIcons; - private string hotkeyStr; - private HotkeyModifiers hotkeyModifiers; - private Key hotkey; + private string hotkeyStr = "Ctrl + Q"; + private HotkeyModifiers hotkeyModifiers = HotkeyModifiers.MOD_CONTROL; + private Key hotkey = Key.Q; #region GetSet public Key Hotkey