PowerToys/Wox.Infrastructure/Hotkey/HotkeyModel.cs

146 lines
3.8 KiB
C#
Raw Normal View History

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; }
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)
{
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;
}
private void Parse(string hotkeyString)
2014-02-22 15:52:20 +08:00
{
if (string.IsNullOrEmpty(hotkeyString))
2014-02-22 15:52:20 +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
{
CharKey = specialSymbolPair.Value.Key;
2014-02-22 15:52:20 +08:00
}
else
2014-02-22 15:52:20 +08:00
{
try
{
CharKey = (Key) Enum.Parse(typeof (Key), charKey);
2014-02-22 15:52:20 +08:00
}
catch (ArgumentException)
2014-02-22 15:52:20 +08:00
{
}
}
}
}
public override string ToString()
{
string text = string.Empty;
if (Ctrl)
{
text += "Ctrl + ";
2014-02-22 15:52:20 +08:00
}
if (Alt)
{
text += "Alt + ";
2014-02-22 15:52:20 +08:00
}
if (Shift)
{
text += "Shift + ";
2014-02-22 15:52:20 +08:00
}
if (Win)
{
text += "Win + ";
}
if (CharKey != Key.None)
{
text += specialSymbolDictionary.ContainsKey(CharKey)
2016-01-07 05:34:42 +08:00
? specialSymbolDictionary[CharKey]
: CharKey.ToString();
2014-02-22 15:52:20 +08:00
}
else if (!string.IsNullOrEmpty(text))
2014-02-22 15:52:20 +08:00
{
text = text.Remove(text.Length - 3);
2014-02-22 15:52:20 +08:00
}
return text;
}
}
}