Merge pull request #288 from Leon99/dev

Support for assigning any key as a hotkey; minor refactoring
This commit is contained in:
qianlifeng 2015-06-08 10:21:50 +08:00
commit 124803c89d
5 changed files with 100 additions and 103 deletions

View File

@ -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,39 +59,48 @@ 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))
{ {
List<string> keys = hotkeyString.Replace(" ", "").Split('+').ToList(); return;
if (keys.Contains("Alt")) }
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; CharKey = specialSymbolPair.Value.Key;
keys.Remove("Alt");
} }
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 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;

View File

@ -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();

View File

@ -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);
},
Dispatcher.DelayInvoke("HotkeyAvailableTest", o => SetHotkey(text), TimeSpan.FromMilliseconds(300)); TimeSpan.FromMilliseconds(500));
} }
public void SetHotkey(string keyStr, bool triggerValidate = true) public void SetHotkey(HotkeyModel keyModel, 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);
}
} }
} }

View File

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

View File

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