From 39607bdcb5bea1355820c96354b4105294807787 Mon Sep 17 00:00:00 2001 From: liufei Date: Wed, 25 May 2022 09:50:43 +0800 Subject: [PATCH] =?UTF-8?q?:children=5Fcrossing:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=BF=AB=E6=8D=B7=E9=94=AE/=E4=BC=98=E5=8C=96=E6=8B=BE?= =?UTF-8?q?=E8=89=B2=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UserControls/Config/MotionControl.xaml | 72 +++--- .../UserControls/Config/MotionControl.xaml.cs | 228 ++++++++++-------- .../Windows/GlobalColorPickerWindow.xaml.cs | 1 + Util/GlobalHotKey.cs | 1 + Util/KeyUtil.cs | 137 +++++++++++ ViewModel/AppConfig.cs | 19 +- 6 files changed, 333 insertions(+), 125 deletions(-) create mode 100644 Util/KeyUtil.cs diff --git a/Control/UserControls/Config/MotionControl.xaml b/Control/UserControls/Config/MotionControl.xaml index 2c482fc..206766b 100644 --- a/Control/UserControls/Config/MotionControl.xaml +++ b/Control/UserControls/Config/MotionControl.xaml @@ -10,10 +10,11 @@ d:DataContext="{d:DesignInstance Type=viewmodel:AppConfig}" xmlns:cst="clr-namespace:GeekDesk.Constant" Background="Transparent" - d:DesignHeight="400" d:DesignWidth="450"> + d:DesignHeight="450" d:DesignWidth="450"> + @@ -108,21 +109,34 @@ IsChecked="{Binding AppHideType, Mode=TwoWay, Converter={StaticResource HideTypeConvert}, ConverterParameter=3}"/> + + + + + + + + + Tag="{x:Static cst:HotKeyType.Main}" + VerticalAlignment="Top" + IsReadOnly="True" + IsReadOnlyCaretVisible="True" + Width="170" + Text="{Binding HotkeyStr}" + KeyDown="HotKeyDown" + KeyUp="HotKeyUp" + InputMethod.IsInputMethodEnabled="False" + /> + Tag="{x:Static cst:HotKeyType.ToDo}" + VerticalAlignment="Top" + IsReadOnly="True" + IsReadOnlyCaretVisible="True" + Width="170" + Text="{Binding ToDoHotkeyStr}" + KeyDown="HotKeyDown" + KeyUp="HotKeyUp" + InputMethod.IsInputMethodEnabled="False" + /> + Tag="{x:Static cst:HotKeyType.ColorPicker}" + VerticalAlignment="Top" + IsReadOnly="True" + IsReadOnlyCaretVisible="True" + Width="170" + Text="{Binding ColorPickerHotkeyStr}" + KeyDown="HotKeyDown" + KeyUp="HotKeyUp" + InputMethod.IsInputMethodEnabled="False" + /> private void HotKeyDown(object sender, KeyEventArgs e) { - HotKeyType hkType = (HotKeyType)(sender as TextBox).Tag; + Console.WriteLine("downKey:" + e.Key.ToString()); - Key downKey = e.Key; - if (downKey == Key.System) + lock (this) { - downKey = e.SystemKey; - } + HotKeyType hkType = (HotKeyType)(sender as TextBox).Tag; - if (!CheckIsEnable(hkType)) return; - - - if (prevKeyTemp == Key.None || prevKeyTemp != downKey) - { - if (hotkeyFinished) + Key downKey = e.Key; + if (downKey == Key.System) { - - switch (hkType) - { - case HotKeyType.Main: - appConfig.Hotkey = 0; - appConfig.HotkeyStr = ""; - appConfig.HotkeyModifiers = 0; - break; - case HotKeyType.ToDo: - appConfig.ToDoHotkey = 0; - appConfig.ToDoHotkeyStr = ""; - appConfig.ToDoHotkeyModifiers = 0; - break; - case HotKeyType.ColorPicker: - appConfig.ColorPickerHotkey = 0; - appConfig.ColorPickerHotkeyStr = ""; - appConfig.ColorPickerHotkeyModifiers = 0; - break; - } - hotkeyFinished = false; + downKey = e.SystemKey; } - //首次按下按键 - if ((HotKeyType.Main == hkType && (appConfig.HotkeyStr == null || appConfig.HotkeyStr.Length == 0)) - || (HotKeyType.ToDo == hkType && (appConfig.ToDoHotkeyStr == null || appConfig.ToDoHotkeyStr.Length == 0)) - || (HotKeyType.ColorPicker == hkType && (appConfig.ColorPickerHotkeyStr == null || appConfig.ColorPickerHotkeyStr.Length == 0)) - ) + + if (!CheckIsEnable(hkType)) return; + + + Console.WriteLine("prevKeyTemp:" + prevKeyTemp.ToString()); + + if (prevKeyTemp == Key.None || prevKeyTemp != downKey) { - if (CheckModifierKeys(downKey)) + if (hotkeyFinished) { - //辅助键 switch (hkType) { case HotKeyType.Main: - appConfig.HotkeyStr = GetKeyName(downKey); - appConfig.HotkeyModifiers = GetModifierKeys(downKey); + appConfig.Hotkey = Key.None; + appConfig.HotkeyStr = ""; + appConfig.HotkeyModifiers = GlobalHotKey.HotkeyModifiers.None; break; case HotKeyType.ToDo: - appConfig.ToDoHotkeyStr = GetKeyName(downKey); - appConfig.ToDoHotkeyModifiers = GetModifierKeys(downKey); + appConfig.ToDoHotkey = Key.None; + appConfig.ToDoHotkeyStr = ""; + appConfig.ToDoHotkeyModifiers = GlobalHotKey.HotkeyModifiers.None; break; case HotKeyType.ColorPicker: - appConfig.ColorPickerHotkeyStr = GetKeyName(downKey); - appConfig.ColorPickerHotkeyModifiers = GetModifierKeys(downKey); + appConfig.ColorPickerHotkey = Key.None; + appConfig.ColorPickerHotkeyStr = ""; + appConfig.ColorPickerHotkeyModifiers = GlobalHotKey.HotkeyModifiers.None; break; } - - prevKeyTemp = downKey; - keysTemp.Add(e); + hotkeyFinished = false; } - } - else - { - //非首次按下 需要判断前一个键值是否为辅助键 - if (CheckModifierKeys(prevKeyTemp) - && ((downKey >= Key.A && downKey <= Key.Z) - || (downKey >= Key.F1 && downKey <= Key.F12) - || (downKey >= Key.D0 && downKey <= Key.D9))) - { - switch (hkType) - { - case HotKeyType.Main: - appConfig.Hotkey = downKey; - appConfig.HotkeyStr += downKey.ToString(); - break; - case HotKeyType.ToDo: - appConfig.ToDoHotkey = downKey; - appConfig.ToDoHotkeyStr += downKey.ToString(); - break; - case HotKeyType.ColorPicker: - appConfig.ColorPickerHotkey = downKey; - appConfig.ColorPickerHotkeyStr += downKey.ToString(); - break; - } - prevKeyTemp = downKey; - keysTemp.Add(e); + //首次按下按键 + if ((HotKeyType.Main == hkType && (appConfig.HotkeyStr == null || appConfig.HotkeyStr.Length == 0)) + || (HotKeyType.ToDo == hkType && (appConfig.ToDoHotkeyStr == null || appConfig.ToDoHotkeyStr.Length == 0)) + || (HotKeyType.ColorPicker == hkType && (appConfig.ColorPickerHotkeyStr == null || appConfig.ColorPickerHotkeyStr.Length == 0)) + ) + { + if (CheckModifierKeys(downKey)) + { + //辅助键 + switch (hkType) + { + case HotKeyType.Main: + appConfig.HotkeyStr = GetKeyName(downKey); + appConfig.HotkeyModifiers = GetModifierKeys(downKey); + break; + case HotKeyType.ToDo: + appConfig.ToDoHotkeyStr = GetKeyName(downKey); + appConfig.ToDoHotkeyModifiers = GetModifierKeys(downKey); + break; + case HotKeyType.ColorPicker: + appConfig.ColorPickerHotkeyStr = GetKeyName(downKey); + appConfig.ColorPickerHotkeyModifiers = GetModifierKeys(downKey); + break; + } + Console.WriteLine("进入设置" + downKey.ToString()); + prevKeyTemp = downKey; + keysTemp.Add(e); + } } - else if (CheckModifierKeys(downKey)) + else { - switch (hkType) + //非首次按下 需要判断前一个键值是否为辅助键 + if (CheckModifierKeys(prevKeyTemp) + && ((downKey >= Key.A && downKey <= Key.Z) + || (downKey >= Key.F1 && downKey <= Key.F12) + || (downKey >= Key.D0 && downKey <= Key.D9) + || downKey == Key.Oem3 + )) { - case HotKeyType.Main: - appConfig.HotkeyStr += GetKeyName(downKey); - appConfig.HotkeyModifiers |= GetModifierKeys(downKey); - break; - case HotKeyType.ToDo: - appConfig.ToDoHotkeyStr += GetKeyName(downKey); - appConfig.ToDoHotkeyModifiers |= GetModifierKeys(downKey); - break; - case HotKeyType.ColorPicker: - appConfig.ColorPickerHotkeyStr += GetKeyName(downKey); - appConfig.ColorPickerHotkeyModifiers |= GetModifierKeys(downKey); - break; + KeyUtil.KeyProp keyProp = new KeyUtil.KeyProp(); + KeyUtil.KeyToChar(downKey, ref keyProp, true); + string downKeyStr = keyProp.character.ToString(); + //string downKeyStr = ""; + switch (hkType) + { + case HotKeyType.Main: + appConfig.Hotkey = downKey; + appConfig.HotkeyStr += downKeyStr; + break; + case HotKeyType.ToDo: + appConfig.ToDoHotkey = downKey; + appConfig.ToDoHotkeyStr += downKeyStr; + break; + case HotKeyType.ColorPicker: + appConfig.ColorPickerHotkey = downKey; + appConfig.ColorPickerHotkeyStr += downKeyStr; + break; + } + prevKeyTemp = downKey; + keysTemp.Add(e); } + else if (CheckModifierKeys(downKey)) + { + switch (hkType) + { + case HotKeyType.Main: + appConfig.HotkeyStr += GetKeyName(downKey); + appConfig.HotkeyModifiers |= GetModifierKeys(downKey); + break; + case HotKeyType.ToDo: + appConfig.ToDoHotkeyStr += GetKeyName(downKey); + appConfig.ToDoHotkeyModifiers |= GetModifierKeys(downKey); + break; + case HotKeyType.ColorPicker: + appConfig.ColorPickerHotkeyStr += GetKeyName(downKey); + appConfig.ColorPickerHotkeyModifiers |= GetModifierKeys(downKey); + break; + } - prevKeyTemp = downKey; - keysTemp.Add(e); + prevKeyTemp = downKey; + keysTemp.Add(e); + } } } } @@ -200,13 +213,9 @@ namespace GeekDesk.Control.UserControls.Config } - [MethodImpl(MethodImplOptions.Synchronized)] + //[MethodImpl(MethodImplOptions.Synchronized)] private void HotKeyUp(object sender, KeyEventArgs e) { - HotKeyType hkType = (HotKeyType)(sender as TextBox).Tag; - - if (!CheckIsEnable(hkType)) return; - lock (this) { bool allKeyUp = true; @@ -225,6 +234,9 @@ namespace GeekDesk.Control.UserControls.Config prevKeyTemp = Key.None; hotkeyFinished = true; + HotKeyType hkType = (HotKeyType)(sender as TextBox).Tag; + if (!CheckIsEnable(hkType)) return; + switch (hkType) { case HotKeyType.Main: @@ -336,15 +348,41 @@ namespace GeekDesk.Control.UserControls.Config { case HotKeyType.Main: if (true == appConfig.EnableAppHotKey) - MainWindow.RegisterHotKey(false); + { + MainWindow.RegisterHotKey(false); + } else + { + if (MainWindow.hotKeyId != -1) + { + GlobalHotKey.Dispose(MainWindow.hotKeyId); + } + } break; case HotKeyType.ToDo: if (true == appConfig.EnableTodoHotKey) + { MainWindow.RegisterCreateToDoHotKey(false); + } + else + { + if (MainWindow.hotKeyId != -1) + { + GlobalHotKey.Dispose(MainWindow.toDoHotKeyId); + } + } break; case HotKeyType.ColorPicker: if (true == appConfig.EnableColorPickerHotKey) + { MainWindow.RegisterColorPickerHotKey(false); + } + else + { + if (MainWindow.hotKeyId != -1) + { + GlobalHotKey.Dispose(MainWindow.colorPickerHotKeyId); + } + } break; } } diff --git a/Control/Windows/GlobalColorPickerWindow.xaml.cs b/Control/Windows/GlobalColorPickerWindow.xaml.cs index 1407734..5045abf 100644 --- a/Control/Windows/GlobalColorPickerWindow.xaml.cs +++ b/Control/Windows/GlobalColorPickerWindow.xaml.cs @@ -81,6 +81,7 @@ namespace GeekDesk.Control.Windows if (window == null || !window.Activate()) { window = new GlobalColorPickerWindow(); + window.Show(); } window.Hide(); GlobalColorPickerWindow thisWindow = (GlobalColorPickerWindow)window; diff --git a/Util/GlobalHotKey.cs b/Util/GlobalHotKey.cs index f1c736d..c4744fb 100644 --- a/Util/GlobalHotKey.cs +++ b/Util/GlobalHotKey.cs @@ -10,6 +10,7 @@ namespace GeekDesk.Util { public enum HotkeyModifiers { + None = 0, MOD_ALT = 0x1, MOD_CONTROL = 0x2, MOD_SHIFT = 0x4, diff --git a/Util/KeyUtil.cs b/Util/KeyUtil.cs new file mode 100644 index 0000000..be9330f --- /dev/null +++ b/Util/KeyUtil.cs @@ -0,0 +1,137 @@ +using System; + +using System.Windows.Input; + +namespace GeekDesk.Util +{ + public class KeyUtil + { + public class KeyProp + { + public Key key; + public bool printable; + public char character; + public bool shift; + public bool ctrl; + public bool alt; + public int type; //sideband + public string s; //sideband + }; + + public static void KeyToChar(Key key, + ref KeyProp keyProp, + bool downCap = false, + bool downShift = false + ) + { + bool iscap; + bool caplock = false; + bool shift; + + keyProp.key = key; + + keyProp.alt = Keyboard.IsKeyDown(Key.LeftAlt) || + Keyboard.IsKeyDown(Key.RightAlt); + + keyProp.ctrl = Keyboard.IsKeyDown(Key.LeftCtrl) || + Keyboard.IsKeyDown(Key.RightCtrl); + + keyProp.shift = Keyboard.IsKeyDown(Key.LeftShift) || + Keyboard.IsKeyDown(Key.RightShift); + + if (keyProp.alt || keyProp.ctrl) + { + keyProp.printable = false; + keyProp.type = 1; + } + else + { + keyProp.printable = true; + keyProp.type = 0; + } + + shift = downShift || keyProp.shift; + //caplock = Console.CapsLock; //Keyboard.IsKeyToggled(Key.CapsLock); + iscap = downCap || ((caplock && !shift) || (!caplock && shift)); + + switch (key) + { + case Key.Enter: keyProp.character = '\n'; return; + case Key.A: keyProp.character = (iscap ? 'A' : 'a'); return; + case Key.B: keyProp.character = (iscap ? 'B' : 'b'); return; + case Key.C: keyProp.character = (iscap ? 'C' : 'c'); return; + case Key.D: keyProp.character = (iscap ? 'D' : 'd'); return; + case Key.E: keyProp.character = (iscap ? 'E' : 'e'); return; + case Key.F: keyProp.character = (iscap ? 'F' : 'f'); return; + case Key.G: keyProp.character = (iscap ? 'G' : 'g'); return; + case Key.H: keyProp.character = (iscap ? 'H' : 'h'); return; + case Key.I: keyProp.character = (iscap ? 'I' : 'i'); return; + case Key.J: keyProp.character = (iscap ? 'J' : 'j'); return; + case Key.K: keyProp.character = (iscap ? 'K' : 'k'); return; + case Key.L: keyProp.character = (iscap ? 'L' : 'l'); return; + case Key.M: keyProp.character = (iscap ? 'M' : 'm'); return; + case Key.N: keyProp.character = (iscap ? 'N' : 'n'); return; + case Key.O: keyProp.character = (iscap ? 'O' : 'o'); return; + case Key.P: keyProp.character = (iscap ? 'P' : 'p'); return; + case Key.Q: keyProp.character = (iscap ? 'Q' : 'q'); return; + case Key.R: keyProp.character = (iscap ? 'R' : 'r'); return; + case Key.S: keyProp.character = (iscap ? 'S' : 's'); return; + case Key.T: keyProp.character = (iscap ? 'T' : 't'); return; + case Key.U: keyProp.character = (iscap ? 'U' : 'u'); return; + case Key.V: keyProp.character = (iscap ? 'V' : 'v'); return; + case Key.W: keyProp.character = (iscap ? 'W' : 'w'); return; + case Key.X: keyProp.character = (iscap ? 'X' : 'x'); return; + case Key.Y: keyProp.character = (iscap ? 'Y' : 'y'); return; + case Key.Z: keyProp.character = (iscap ? 'Z' : 'z'); return; + case Key.D0: keyProp.character = (shift ? ')' : '0'); return; + case Key.D1: keyProp.character = (shift ? '!' : '1'); return; + case Key.D2: keyProp.character = (shift ? '@' : '2'); return; + case Key.D3: keyProp.character = (shift ? '#' : '3'); return; + case Key.D4: keyProp.character = (shift ? '$' : '4'); return; + case Key.D5: keyProp.character = (shift ? '%' : '5'); return; + case Key.D6: keyProp.character = (shift ? '^' : '6'); return; + case Key.D7: keyProp.character = (shift ? '&' : '7'); return; + case Key.D8: keyProp.character = (shift ? '*' : '8'); return; + case Key.D9: keyProp.character = (shift ? '(' : '9'); return; + case Key.OemPlus: keyProp.character = (shift ? '+' : '='); return; + case Key.OemMinus: keyProp.character = (shift ? '_' : '-'); return; + case Key.OemQuestion: keyProp.character = (shift ? '?' : '/'); return; + case Key.OemComma: keyProp.character = (shift ? '<' : ','); return; + case Key.OemPeriod: keyProp.character = (shift ? '>' : '.'); return; + case Key.OemOpenBrackets: keyProp.character = (shift ? '{' : '['); return; + case Key.OemQuotes: keyProp.character = (shift ? '"' : '\''); return; + case Key.Oem1: keyProp.character = (shift ? ':' : ';'); return; + case Key.Oem3: keyProp.character = (shift ? '~' : '`'); return; + case Key.Oem5: keyProp.character = (shift ? '|' : '\\'); return; + case Key.Oem6: keyProp.character = (shift ? '}' : ']'); return; + case Key.Tab: keyProp.character = '\t'; return; + case Key.Space: keyProp.character = ' '; return; + + // Number Pad + case Key.NumPad0: keyProp.character = '0'; return; + case Key.NumPad1: keyProp.character = '1'; return; + case Key.NumPad2: keyProp.character = '2'; return; + case Key.NumPad3: keyProp.character = '3'; return; + case Key.NumPad4: keyProp.character = '4'; return; + case Key.NumPad5: keyProp.character = '5'; return; + case Key.NumPad6: keyProp.character = '6'; return; + case Key.NumPad7: keyProp.character = '7'; return; + case Key.NumPad8: keyProp.character = '8'; return; + case Key.NumPad9: keyProp.character = '9'; return; + case Key.Subtract: keyProp.character = '-'; return; + case Key.Add: keyProp.character = '+'; return; + case Key.Decimal: keyProp.character = '.'; return; + case Key.Divide: keyProp.character = '/'; return; + case Key.Multiply: keyProp.character = '*'; return; + + default: + keyProp.type = 1; + keyProp.printable = false; + keyProp.character = '\x00'; + return; + } //switch + } + + + } +} diff --git a/ViewModel/AppConfig.cs b/ViewModel/AppConfig.cs index 008e05b..07e1f56 100644 --- a/ViewModel/AppConfig.cs +++ b/ViewModel/AppConfig.cs @@ -86,14 +86,29 @@ namespace GeekDesk.ViewModel private GradientBGParam gradientBGParam = null; //渐变背景参数 - private bool? enableAppHotKey = true; //可能为null - private bool? enableTodoHotKey = true; //可能为null + private bool? enableAppHotKey = true; //可能为null 开启热键 + private bool? enableTodoHotKey = true; //可能为null 开启待办热键 private bool enableColorPickerHotKey; //新增 默认为false 不需要考虑null值 + private SearchType searchType; + #region GetSet + public SearchType SearchType + { + get + { + return searchType; + } + set + { + searchType = value; + OnPropertyChanged("SearchType"); + } + } + public bool EnableColorPickerHotKey { get