mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-14 19:49:15 +08:00
Merge pull request #288 from Leon99/dev
Support for assigning any key as a hotkey; minor refactoring
This commit is contained in:
commit
124803c89d
@ -13,6 +13,13 @@ namespace Wox.Infrastructure.Hotkey
|
||||
public bool Ctrl { get; set; }
|
||||
public Key CharKey { get; set; }
|
||||
|
||||
|
||||
Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string>
|
||||
{
|
||||
{Key.Space, "Space"},
|
||||
{Key.Oem3, "~"}
|
||||
};
|
||||
|
||||
public ModifierKeys ModifierKeys
|
||||
{
|
||||
get
|
||||
@ -38,11 +45,9 @@ namespace Wox.Infrastructure.Hotkey
|
||||
}
|
||||
}
|
||||
|
||||
public HotkeyModel() { }
|
||||
|
||||
public HotkeyModel(string hotkeyString)
|
||||
{
|
||||
ParseHotkey(hotkeyString);
|
||||
Parse(hotkeyString);
|
||||
}
|
||||
|
||||
public HotkeyModel(bool alt, bool shift, bool win, bool ctrl, Key key)
|
||||
@ -54,39 +59,48 @@ namespace Wox.Infrastructure.Hotkey
|
||||
CharKey = key;
|
||||
}
|
||||
|
||||
private void ParseHotkey(string hotkeyString)
|
||||
private void Parse(string hotkeyString)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(hotkeyString))
|
||||
if (string.IsNullOrEmpty(hotkeyString))
|
||||
{
|
||||
List<string> keys = hotkeyString.Replace(" ", "").Split('+').ToList();
|
||||
if (keys.Contains("Alt"))
|
||||
return;
|
||||
}
|
||||
List<string> keys = hotkeyString.Replace(" ", "").Split('+').ToList();
|
||||
if (keys.Contains("Alt"))
|
||||
{
|
||||
Alt = true;
|
||||
keys.Remove("Alt");
|
||||
}
|
||||
if (keys.Contains("Shift"))
|
||||
{
|
||||
Shift = true;
|
||||
keys.Remove("Shift");
|
||||
}
|
||||
if (keys.Contains("Win"))
|
||||
{
|
||||
Win = true;
|
||||
keys.Remove("Win");
|
||||
}
|
||||
if (keys.Contains("Ctrl"))
|
||||
{
|
||||
Ctrl = true;
|
||||
keys.Remove("Ctrl");
|
||||
}
|
||||
if (keys.Count > 0)
|
||||
{
|
||||
string charKey = keys[0];
|
||||
KeyValuePair<Key, string>? specialSymbolPair = specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey);
|
||||
if (specialSymbolPair.Value.Value != null)
|
||||
{
|
||||
Alt = true;
|
||||
keys.Remove("Alt");
|
||||
CharKey = specialSymbolPair.Value.Key;
|
||||
}
|
||||
if (keys.Contains("Shift"))
|
||||
else
|
||||
{
|
||||
Shift = true;
|
||||
keys.Remove("Shift");
|
||||
}
|
||||
if (keys.Contains("Win"))
|
||||
{
|
||||
Win = true;
|
||||
keys.Remove("Win");
|
||||
}
|
||||
if (keys.Contains("Ctrl"))
|
||||
{
|
||||
Ctrl = true;
|
||||
keys.Remove("Ctrl");
|
||||
}
|
||||
if (keys.Count > 0)
|
||||
{
|
||||
string charKey = keys[0];
|
||||
try
|
||||
{
|
||||
CharKey = (Key)Enum.Parse(typeof(Key), charKey);
|
||||
CharKey = (Key) Enum.Parse(typeof (Key), charKey);
|
||||
}
|
||||
catch
|
||||
catch (ArgumentException)
|
||||
{
|
||||
|
||||
}
|
||||
@ -99,23 +113,30 @@ namespace Wox.Infrastructure.Hotkey
|
||||
string text = string.Empty;
|
||||
if (Ctrl)
|
||||
{
|
||||
text += "Ctrl";
|
||||
text += "Ctrl + ";
|
||||
}
|
||||
if (Alt)
|
||||
{
|
||||
text += string.IsNullOrEmpty(text) ? "Alt" : " + Alt";
|
||||
text += "Alt + ";
|
||||
}
|
||||
if (Shift)
|
||||
{
|
||||
text += string.IsNullOrEmpty(text) ? "Shift" : " + Shift";
|
||||
text += "Shift + ";
|
||||
}
|
||||
if (Win)
|
||||
{
|
||||
text += string.IsNullOrEmpty(text) ? "Win" : " + Win";
|
||||
text += "Win + ";
|
||||
}
|
||||
if (!string.IsNullOrEmpty(CharKey.ToString()))
|
||||
|
||||
if (CharKey != Key.None)
|
||||
{
|
||||
text += string.IsNullOrEmpty(text) ? CharKey.ToString() : " + " + CharKey;
|
||||
text += specialSymbolDictionary.ContainsKey(CharKey)
|
||||
? specialSymbolDictionary[CharKey].ToString()
|
||||
: CharKey.ToString();
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
text = text.Remove(text.Length - 3);
|
||||
}
|
||||
|
||||
return text;
|
||||
|
@ -44,7 +44,7 @@ namespace Wox
|
||||
ActionKeyword = tbAction.Text
|
||||
};
|
||||
UserSettingStorage.Instance.CustomPluginHotkeys.Add(pluginHotkey);
|
||||
settingWidow.MainWindow.SetHotkey(ctlHotkey.CurrentHotkey.ToString(), delegate
|
||||
settingWidow.MainWindow.SetHotkey(ctlHotkey.CurrentHotkey, delegate
|
||||
{
|
||||
settingWidow.MainWindow.ChangeQuery(pluginHotkey.ActionKeyword);
|
||||
settingWidow.MainWindow.ShowApp();
|
||||
|
@ -1,18 +1,12 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using NHotkey;
|
||||
using NHotkey.Wpf;
|
||||
using Wox.Core.i18n;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
using Wox.Plugin;
|
||||
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
|
||||
using UserControl = System.Windows.Controls.UserControl;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
@ -21,11 +15,11 @@ namespace Wox
|
||||
public HotkeyModel CurrentHotkey { get; private set; }
|
||||
public bool CurrentHotkeyAvailable { get; private set; }
|
||||
|
||||
public event EventHandler OnHotkeyChanged;
|
||||
public event EventHandler HotkeyChanged;
|
||||
|
||||
protected virtual void OnOnHotkeyChanged()
|
||||
protected virtual void OnHotkeyChanged()
|
||||
{
|
||||
EventHandler handler = OnHotkeyChanged;
|
||||
EventHandler handler = HotkeyChanged;
|
||||
if (handler != null) handler(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
@ -34,7 +28,7 @@ namespace Wox
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
|
||||
void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
e.Handled = true;
|
||||
tbMsg.Visibility = Visibility.Hidden;
|
||||
@ -42,60 +36,40 @@ namespace Wox
|
||||
//when alt is pressed, the real key should be e.SystemKey
|
||||
Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
|
||||
|
||||
string text = string.Empty;
|
||||
SpecialKeyState specialKeyState = GlobalHotkey.Instance.CheckModifiers();
|
||||
if (specialKeyState.AltPressed)
|
||||
{
|
||||
text += "Alt";
|
||||
}
|
||||
if (specialKeyState.CtrlPressed)
|
||||
{
|
||||
text += string.IsNullOrEmpty(text) ? "Ctrl" : " + Ctrl";
|
||||
}
|
||||
if (specialKeyState.ShiftPressed)
|
||||
{
|
||||
text += string.IsNullOrEmpty(text) ? "Shift" : " + Shift";
|
||||
}
|
||||
if (specialKeyState.WinPressed)
|
||||
{
|
||||
text += string.IsNullOrEmpty(text) ? "Win" : " + Win";
|
||||
}
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
text += "Ctrl + Alt";
|
||||
}
|
||||
|
||||
if (IsKeyACharOrNumber(key))
|
||||
{
|
||||
text += " + " + key;
|
||||
}
|
||||
else if (key == Key.Space)
|
||||
{
|
||||
text += " + Space";
|
||||
}
|
||||
else
|
||||
var hotkeyModel = new HotkeyModel(
|
||||
specialKeyState.AltPressed,
|
||||
specialKeyState.ShiftPressed,
|
||||
specialKeyState.WinPressed,
|
||||
specialKeyState.CtrlPressed,
|
||||
key);
|
||||
|
||||
var hotkeyString = hotkeyModel.ToString();
|
||||
|
||||
if (hotkeyString == tbHotkey.Text)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (text == tbHotkey.Text)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Dispatcher.DelayInvoke("HotkeyAvailableTest", o => SetHotkey(text), TimeSpan.FromMilliseconds(300));
|
||||
Dispatcher.DelayInvoke("HotkeyAvailabilityTest",
|
||||
o =>
|
||||
{
|
||||
SetHotkey(hotkeyModel);
|
||||
},
|
||||
TimeSpan.FromMilliseconds(500));
|
||||
}
|
||||
|
||||
public void SetHotkey(string keyStr, bool triggerValidate = true)
|
||||
public void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true)
|
||||
{
|
||||
tbMsg.Visibility = Visibility.Visible;
|
||||
tbHotkey.Text = keyStr;
|
||||
CurrentHotkey = keyModel;
|
||||
|
||||
tbHotkey.Text = CurrentHotkey.ToString();
|
||||
tbHotkey.Select(tbHotkey.Text.Length, 0);
|
||||
CurrentHotkey = new HotkeyModel(keyStr);
|
||||
|
||||
if (triggerValidate)
|
||||
{
|
||||
CurrentHotkeyAvailable = CheckHotAvailabel(CurrentHotkey);
|
||||
CurrentHotkeyAvailable = CheckHotkeyAvailability();
|
||||
if (!CurrentHotkeyAvailable)
|
||||
{
|
||||
tbMsg.Foreground = new SolidColorBrush(Colors.Red);
|
||||
@ -106,15 +80,21 @@ namespace Wox
|
||||
tbMsg.Foreground = new SolidColorBrush(Colors.Green);
|
||||
tbMsg.Text = InternationalizationManager.Instance.GetTranslation("succeed");
|
||||
}
|
||||
OnOnHotkeyChanged();
|
||||
tbMsg.Visibility = Visibility.Visible;
|
||||
OnHotkeyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private bool CheckHotAvailabel(HotkeyModel hotkey)
|
||||
public void SetHotkey(string keyStr, bool triggerValidate = true)
|
||||
{
|
||||
SetHotkey(new HotkeyModel(keyStr), triggerValidate);
|
||||
}
|
||||
|
||||
private bool CheckHotkeyAvailability()
|
||||
{
|
||||
try
|
||||
{
|
||||
HotkeyManager.Current.AddOrReplace("HotkeyAvailableTest", hotkey.CharKey, hotkey.ModifierKeys, OnHotkey);
|
||||
HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", CurrentHotkey.CharKey, CurrentHotkey.ModifierKeys, (sender, e) => {});
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -123,20 +103,10 @@ namespace Wox
|
||||
}
|
||||
finally
|
||||
{
|
||||
HotkeyManager.Current.Remove("HotkeyAvailableTest");
|
||||
HotkeyManager.Current.Remove("HotkeyAvailabilityTest");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void OnHotkey(object sender, HotkeyEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private static bool IsKeyACharOrNumber(Key key)
|
||||
{
|
||||
return (key >= Key.A && key <= Key.Z) || (key >= Key.D0 && key <= Key.D9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -331,6 +331,12 @@ namespace Wox
|
||||
public void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
|
||||
{
|
||||
var hotkey = new HotkeyModel(hotkeyStr);
|
||||
SetHotkey(hotkey, action);
|
||||
}
|
||||
|
||||
public void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
||||
{
|
||||
string hotkeyStr = hotkey.ToString();
|
||||
try
|
||||
{
|
||||
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
||||
|
@ -214,7 +214,7 @@ namespace Wox
|
||||
{
|
||||
if (ctlHotkey.CurrentHotkeyAvailable)
|
||||
{
|
||||
MainWindow.SetHotkey(ctlHotkey.CurrentHotkey.ToString(), delegate
|
||||
MainWindow.SetHotkey(ctlHotkey.CurrentHotkey, delegate
|
||||
{
|
||||
if (!MainWindow.IsVisible)
|
||||
{
|
||||
@ -244,7 +244,7 @@ namespace Wox
|
||||
|
||||
private void OnHotkeyTabSelected()
|
||||
{
|
||||
ctlHotkey.OnHotkeyChanged += ctlHotkey_OnHotkeyChanged;
|
||||
ctlHotkey.HotkeyChanged += ctlHotkey_OnHotkeyChanged;
|
||||
ctlHotkey.SetHotkey(UserSettingStorage.Instance.Hotkey, false);
|
||||
lvCustomHotkey.ItemsSource = UserSettingStorage.Instance.CustomPluginHotkeys;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user