2014-02-22 15:52:20 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
2014-12-21 22:03:03 +08:00
|
|
|
|
namespace Wox.Infrastructure.Hotkey
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
|
|
|
|
public class HotkeyModel
|
|
|
|
|
{
|
|
|
|
|
public bool Alt { get; set; }
|
|
|
|
|
public bool Shift { get; set; }
|
|
|
|
|
public bool Win { get; set; }
|
|
|
|
|
public bool Ctrl { get; set; }
|
|
|
|
|
public Key CharKey { get; set; }
|
|
|
|
|
|
2015-06-07 11:45:06 +08:00
|
|
|
|
|
|
|
|
|
Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string>
|
|
|
|
|
{
|
|
|
|
|
{Key.Space, "Space"},
|
|
|
|
|
{Key.Oem3, "~"}
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-22 15:52:20 +08:00
|
|
|
|
public ModifierKeys ModifierKeys
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
ModifierKeys modifierKeys = ModifierKeys.None;
|
|
|
|
|
if (Alt)
|
|
|
|
|
{
|
|
|
|
|
modifierKeys = ModifierKeys.Alt;
|
|
|
|
|
}
|
|
|
|
|
if (Shift)
|
|
|
|
|
{
|
|
|
|
|
modifierKeys = modifierKeys | ModifierKeys.Shift;
|
|
|
|
|
}
|
|
|
|
|
if (Win)
|
|
|
|
|
{
|
|
|
|
|
modifierKeys = modifierKeys | ModifierKeys.Windows;
|
|
|
|
|
}
|
|
|
|
|
if (Ctrl)
|
|
|
|
|
{
|
|
|
|
|
modifierKeys = modifierKeys | ModifierKeys.Control;
|
|
|
|
|
}
|
|
|
|
|
return modifierKeys;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HotkeyModel(string hotkeyString)
|
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
Parse(hotkeyString);
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HotkeyModel(bool alt, bool shift, bool win, bool ctrl, Key key)
|
|
|
|
|
{
|
|
|
|
|
Alt = alt;
|
|
|
|
|
Shift = shift;
|
|
|
|
|
Win = win;
|
|
|
|
|
Ctrl = ctrl;
|
|
|
|
|
CharKey = key;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-07 11:45:06 +08:00
|
|
|
|
private void Parse(string hotkeyString)
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
if (string.IsNullOrEmpty(hotkeyString))
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
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)
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
CharKey = specialSymbolPair.Value.Key;
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
2015-06-07 11:45:06 +08:00
|
|
|
|
else
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
CharKey = (Key) Enum.Parse(typeof (Key), charKey);
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
2015-06-07 11:45:06 +08:00
|
|
|
|
catch (ArgumentException)
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
string text = string.Empty;
|
|
|
|
|
if (Ctrl)
|
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
text += "Ctrl + ";
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
if (Alt)
|
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
text += "Alt + ";
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
if (Shift)
|
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
text += "Shift + ";
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
if (Win)
|
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
text += "Win + ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CharKey != Key.None)
|
|
|
|
|
{
|
|
|
|
|
text += specialSymbolDictionary.ContainsKey(CharKey)
|
2016-01-07 05:34:42 +08:00
|
|
|
|
? specialSymbolDictionary[CharKey]
|
2015-06-07 11:45:06 +08:00
|
|
|
|
: CharKey.ToString();
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
2015-06-07 11:45:06 +08:00
|
|
|
|
else if (!string.IsNullOrEmpty(text))
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
text = text.Remove(text.Length - 3);
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|