Browse Source

热键注册及绑定

Demo_Liu 4 years ago
parent
commit
2f0d6a9405

+ 3 - 1
Control/ConfigWindow.xaml.cs

@@ -18,13 +18,15 @@ namespace GeekDesk.Control
         private static AboutControl about = new AboutControl();
         private static ThemeControl theme = new ThemeControl();
         private static MotionControl motion = new MotionControl();
+        public MainWindow mainWindow;
 
-        public ConfigWindow(AppConfig appConfig)
+        public ConfigWindow(AppConfig appConfig, MainWindow mainWindow)
         {
             InitializeComponent();
             this.DataContext = appConfig;
             RightCard.Content = about;
             this.Topmost = true;
+            this.mainWindow = mainWindow;
         }
 
          

+ 2 - 1
Control/UserControls/MotionControl.xaml

@@ -60,9 +60,10 @@
                         VerticalAlignment="Top" 
                         IsReadOnly="True" 
                         IsReadOnlyCaretVisible="True" 
-                        Width="80"
+                        Width="200"
                         Text="{Binding HotkeyStr}" 
                         KeyDown="HotKeyDown"
+                        KeyUp="HotKeyUp"
                         Margin="12.967,-7.38,-12.967,0"/>
         </hc:UniformSpacingPanel>
     </hc:SimplePanel>

+ 165 - 18
Control/UserControls/MotionControl.xaml.cs

@@ -23,7 +23,8 @@ namespace GeekDesk.Control.UserControls
     /// </summary>
     public partial class MotionControl : UserControl
     {
-        private static bool controlKeyDown = false;
+        private static bool hotkeyFinished = true; //热键设置结束
+        private static KeyEventArgs prevKeyTemp; //上一个按键
         private static AppConfig appConfig = MainWindow.appData.AppConfig;
 
         public MotionControl()
@@ -33,44 +34,190 @@ namespace GeekDesk.Control.UserControls
 
 
         /// <summary>
-        /// 热键按下
+        /// 注册热键按下
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void HotKeyDown(object sender, KeyEventArgs e)
         {
-            if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) 
-                || e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Windows)
-                || e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt)
-                || (e.Key >= Key.A && e.Key <= Key.Z))
-            {
-                if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control)
-                || e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Windows)
-                || e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt))
+            if (!e.IsRepeat)
+            {
+                if (hotkeyFinished)
+                {
+                    appConfig.Hotkey = 0;
+                    appConfig.HotkeyStr = "";
+                    appConfig.HotkeyModifiers = 0;
+                    hotkeyFinished = false;
+                }
+                //首次按下按键
+                if (appConfig.HotkeyStr == null || appConfig.HotkeyStr.Length == 0)
                 {
-                    appConfig.HotkeyStr += GetKeyName(e);
-                } else if (appConfig.HotkeyStr.Length > 0
-                && (e.Key >= Key.A && e.Key <= Key.Z))
+                    if (CheckModifierKeys(e))
+                    {
+                        //辅助键
+                        appConfig.HotkeyStr = GetKeyName(e);
+                        appConfig.HotkeyModifiers = GetModifierKeys(e);
+                        prevKeyTemp = e;
+                    }
+                }
+                else
                 {
-                    appConfig.HotkeyStr += e.Key.ToString();
+                    //非首次按下  需要判断前一个键值是否为辅助键
+                    if (CheckModifierKeys(prevKeyTemp)
+                        && ((e.Key >= Key.A && e.Key <= Key.Z)
+                        || (e.Key >= Key.F1 && e.Key <= Key.F12)
+                        || (e.Key >= Key.D0 && e.Key <= Key.D9)))
+                    {
+                        appConfig.Hotkey = e.Key;
+                        appConfig.HotkeyStr += e.Key.ToString();
+                        prevKeyTemp = e;
+                    }
+                    else if (CheckModifierKeys(e))
+                    {
+                        appConfig.HotkeyStr += GetKeyName(e);
+                        appConfig.HotkeyModifiers |= GetModifierKeys(e);
+                        prevKeyTemp = e;
+                    }
                 }
             }
         }
 
         private string GetKeyName(KeyEventArgs e)
         {
-            if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control))
+            Key key = e.Key;
+            if (key == Key.LeftCtrl || key == Key.RightCtrl)
             {
                 return "Ctrl + ";
-            } else if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Windows))
+            } else if (key == Key.LWin || key == Key.RWin)
             {
                 return "Win + ";
-            } else if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt))
+            }
+            else if (key == Key.LeftShift || key == Key.RightShift)
+            {
+                return "Shift + ";
+            }
+            else
             {
                 return "Alt + ";
             }
