🚸 优化快捷键/优化拾色器

This commit is contained in:
liufei
2022-05-25 09:50:43 +08:00
parent a0ba6d4ecd
commit 39607bdcb5
6 changed files with 333 additions and 125 deletions

View File

@@ -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">
<UserControl.Resources>
<cvt:HideTypeConvert x:Key="HideTypeConvert"/>
<cvt:SearchTypeConvert x:Key="SearchTypeConvert"/>
</UserControl.Resources>
<Grid Background="Transparent" MouseDown="DragMove"
>
@@ -108,6 +109,18 @@
IsChecked="{Binding AppHideType, Mode=TwoWay, Converter={StaticResource HideTypeConvert}, ConverterParameter=3}"/>
</hc:UniformSpacingPanel>
<hc:UniformSpacingPanel Spacing="10" Margin="0,10,0,0" Grid.ColumnSpan="4">
<TextBlock Text="搜索方式" VerticalAlignment="Center"/>
</hc:UniformSpacingPanel>
<hc:UniformSpacingPanel Spacing="10" Margin="10,5,0,0" Grid.ColumnSpan="4">
<RadioButton Margin="10,0,0,0" Background="{DynamicResource SecondaryRegionBrush}"
Style="{StaticResource RadioButtonIcon}" Content="快捷键"
IsChecked="{Binding SearchType, Mode=TwoWay, Converter={StaticResource SearchTypeConvert}, ConverterParameter=0}"/>
<RadioButton Margin="10,0,0,0" Background="{DynamicResource SecondaryRegionBrush}"
Style="{StaticResource RadioButtonIcon}" Content="按键即搜"
IsChecked="{Binding SearchType, Mode=TwoWay, Converter={StaticResource SearchTypeConvert}, ConverterParameter=1}"/>
</hc:UniformSpacingPanel>
<hc:UniformSpacingPanel Spacing="10" Margin="0,10,0,0" Grid.ColumnSpan="4">
<TextBlock Text="热键设置" VerticalAlignment="Center"/>
</hc:UniformSpacingPanel>
@@ -122,6 +135,7 @@
Text="{Binding HotkeyStr}"
KeyDown="HotKeyDown"
KeyUp="HotKeyUp"
InputMethod.IsInputMethodEnabled="False"
/>
<CheckBox Content="启用"
Click="EnableHotKey_Click"
@@ -145,6 +159,7 @@
Text="{Binding ToDoHotkeyStr}"
KeyDown="HotKeyDown"
KeyUp="HotKeyUp"
InputMethod.IsInputMethodEnabled="False"
/>
<CheckBox Content="启用"
Click="EnableHotKey_Click"
@@ -169,6 +184,7 @@
Text="{Binding ColorPickerHotkeyStr}"
KeyDown="HotKeyDown"
KeyUp="HotKeyUp"
InputMethod.IsInputMethodEnabled="False"
/>
<CheckBox Content="启用"
Tag="{x:Static cst:HotKeyType.ColorPicker}"

View File

@@ -2,6 +2,7 @@
using GeekDesk.MyThread;
using GeekDesk.Util;
using GeekDesk.ViewModel;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Windows;
@@ -33,6 +34,10 @@ namespace GeekDesk.Control.UserControls.Config
/// <param name="sender"></param>
/// <param name="e"></param>
private void HotKeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("downKey:" + e.Key.ToString());
lock (this)
{
HotKeyType hkType = (HotKeyType)(sender as TextBox).Tag;
@@ -45,31 +50,33 @@ namespace GeekDesk.Control.UserControls.Config
if (!CheckIsEnable(hkType)) return;
Console.WriteLine("prevKeyTemp:" + prevKeyTemp.ToString());
if (prevKeyTemp == Key.None || prevKeyTemp != downKey)
{
if (hotkeyFinished)
{
switch (hkType)
{
case HotKeyType.Main:
appConfig.Hotkey = 0;
appConfig.Hotkey = Key.None;
appConfig.HotkeyStr = "";
appConfig.HotkeyModifiers = 0;
appConfig.HotkeyModifiers = GlobalHotKey.HotkeyModifiers.None;
break;
case HotKeyType.ToDo:
appConfig.ToDoHotkey = 0;
appConfig.ToDoHotkey = Key.None;
appConfig.ToDoHotkeyStr = "";
appConfig.ToDoHotkeyModifiers = 0;
appConfig.ToDoHotkeyModifiers = GlobalHotKey.HotkeyModifiers.None;
break;
case HotKeyType.ColorPicker:
appConfig.ColorPickerHotkey = 0;
appConfig.ColorPickerHotkey = Key.None;
appConfig.ColorPickerHotkeyStr = "";
appConfig.ColorPickerHotkeyModifiers = 0;
appConfig.ColorPickerHotkeyModifiers = GlobalHotKey.HotkeyModifiers.None;
break;
}
hotkeyFinished = false;
}
//首次按下按键
if ((HotKeyType.Main == hkType && (appConfig.HotkeyStr == null || appConfig.HotkeyStr.Length == 0))
|| (HotKeyType.ToDo == hkType && (appConfig.ToDoHotkeyStr == null || appConfig.ToDoHotkeyStr.Length == 0))
@@ -94,7 +101,7 @@ namespace GeekDesk.Control.UserControls.Config
appConfig.ColorPickerHotkeyModifiers = GetModifierKeys(downKey);
break;
}
Console.WriteLine("进入设置" + downKey.ToString());
prevKeyTemp = downKey;
keysTemp.Add(e);
}
@@ -105,24 +112,29 @@ namespace GeekDesk.Control.UserControls.Config
if (CheckModifierKeys(prevKeyTemp)
&& ((downKey >= Key.A && downKey <= Key.Z)
|| (downKey >= Key.F1 && downKey <= Key.F12)
|| (downKey >= Key.D0 && downKey <= Key.D9)))
|| (downKey >= Key.D0 && downKey <= Key.D9)
|| downKey == Key.Oem3
))
{
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 += downKey.ToString();
appConfig.HotkeyStr += downKeyStr;
break;
case HotKeyType.ToDo:
appConfig.ToDoHotkey = downKey;
appConfig.ToDoHotkeyStr += downKey.ToString();
appConfig.ToDoHotkeyStr += downKeyStr;
break;
case HotKeyType.ColorPicker:
appConfig.ColorPickerHotkey = downKey;
appConfig.ColorPickerHotkeyStr += downKey.ToString();
appConfig.ColorPickerHotkeyStr += downKeyStr;
break;
}
prevKeyTemp = downKey;
keysTemp.Add(e);
}
@@ -150,6 +162,7 @@ namespace GeekDesk.Control.UserControls.Config
}
}
}
}
private string GetKeyName(Key key)
{
@@ -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);
} 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;
}
}

View File

@@ -81,6 +81,7 @@ namespace GeekDesk.Control.Windows
if (window == null || !window.Activate())
{
window = new GlobalColorPickerWindow();
window.Show();
}
window.Hide();
GlobalColorPickerWindow thisWindow = (GlobalColorPickerWindow)window;

View File

@@ -10,6 +10,7 @@ namespace GeekDesk.Util
{
public enum HotkeyModifiers
{
None = 0,
MOD_ALT = 0x1,
MOD_CONTROL = 0x2,
MOD_SHIFT = 0x4,

137
Util/KeyUtil.cs Normal file
View File

@@ -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
}
}
}

View File

@@ -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