diff --git a/Control/UserControls/Config/MotionControl.xaml.cs b/Control/UserControls/Config/MotionControl.xaml.cs index 5d846b6..79699ce 100644 --- a/Control/UserControls/Config/MotionControl.xaml.cs +++ b/Control/UserControls/Config/MotionControl.xaml.cs @@ -7,6 +7,7 @@ using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; @@ -27,8 +28,9 @@ namespace GeekDesk.Control.UserControls.Config /// public partial class MotionControl : UserControl { - private static bool hotkeyFinished = true; //热键设置结束 + public static bool hotkeyFinished = true; //热键设置结束 private static KeyEventArgs prevKeyTemp; //上一个按键 + private static List keysTemp = new List();//存储一次快捷键集合 private static AppConfig appConfig = MainWindow.appData.AppConfig; public MotionControl() @@ -62,6 +64,7 @@ namespace GeekDesk.Control.UserControls.Config appConfig.HotkeyStr = GetKeyName(e); appConfig.HotkeyModifiers = GetModifierKeys(e); prevKeyTemp = e; + keysTemp.Add(e); } } else @@ -75,12 +78,14 @@ namespace GeekDesk.Control.UserControls.Config appConfig.Hotkey = e.Key; appConfig.HotkeyStr += e.Key.ToString(); prevKeyTemp = e; + keysTemp.Add(e); } else if (CheckModifierKeys(e)) { appConfig.HotkeyStr += GetKeyName(e); appConfig.HotkeyModifiers |= GetModifierKeys(e); prevKeyTemp = e; + keysTemp.Add(e); } } } @@ -138,94 +143,94 @@ namespace GeekDesk.Control.UserControls.Config } - private void HotKeyUp(object sender, KeyEventArgs e) + [MethodImpl(MethodImplOptions.Synchronized)] + private void HotKeyUp(object sender, KeyEventArgs e) { - hotkeyFinished = true; - ConfigWindow cw = (ConfigWindow)Window.GetWindow(this); - try + lock(this) { - if (cw.mainWindow.hotKeyId != -1) + bool allKeyUp = true; + //判断所有键是否都松开 + foreach (KeyEventArgs key in keysTemp) { - Hotkey.UnRegist(new WindowInteropHelper(cw.mainWindow).Handle, Hotkey.keymap[cw.mainWindow.hotKeyId]); + HandyControl.Controls.Growl.SuccessGlobal(key.Key.ToString() + "=" + key.KeyStates); + if (key.KeyStates == KeyStates.Down) + { + allKeyUp = false; + break; + } } - cw.mainWindow.hotKeyId = Hotkey.Regist(cw.mainWindow, appConfig.HotkeyModifiers, appConfig.Hotkey, () => + if (allKeyUp && !hotkeyFinished) { - if (cw.mainWindow.Visibility == Visibility.Collapsed) + hotkeyFinished = true; + if (MainWindow.hotKeyId != -1) { - cw.mainWindow.ShowApp(); + Hotkey.UnRegist(new WindowInteropHelper(MainWindow.mainWindow).Handle, Hotkey.keymap[MainWindow.hotKeyId]); } - else - { - cw.mainWindow.Visibility = Visibility.Collapsed; - } - }); - } catch (Exception) - { - HandyControl.Controls.Growl.WarningGlobal("当前快捷键已被其它程序占用(" + appConfig.HotkeyStr + ")!"); + MainWindow.RegisterHotKey(); + } } - } - private void ShowApp(MainWindow mainWindow) - { - if (appConfig.FollowMouse) - { - ShowAppAndFollowMouse(mainWindow); - } - else - { - this.Visibility = Visibility.Visible; - } - Keyboard.Focus(this); - } + //private void ShowApp(MainWindow mainWindow) + //{ + // if (appConfig.FollowMouse) + // { + // ShowAppAndFollowMouse(mainWindow); + // } + // else + // { + // this.Visibility = Visibility.Visible; + // } + // Keyboard.Focus(this); + //} - /// - /// 随鼠标位置显示面板 (鼠标始终在中间) - /// - private void ShowAppAndFollowMouse(MainWindow mainWindow) - { - //获取鼠标位置 - System.Windows.Point p = MouseUtil.GetMousePosition(); - double left = SystemParameters.VirtualScreenLeft; - double top = SystemParameters.VirtualScreenTop; - double width = SystemParameters.VirtualScreenWidth; - double height = SystemParameters.VirtualScreenHeight; - double right = width - Math.Abs(left); - double bottom = height - Math.Abs(top); + ///// + ///// 随鼠标位置显示面板 (鼠标始终在中间) + ///// + //private void ShowAppAndFollowMouse(MainWindow mainWindow) + //{ + // //获取鼠标位置 + // System.Windows.Point p = MouseUtil.GetMousePosition(); + // double left = SystemParameters.VirtualScreenLeft; + // double top = SystemParameters.VirtualScreenTop; + // double width = SystemParameters.VirtualScreenWidth; + // double height = SystemParameters.VirtualScreenHeight; + // double right = width - Math.Abs(left); + // double bottom = height - Math.Abs(top); - if (p.X - mainWindow.Width / 2 < left) - { - //判断是否在最左边缘 - mainWindow.Left = left; - } - else if (p.X + mainWindow.Width / 2 > right) - { - //判断是否在最右边缘 - mainWindow.Left = right - mainWindow.Width; - } - else - { - mainWindow.Left = p.X - mainWindow.Width / 2; - } + // if (p.X - mainWindow.Width / 2 < left) + // { + // //判断是否在最左边缘 + // mainWindow.Left = left; + // } + // else if (p.X + mainWindow.Width / 2 > right) + // { + // //判断是否在最右边缘 + // mainWindow.Left = right - mainWindow.Width; + // } + // else + // { + // mainWindow.Left = p.X - mainWindow.Width / 2; + // } - if (p.Y - mainWindow.Height / 2 < top) - { - //判断是否在最上边缘 - mainWindow.Top = top; - } - else if (p.Y + mainWindow.Height / 2 > bottom) - { - //判断是否在最下边缘 - mainWindow.Top = bottom - mainWindow.Height; - } - else - { - mainWindow.Top = p.Y - mainWindow.Height / 2; - } + // if (p.Y - mainWindow.Height / 2 < top) + // { + // //判断是否在最上边缘 + // mainWindow.Top = top; + // } + // else if (p.Y + mainWindow.Height / 2 > bottom) + // { + // //判断是否在最下边缘 + // mainWindow.Top = bottom - mainWindow.Height; + // } + // else + // { + // mainWindow.Top = p.Y - mainWindow.Height / 2; + // } - mainWindow.Visibility = Visibility.Visible; - } + // mainWindow.Visibility = Visibility.Visible; + //} } } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 78cac80..f2de028 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -1,6 +1,7 @@ using DraggAnimatedPanelExample; using GeekDesk.Constant; using GeekDesk.Control; +using GeekDesk.Control.UserControls.Config; using GeekDesk.Control.Windows; using GeekDesk.Task; using GeekDesk.Util; @@ -29,12 +30,14 @@ namespace GeekDesk { public static AppData appData = CommonCode.GetAppDataByFile(); - public int hotKeyId = -1; + public static int hotKeyId = -1; + public static MainWindow mainWindow; public HotKeyManager hkm = new HotKeyManager(); public MainWindow() { LoadData(); InitializeComponent(); + mainWindow = this; this.Topmost = true; this.Loaded += Window_Loaded; this.SizeChanged += MainWindow_Resize; @@ -58,28 +61,43 @@ namespace GeekDesk if (!appData.AppConfig.StartedShowPanel) { this.Visibility = Visibility.Collapsed; + } else + { + ShowApp(); } + + + } + + /// + /// 注册当前窗口的热键 + /// + public static void RegisterHotKey() + { 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) + //加载完毕注册热键 + hotKeyId = Hotkey.Regist(new WindowInteropHelper(MainWindow.mainWindow).Handle, appData.AppConfig.HotkeyModifiers, appData.AppConfig.Hotkey, () => + { + if (MotionControl.hotkeyFinished) + { + if (mainWindow.Visibility == Visibility.Collapsed) + { + ShowApp(); + } + else + { + mainWindow.Visibility = Visibility.Collapsed; + } + } + }); + //HandyControl.Controls.Growl.SuccessGlobal("快捷键注册成功(" + appData.AppConfig.HotkeyStr + ")!"); + } + catch (Exception) { HandyControl.Controls.Growl.WarningGlobal("启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!"); } - + } private void DisplayWindowHotKeyPress(object sender, KeyPressedEventArgs e) @@ -180,22 +198,22 @@ namespace GeekDesk { ShowApp(); } - public void ShowApp() + public static void ShowApp() { if (appData.AppConfig.FollowMouse) { ShowAppAndFollowMouse(); } else { - this.Visibility = Visibility.Visible; + mainWindow.Visibility = Visibility.Visible; } - Keyboard.Focus(this); + Keyboard.Focus(mainWindow); } /// /// 随鼠标位置显示面板 (鼠标始终在中间) /// - private void ShowAppAndFollowMouse() + private static void ShowAppAndFollowMouse() { //获取鼠标位置 System.Windows.Point p = MouseUtil.GetMousePosition(); @@ -207,38 +225,38 @@ namespace GeekDesk double bottom = height - Math.Abs(top); - if (p.X - this.Width / 2 < left) + if (p.X - mainWindow.Width / 2 < left) { //判断是否在最左边缘 - this.Left = left; + mainWindow.Left = left; } - else if (p.X + this.Width / 2 > right) + else if (p.X + mainWindow.Width / 2 > right) { //判断是否在最右边缘 - this.Left = right - this.Width; + mainWindow.Left = right - mainWindow.Width; } else { - this.Left = p.X - this.Width / 2; + mainWindow.Left = p.X - mainWindow.Width / 2; } - if (p.Y - this.Height / 2 < top) + if (p.Y - mainWindow.Height / 2 < top) { //判断是否在最上边缘 - this.Top = top; + mainWindow.Top = top; } - else if (p.Y + this.Height / 2 > bottom) + else if (p.Y + mainWindow.Height / 2 > bottom) { //判断是否在最下边缘 - this.Top = bottom - this.Height; + mainWindow.Top = bottom - mainWindow.Height; } else { - this.Top = p.Y - this.Height / 2; + mainWindow.Top = p.Y - mainWindow.Height / 2; } - this.Visibility = Visibility.Visible; + mainWindow.Visibility = Visibility.Visible; } diff --git a/Util/HotKey.cs b/Util/HotKey.cs index e9ea296..efce0ec 100644 --- a/Util/HotKey.cs +++ b/Util/HotKey.cs @@ -30,17 +30,16 @@ namespace GeekDesk.Util /// 组合键 /// 快捷键 /// 回调函数 - public static int Regist(Window window, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack) + public static int Regist(IntPtr windowHandle, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack) { - var hwnd = new WindowInteropHelper(window).Handle; - var _hwndSource = HwndSource.FromHwnd(hwnd); + var _hwndSource = HwndSource.FromHwnd(windowHandle); _hwndSource.AddHook(WndProc); int id = keyid++; var vk = KeyInterop.VirtualKeyFromKey(key); - keymap[id] = callBack; - if (!RegisterHotKey(hwnd, id, fsModifiers, (uint)vk)) throw new Exception("RegisterHotKey Failed"); + keymap.Add(id, callBack); + if (!RegisterHotKey(windowHandle, id, fsModifiers, (uint)vk)) throw new Exception("RegisterHotKey Failed"); return id; } @@ -67,10 +66,15 @@ namespace GeekDesk.Util /// 回调函数 public static void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack) { - foreach (KeyValuePair var in keymap) + + List list = new List(keymap.Keys); + for (int i=0; i < list.Count; i++) { - if (var.Value == callBack) - UnregisterHotKey(hWnd, var.Key); + if (keymap[list[i]] == callBack) + { + UnregisterHotKey(hWnd, list[i]); + keymap.Remove(list[i]); + } } }