-            return "";
+
+        }
+
+        private HotkeyModifiers GetModifierKeys(KeyEventArgs e)
+        {
+            Key key = e.Key;
+            if (key == Key.LeftCtrl || key == Key.RightCtrl)
+            {
+                return HotkeyModifiers.MOD_CONTROL;
+            }
+            else if (key == Key.LWin || key == Key.RWin)
+            {
+                return HotkeyModifiers.MOD_WIN;
+            }
+            else if (key == Key.LeftShift || key == Key.RightShift)
+            {
+                return HotkeyModifiers.MOD_SHIFT;
+            }
+            else
+            {
+                return HotkeyModifiers.MOD_ALT;
+            }
+        }
+
+        private bool CheckModifierKeys(KeyEventArgs e)
+        {
+            Key key = e.Key;
+            return key == Key.LeftCtrl || key == Key.RightCtrl
+                || key == Key.LWin || key == Key.RWin
+                || key == Key.LeftShift || key == Key.RightShift
+                || key == Key.LeftAlt || key == Key.RightAlt;
+        }
+
+
+        private void HotKeyUp(object sender, KeyEventArgs e)
+        {
+            hotkeyFinished = true;
+            ConfigWindow cw = (ConfigWindow)Window.GetWindow(this);
+            try
+            {
+                Hotkey.Regist(cw.mainWindow, appConfig.HotkeyModifiers, appConfig.Hotkey, () =>
+                {
+                    if (cw.mainWindow.Visibility == Visibility.Collapsed)
+                    {
+                        ShowApp(cw.mainWindow);
+                    }
+                    else
+                    {
+                        cw.mainWindow.Visibility = Visibility.Collapsed;
+                    }
+                });
+            } catch (Exception)
+            {
+                HandyControl.Controls.Growl.ErrorGlobal("热键注册失败,当前热键已被其它程序占用!");
+            }
+            
+        }
+
+        private void ShowApp(MainWindow mainWindow)
+        {
+            if (appConfig.FollowMouse)
+            {
+                ShowAppAndFollowMouse(mainWindow);
+            }
+            else
+            {
+                this.Visibility = Visibility.Visible;
+            }
+            Keyboard.Focus(this);
         }
 
+        /// <summary>
+        /// 随鼠标位置显示面板 (鼠标始终在中间)
+        /// </summary>
+        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.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;
+        }
     }
 }

+ 34 - 27
MainWindow.xaml.cs

@@ -137,6 +137,29 @@ namespace GeekDesk
         //    appData.AppConfig.MenuCardWidth = LeftColumn.Width.Value;
         //}
 
+       
+
+        /// <summary>
+        /// 右键任务栏图标 显示主面板
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void ShowApp(object sender, RoutedEventArgs e)
+        {
+            ShowApp();
+        }
+        private void ShowApp()
+        {
+            if (appData.AppConfig.FollowMouse)
+            {
+                ShowAppAndFollowMouse();
+            } else
+            {
+                this.Visibility = Visibility.Visible;
+            }
+            Keyboard.Focus(this);
+        }
+
         /// <summary>
         /// 随鼠标位置显示面板 (鼠标始终在中间)
         /// </summary>
@@ -151,30 +174,34 @@ namespace GeekDesk
             double right = width - Math.Abs(left);
             double bottom = height - Math.Abs(top);
 
-            
+
             if (p.X - this.Width / 2 < left)
             {
                 //判断是否在最左边缘
                 this.Left = left;
-            } else if (p.X + this.Width / 2 > right) 
+            }
+            else if (p.X + this.Width / 2 > right)
             {
                 //判断是否在最右边缘
                 this.Left = right - this.Width;
-            } else
+            }
+            else
             {
                 this.Left = p.X - this.Width / 2;
             }
 
-            
+
             if (p.Y - this.Height / 2 < top)
             {
                 //判断是否在最上边缘
                 this.Top = top;
-            } else if (p.Y + this.Height/2 > bottom) 
+            }
+            else if (p.Y + this.Height / 2 > bottom)
             {
                 //判断是否在最下边缘
                 this.Top = bottom - this.Height;
-            } else
+            }
+            else
             {
                 this.Top = p.Y - this.Height / 2;
             }
@@ -182,26 +209,6 @@ namespace GeekDesk
             this.Visibility = Visibility.Visible;
         }
 
-        /// <summary>
-        /// 右键任务栏图标 显示主面板
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private void ShowApp(object sender, RoutedEventArgs e)
-        {
-            ShowApp();
-        }
-        private void ShowApp()
-        {
-            if (appData.AppConfig.FollowMouse)
-            {
-                ShowAppAndFollowMouse();
-            } else
-            {
-                this.Visibility = Visibility.Visible;
-            }
-            Keyboard.Focus(this);
-        }
 
         /// <summary>
         /// 图片图标单击事件
@@ -266,7 +273,7 @@ namespace GeekDesk
         private void ConfigButtonClick(object sender, RoutedEventArgs e)
         {
             //SettingMenu.IsOpen = true;
-            new ConfigWindow(appData.AppConfig).Show();
+            new ConfigWindow(appData.AppConfig, this).Show();
         }
 
         /// <summary>