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