private UnrestrictedKeyGesture(Key key, ModifierKeys modifiers, string displayString, bool validateGesture) { if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers)) { throw new InvalidEnumArgumentException("modifiers", (int)modifiers, typeof(ModifierKeys)); } if (!IsDefinedKey(key)) { throw new InvalidEnumArgumentException("key", (int)key, typeof(Key)); } if (displayString == null) { throw new ArgumentNullException("displayString"); } // Do not validate the gesture, can't see any reason why you'd want to limit what keys a gesture // can be composed of. Default behaviour is to require modifier keys for alpha-numeric keys // if (validateGesture && !IsValid(key, modifiers)) // { // throw new NotSupportedException(SR.Get("KeyGesture_Invalid", new object[] { modifiers, key })); // } _modifiers = modifiers; _key = key; _displayString = displayString; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is string) { string bindingString = value as string; if (!string.IsNullOrEmpty(bindingString)) { KeyBinding keybind = new KeyBinding(); string[] values = bindingString.Split('+'); for (int i = 0; i < values.Length; i++) { if (i == values.Length - 1) { KeyConverter k = new KeyConverter(); keybind.Key = (Key)k.ConvertFromString(values[i]); } else { ModifierKeysConverter conv = new ModifierKeysConverter(); keybind.Modifiers = keybind.Modifiers | (ModifierKeys)conv.ConvertFromString(values[i]); } } return(keybind); } return(null); } return(null); }
public static HotKey Str2HotKey(string s) { try { if (string.IsNullOrEmpty(s)) { return(null); } int offset = s.LastIndexOf("+", StringComparison.OrdinalIgnoreCase); if (offset <= 0) { return(null); } string modifierStr = s.Substring(0, offset).Trim(); string keyStr = s.Substring(offset + 1).Trim(); KeyConverter kc = new KeyConverter(); ModifierKeysConverter mkc = new ModifierKeysConverter(); // ReSharper disable once PossibleNullReferenceException Key key = (Key)kc.ConvertFrom(keyStr.ToUpper()); // ReSharper disable once PossibleNullReferenceException ModifierKeys modifier = (ModifierKeys)mkc.ConvertFrom(modifierStr.ToUpper()); return(new HotKey(key, modifier)); } catch (NotSupportedException) { return(null); } catch (NullReferenceException) { return(null); } }
/// <inheritdoc /> /// <summary> /// Determines whether the specified object can be converted into a <see cref="T:System.String" />. /// </summary> /// <param name="value">The object to evaluate for conversion.</param> /// <param name="context">Context information that is used for conversion.</param> /// <returns> /// <see langword="true" /> if the <paramref name="value" /> can be converted into a /// <see cref="T:System.String" />; otherwise, <see langword="false" />. /// </returns> public override bool CanConvertToString(object value, IValueSerializerContext context) { var gesture = value as MultiKeyGesture; return(gesture != null && ModifierKeysConverter.IsDefinedModifierKeys(gesture.Modifiers) && MultiKeyGesture.IsDefinedKey(gesture.Key)); }
private void Load(string path, string basePath) { if (!File.Exists(basePath)) { return; } var xmlBaseReader = new CommonXmlReader(basePath); var baseDic = new KeyConfigDictionary() { { "shortcutnames", xmlBaseReader.GetAttributes("shortcutname", "shortcuts/shortcut") }, { "specialkeies", xmlBaseReader.GetAttributes("specialkey", "shortcuts/shortcut") }, { "mainkeies", xmlBaseReader.GetAttributes("mainkey", "shortcuts/shortcut") }, { "descriptions", xmlBaseReader.GetValues("shortcuts/shortcut") } }; var modConverter = new ModifierKeysConverter(); var keyConverter = new KeyConverter(); for (int i = 0; i < baseDic.MinValueCount; ++i) { ModifierKeys specialKey = (ModifierKeys)modConverter.ConvertFromString(baseDic["specialkeies"][i]); Key mainKey = (Key)keyConverter.ConvertFromString(baseDic["mainkeies"][i]); ShortcutKeies.Add(baseDic["shortcutnames"][i], new ShortcutKey(baseDic["shortcutnames"][i], specialKey, mainKey, baseDic["descriptions"][i].TrimEnd('\n').TrimEnd('\r'))); } if (!File.Exists(path)) { return; } var xmlReader = new CommonXmlReader(path); var dic = new KeyConfigDictionary { { "shortcutnames", xmlReader.GetAttributes("shortcutname", "shortcuts/shortcut") }, { "specialkeies", xmlReader.GetAttributes("specialkey", "shortcuts/shortcut") }, { "mainkeies", xmlReader.GetAttributes("mainkey", "shortcuts/shortcut") } }; for (int i = 0; i < dic.MinValueCount; ++i) { ModifierKeys specialKey; if (dic["specialkeies"][i].Equals("None")) { specialKey = ModifierKeys.None; } else { specialKey = (ModifierKeys)modConverter.ConvertFromString(dic["specialkeies"][i]); } Key mainKey = (Key)keyConverter.ConvertFromString(dic["mainkeies"][i]); string description = ShortcutKeies[dic["shortcutnames"][i]].Description; ShortcutKeies[dic["shortcutnames"][i]] = new ShortcutKey(dic["shortcutnames"][i], specialKey, mainKey, description); } return; }
public virtual void SetKeys(string modifiers, string key) { ModifierKeysConverter mod_ser = new ModifierKeysConverter(); Modifiers_ = (ModifierKeys)mod_ser.ConvertFromString(modifiers); KeyConverter key_ser = new KeyConverter(); Key_ = (Key)key_ser.ConvertFromString(key); }
public static void VerifyIsHotkeyDefined(Hotkey hotkey) { if (!ModifierKeysConverter.IsDefinedModifierKeys(hotkey.Modifiers)) { throw new InvalidEnumArgumentException("hotkey.Modifiers", Convert.ToInt32(hotkey.Modifiers), typeof(ModifierKeys)); } VerifyIsDefined <Key>(hotkey.Key, "Key"); }
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (((destinationType == typeof(string)) && (context != null)) && (context.Instance != null)) { UnrestrictedKeyGesture instance = context.Instance as UnrestrictedKeyGesture; if (instance != null) { return(ModifierKeysConverter.IsDefinedModifierKeys(instance.Modifiers) && IsDefinedKey(instance.Key)); } } return(false); }
private void InitializeHotKeys() { var kc = new SWF.KeysConverter(); var mkc = new ModifierKeysConverter(); _sshHotkey = new HotKey((ModifierKeys)mkc.ConvertFromString(_config.Hotkeys.Select.Modifiers), (SWF.Keys)kc.ConvertFromString(_config.Hotkeys.Select.Key)); _sshHotkey.HotKeyPressed += k => ((MainWindow)this.MainWindow).ShowFullscreenWindow(); Exit += (s, e) => { _sshHotkey.Dispose(); }; }
/// <inheritdoc /> public override bool CanConvertToString(object value, IValueSerializerContext context) { var result = false; var gesture = value as MouseWheelGesture; if (gesture != null) { if (ModifierKeysConverter.IsDefinedModifierKeys(gesture.Modifiers) && gesture.MouseAction == MouseAction.WheelClick && MouseWheelDirectionExt.IsDefined(gesture.Direction)) { result = true; } } return(result); }
public MultiKeyGesture(IEnumerable <Key> keys, ModifierKeys modifiers, string displayString) : base(Key.None, modifiers, GetDisplayString(keys, modifiers, displayString)) { if (keys == null) { throw new ArgumentNullException(nameof(keys)); } if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers)) { throw new InvalidEnumArgumentException(nameof(modifiers), (int)modifiers, typeof(ModifierKeys)); } _keys = new List <Key>(keys); if (_keys.Count == 0) { throw new ArgumentException("A multi key gesture needs to have at least one key."); } }
///<summary> ///TypeConverter method override. ///</summary> ///<param name="context">ITypeDescriptorContext</param> ///<param name="destinationType">Type to convert to</param> ///<returns>true if conversion is possible</returns> public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { // We can convert to an InstanceDescriptor or to a string. if (destinationType == typeof(string)) { // When invoked by the serialization engine we can convert to string only for known type if (context != null && context.Instance != null) { AnyKeyGesture keyGesture = context.Instance as AnyKeyGesture; if (keyGesture != null) { return(ModifierKeysConverter.IsDefinedModifierKeys(keyGesture.Modifiers) && IsDefinedKey(keyGesture.Key)); } } } return(false); }
public static string HotKey2Str(Key key, ModifierKeys modifier) { if (!Enum.IsDefined(typeof(Key), key)) { throw new InvalidEnumArgumentException(nameof(key), (int)key, typeof(Key)); } try { ModifierKeysConverter mkc = new ModifierKeysConverter(); var keyStr = Enum.GetName(typeof(Key), key); var modifierStr = mkc.ConvertToInvariantString(modifier); return($"{modifierStr}+{keyStr}"); } catch (NotSupportedException) { return(null); } }
private ModifierKeys GetModifiers(string[] keyStrings) { ModifierKeys combined = ModifierKeys.None; var converter = new ModifierKeysConverter(); foreach (var keyString in keyStrings) { try { var key = (ModifierKeys)converter.ConvertFromString(keyString); combined = combined | key; } catch (Exception /*e*/) { // The key was not a modifier key, just skip it. } } return(combined); }
/// <summary> /// Private constructor that does the real work. /// </summary> /// <param name="key">Key</param> /// <param name="modifiers">Modifiers</param> /// <param name="displayString">display string</param> /// <param name="validateGesture">If true, throws an exception if the key and modifier are not valid</param> private AnyKeyGesture(Key key, ModifierKeys modifiers, string displayString, bool validateGesture) { if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers)) { throw new InvalidEnumArgumentException("modifiers", (int)modifiers, typeof(ModifierKeys)); } if (!IsDefinedKey(key)) { throw new InvalidEnumArgumentException("key", (int)key, typeof(Key)); } if (validateGesture && !IsValid(key, modifiers)) { throw new NotSupportedException($"Unsupported key gesture \"{this}\"!"); } _modifiers = modifiers; _key = key; _displayString = displayString ?? string.Empty; }
public override string ToString() { string modifiers = null; if (ModifierKeys != ModifierKeys.None) { var converter = new ModifierKeysConverter(); modifiers = converter.ConvertToString(ModifierKeys); } string key = null; if (Key != Key.None) { var keyConverter = new KeyConverter(); key = keyConverter.ConvertToString(Key); } return(string.IsNullOrEmpty(key) ? string.Empty : (string.IsNullOrEmpty(modifiers) ? key : string.Format("{0}+{1}", modifiers, key))); }
public static string GetCultureModifiersName(ModifierKeys modifiers, CultureInfo culture = null) { if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers)) { throw new InvalidEnumArgumentException(nameof(modifiers), (int)modifiers, typeof(ModifierKeys)); } var str = string.Empty; if ((modifiers & ModifierKeys.Control) == ModifierKeys.Control) { str += GetSingleCultureModifierName(ModifierKeys.Control, culture); } if ((modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { if (str.Length > 0) { str += ModifierDelimiter; } str += GetSingleCultureModifierName(ModifierKeys.Shift, culture); } if ((modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) { if (str.Length > 0) { str += ModifierDelimiter; } str += GetSingleCultureModifierName(ModifierKeys.Alt, culture); } if ((modifiers & ModifierKeys.Windows) == ModifierKeys.Windows) { if (str.Length > 0) { str += ModifierDelimiter; } str += GetSingleCultureModifierName(ModifierKeys.Windows, culture); } return(str); }
public override bool CanConvertToString(object value, IValueSerializerContext context) { UnrestrictedKeyGesture gesture = value as UnrestrictedKeyGesture; return(((gesture != null) && ModifierKeysConverter.IsDefinedModifierKeys(gesture.Modifiers)) && UnrestrictedKeyGestureConverter.IsDefinedKey(gesture.Key)); }
public static bool IsHotkeyDefined(Hotkey hotkey) { return (ModifierKeysConverter.IsDefinedModifierKeys(hotkey.Modifiers) && IsDefined <Key>(hotkey.Key)); }
public HotkeyConverter() { _modifierKeyConverter = new ModifierKeysConverter(); _keyConverter = new KeyConverter(); }
private void InitlializeShortucts() { // shortcuts dictionary _shortcuts = new Dictionary <Tuple <Key, ModifierKeys>, Action>(); // key converters var keyConverter = new KeyConverter(); var modifiersKeyConverter = new ModifierKeysConverter(); // open shortcut _shortcuts.Add( new Tuple <Key, ModifierKeys>( (Key)keyConverter.ConvertFromString("O"), (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")), () => Open()); // save shortcut _shortcuts.Add( new Tuple <Key, ModifierKeys>( (Key)keyConverter.ConvertFromString("S"), (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")), () => Save()); // export shortcut _shortcuts.Add( new Tuple <Key, ModifierKeys>( (Key)keyConverter.ConvertFromString("E"), (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")), () => Export()); // undo shortcut _shortcuts.Add( new Tuple <Key, ModifierKeys>( (Key)keyConverter.ConvertFromString("Z"), (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")), () => _view.Undo()); // redo shortcut _shortcuts.Add( new Tuple <Key, ModifierKeys>( (Key)keyConverter.ConvertFromString("Y"), (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")), () => _view.Redo()); // snap shortcut _shortcuts.Add( new Tuple <Key, ModifierKeys>( (Key)keyConverter.ConvertFromString("S"), ModifierKeys.None), () => _view.ToggleSnap()); // clear shortcut _shortcuts.Add( new Tuple <Key, ModifierKeys>( (Key)keyConverter.ConvertFromString("Delete"), (ModifierKeys)modifiersKeyConverter.ConvertFromString("Control")), () => _view.Clear()); // editor shortcuts foreach (var editor in _view.Editors) { var _editor = editor; _shortcuts.Add( new Tuple <Key, ModifierKeys>( (Key)keyConverter.ConvertFromString(editor.Key), editor.Modifiers == "" ? ModifierKeys.None : (ModifierKeys)modifiersKeyConverter.ConvertFromString(editor.Modifiers)), () => _view.Enable(_editor)); } // block shortcut _shortcuts.Add( new Tuple <Key, ModifierKeys>( (Key)keyConverter.ConvertFromString("G"), ModifierKeys.None), () => _view.CreateBlock()); // delete shortcut _shortcuts.Add( new Tuple <Key, ModifierKeys>( (Key)keyConverter.ConvertFromString("Delete"), ModifierKeys.None), () => _view.Delete()); }
// 热键注册/响应 private void RegisterHotKey() { var kc = new KeyConverter(); var mkc = new ModifierKeysConverter(); try { if (Settings.Default.hotKeyModQ != "" && Settings.Default.hotKeyQ != "") { ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModQ); Key key = (Key)kc.ConvertFromString(Settings.Default.hotKeyQ); HotKeyManage.Register(key, mod); } else { HotKeyManage.Register(Key.Q, ModifierKeys.Control); Settings.Default.hotKeyModQ = mkc.ConvertToString(ModifierKeys.Control); Settings.Default.hotKeyQ = kc.ConvertToString(Key.Q); } Mvvm.HotKeyQ = HotKeyManage.ToString(); } catch (Exception err) { MessageBox.Show(err.ToString()); Mvvm.HotKeyQ = HotKeyManage.ToString() + "(冲突)"; } ////////////////////////////////////////////////// try { if (Settings.Default.hotKeyModW != "" && Settings.Default.hotKeyW != "") { ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModW); Key key = (Key)kc.ConvertFromString(Settings.Default.hotKeyW); HotKeyManage.Register(key, mod); } else { HotKeyManage.Register(Key.W, ModifierKeys.Control); Settings.Default.hotKeyModW = mkc.ConvertToString(ModifierKeys.Control); Settings.Default.hotKeyW = kc.ConvertToString(Key.W); } Mvvm.HotKeyW = HotKeyManage.ToString(); } catch (Exception err) { MessageBox.Show(err.ToString()); Mvvm.HotKeyW = HotKeyManage.ToString() + "(冲突)"; } ////////////////////////////////////////////////// try { if (Settings.Default.hotKeyModB != "" && Settings.Default.hotKeyB != "") { ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModB); Key key = (Key)kc.ConvertFromString(Settings.Default.hotKeyB); HotKeyManage.Register(key, mod); } else { HotKeyManage.Register(Key.B, ModifierKeys.Control); Settings.Default.hotKeyModB = mkc.ConvertToString(ModifierKeys.Control); Settings.Default.hotKeyB = kc.ConvertToString(Key.B); } Mvvm.HotKeyB = HotKeyManage.ToString(); } catch (Exception err) { MessageBox.Show(err.ToString()); Mvvm.HotKeyB = HotKeyManage.ToString() + "(冲突)"; } ////////////////////////////////////////////////// try { if (Settings.Default.hotKeyModG != "" && Settings.Default.hotKeyG != "") { ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModG); Key key = (Key)kc.ConvertFromString(Settings.Default.hotKeyG); HotKeyManage.Register(key, mod); } else { HotKeyManage.Register(Key.G, ModifierKeys.Control); Settings.Default.hotKeyModG = mkc.ConvertToString(ModifierKeys.Control); Settings.Default.hotKeyG = kc.ConvertToString(Key.G); } Mvvm.HotKeyG = HotKeyManage.ToString(); } catch (Exception err) { MessageBox.Show(err.ToString()); Mvvm.HotKeyG = HotKeyManage.ToString() + "(冲突)"; } Settings.Default.Save(); }
protected virtual string ConvertTo(ModifierKeys value, Type type = null) { ModifierKeysConverter c = new ModifierKeysConverter(); return((string)c.ConvertTo(value, type ?? typeof(string))); }
public ComplexInputGestureConverter() { _keyConverter = new KeyConverter(); _modifierKeysConverter = new ModifierKeysConverter(); }
public MultiKeyGestureConverter() { _keyConverter = new KeyConverter(); _modifierKeysConverter = new ModifierKeysConverter(); }
protected virtual ModifierKeys?ConvertStringToModifierKeys(string value) { return(ModifierKeysConverter.ConvertStringToModifierKeys(value, null)); }
private static void textBox_PreviewKeyDown(object sender, KeyEventArgs e) { var textBox = sender as TextBox; if (!_ignoredKey.Contains(e.Key) && (e.Key != Key.System || (e.Key == Key.System && !_ignoredKey.Contains(e.SystemKey)))) { var keys = (e.Key == Key.System && !_ignoredKey.Contains(e.SystemKey)) ? e.SystemKey : e.Key; var hotKey = new HotKey() { Ctrl = ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control), Alt = ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt), Shift = ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift), Key = keys }; if (hotKey.Ctrl || hotKey.Alt || hotKey.Shift == true) { var kc = new KeyConverter(); var mkc = new ModifierKeysConverter(); if (textBox.Name == "HotKeyQ") { if (e.Key == Key.C) { return; } Key key = (Key)kc.ConvertFromString(Settings.Default.hotKeyQ); ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModQ); QTranse.HotKeyManage.Unregister(key, mod); QTranse.HotKeyManage.Register(e.Key, Keyboard.Modifiers); Settings.Default.hotKeyQ = kc.ConvertToString(e.Key); Settings.Default.hotKeyModQ = mkc.ConvertToString(Keyboard.Modifiers); Settings.Default.Save(); QTranse.Mvvm.HotKeyQ = string.Format($"{hotKey}"); } if (textBox.Name == "HotKeyW") { if (e.Key == Key.C) { return; } Key key = (Key)kc.ConvertFromString(Settings.Default.hotKeyW); ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModW); QTranse.HotKeyManage.Unregister(key, mod); QTranse.HotKeyManage.Register(e.Key, Keyboard.Modifiers); Settings.Default.hotKeyW = kc.ConvertToString(e.Key); Settings.Default.hotKeyModW = mkc.ConvertToString(Keyboard.Modifiers); Settings.Default.Save(); QTranse.Mvvm.HotKeyW = string.Format($"{hotKey}"); } if (textBox.Name == "HotKeyB") { if (e.Key == Key.C) { return; } Key key = (Key)kc.ConvertFromString(Settings.Default.hotKeyB); ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModB); QTranse.HotKeyManage.Unregister(key, mod); QTranse.HotKeyManage.Register(e.Key, Keyboard.Modifiers); Settings.Default.hotKeyB = kc.ConvertToString(e.Key); Settings.Default.hotKeyModB = mkc.ConvertToString(Keyboard.Modifiers); Settings.Default.Save(); QTranse.Mvvm.HotKeyB = string.Format($"{hotKey}"); } if (textBox.Name == "HotKeyG") { if (e.Key == Key.C) { return; } Key key = (Key)kc.ConvertFromString(Settings.Default.hotKeyG); ModifierKeys mod = (ModifierKeys)mkc.ConvertFromString(Settings.Default.hotKeyModG); QTranse.HotKeyManage.Unregister(key, mod); QTranse.HotKeyManage.Register(e.Key, Keyboard.Modifiers); Settings.Default.hotKeyG = kc.ConvertToString(e.Key); Settings.Default.hotKeyModG = mkc.ConvertToString(Keyboard.Modifiers); Settings.Default.Save(); QTranse.Mvvm.HotKeyG = string.Format($"{hotKey}"); } } } e.Handled = true; }
protected virtual ModifierKeys ConvertFrom(string value) { ModifierKeysConverter c = new ModifierKeysConverter(); return((ModifierKeys)c.ConvertFrom(value)); }