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 /> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var input = (string)value; // a ctrl+b ctrl+alt+c space var items = input.Split(' '); var rv = new List <Gesture>(); foreach (var item in items) { var pair = item.Split('+'); var modifier = ModifierKeys.None; var key = Key.None; try { modifier = (ModifierKeys)_modifierKeysConverter.ConvertFrom(item); } catch (NotSupportedException e) { if (pair.Length > 1) { var stringModifier = string.Join("+", pair.Take(pair.Length - 1)); modifier = (ModifierKeys)_modifierKeysConverter.ConvertFrom(stringModifier); } key = (Key)_keyConverter.ConvertFrom(pair.Last()); } var gesture = new Gesture(key, modifier); if (gesture.IsValid()) { rv.Add(gesture); } } return(rv.Count > 0 ? new MultiKeyGesture(rv) : null); }
public static void RegisterGlobalHotkeys(this Window window) { var converter = new KeyConverter(); foreach (var definition in WindowManager.Definitions.Where(d => d.InputGestureText != null)) { if (definition.InputGestureText.Contains("+")) { string[] parts = definition.InputGestureText.Split('+'); if (parts.Length == 2) { Type windowViewModelType = definition.ViewModelType; var key = (Key)converter.ConvertFrom(parts[1]); ReactiveCommand <Unit, Unit> openCommand = ReactiveCommand.Create(() => { windowManager.OpenOrFocusWindow(windowViewModelType); }); window.InputBindings.Add(new InputBinding( openCommand, new KeyGesture(key, ModifierKeys.Control))); } else { throw new ArgumentException("InputGestureText not recognized: " + definition.InputGestureText); } } } window.InputBindings.Add(new InputBinding(windowManager.CreateOpenCommand(typeof(OptionsDialogViewModel), openAsDialog: true), new KeyGesture(Key.F4))); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var keyStrokes = (value as string).Split(','); var firstKeyStroke = keyStrokes[0]; var firstKeyStrokeParts = firstKeyStroke.Split('+'); var modifierKeys = (ModifierKeys)_modifierKeysConverter.ConvertFrom(firstKeyStrokeParts[0]); var keys = new List <Key>(); keys.Add((Key)_keyConverter.ConvertFrom(firstKeyStrokeParts[1])); for (var i = 1; i < keyStrokes.Length; ++i) { keys.Add((Key)_keyConverter.ConvertFrom(keyStrokes[i])); } return(new MultiKeyGesture(keys, modifierKeys)); }
/// <inheritdoc /> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source) { if (source is string stringValue) { var fullName = stringValue.Trim(); if (fullName == string.Empty) { return(new KeyGestureEx(Key.None)); } string keyToken; string modifiersToken; string displayString; // break apart display string var index = fullName.IndexOf(DISPLAYSTRING_SEPARATOR); if (index >= 0) { displayString = fullName.Substring(index + 1).Trim(); fullName = fullName.Substring(0, index).Trim(); } else { displayString = string.Empty; } // break apart key and modifiers index = fullName.LastIndexOf(MODIFIERS_DELIMITER); if (index >= 0) { // modifiers exists modifiersToken = fullName.Substring(0, index); keyToken = fullName.Substring(index + 1); } else { modifiersToken = string.Empty; keyToken = fullName; } var modifiers = ModifierKeys.None; var resultkey = keyConverter.ConvertFrom(context, culture, keyToken); if (resultkey != null) { var temp = modifierKeysConverter.ConvertFrom(context, culture, modifiersToken); if (temp != null) { modifiers = (ModifierKeys)temp; } return(new KeyGestureEx((Key)resultkey, modifiers, displayString)); } } throw this.GetConvertFromException(source); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var keyStrokes = (value as string).Split(','); var firstKeyStroke = keyStrokes[0]; var plusParts = firstKeyStroke.Split('+'); var modifierKeys = ModifierKeys.None; plusParts.Take(plusParts.Length - 1).ForEach(x => modifierKeys |= (ModifierKeys)_modifierKeysConverter.ConvertFrom(x)); var keys = new List <Key> { (Key)_keyConverter.ConvertFrom(plusParts.Last()) }; keyStrokes.Skip(1).ForEach(x => keys.Add((Key)_keyConverter.ConvertFrom(x))); return(new MultiKeyGesture(keys, modifierKeys)); }
private static Key?ParseSingleKey(string key) { var converter = new KeyConverter(); try { return((Key?)converter.ConvertFrom(key)); } catch { return(null); } }
private Key ConvertKey(String keyDef) { switch (keyDef) { case "+": return(Key.OemPlus); case "-": return(Key.OemMinus); default: return((Key)_keyConverter.ConvertFrom(keyDef)); } }
private static void GetModifiersAndKey(string keyStroke, out ModifierKeys modifiers, out Key key) { if (string.IsNullOrEmpty(keyStroke)) { modifiers = ModifierKeys.None; key = Key.None; return; } int index = keyStroke.LastIndexOf(ModifiersDelimiter); if (index == -1) { modifiers = ModifierKeys.None; key = (Key)KeyConverter.ConvertFrom(keyStroke); } else { string modifierString = keyStroke.Substring(0, index).Trim(); string keyString = keyStroke.Substring(index + 1).Trim(); modifiers = (ModifierKeys)ModifierKeysConverter.ConvertFrom(modifierString); key = (Key)KeyConverter.ConvertFrom(keyString); } }
/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context"> An <see cref="System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param> /// <param name="culture"> The <see cref="System.Globalization.CultureInfo" /> to use as the current culture. </param> /// <param name="value"> The <see cref="object" /> to convert. </param> /// <returns> An <see cref="object" /> that represents the converted value. </returns> /// <exception cref="System.NotSupportedException">The conversion cannot be performed.</exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string str = (value as string); if (!string.IsNullOrEmpty(str)) { string[] sequences = str.Split(','); List <KeySequence> keySequences = new List <KeySequence>(); foreach (string sequence in sequences) { ModifierKeys modifier = ModifierKeys.None; List <Key> keys = new List <Key>(); string[] keyStrings = sequence.Split('+'); int modifiersCount = 0; string temp; while ((temp = keyStrings[modifiersCount]) != null && TryGetModifierKeys(temp.Trim(), out var currentModifier)) { modifiersCount++; modifier |= currentModifier; } for (int i = modifiersCount; i < keyStrings.Length; i++) { string keyString = keyStrings[i]; if (keyString == null) { continue; } if (KeyConverter.ConvertFrom(keyString.Trim()) is Key key) { keys.Add(key); } } keySequences.Add(new KeySequence(modifier, keys.ToArray())); } return(new MultiKeyGesture(str, keySequences.ToArray())); } throw GetConvertFromException(value); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source) { if ((source != null) && (source is string)) { string str2; string str3; string str4; string str = ((string)source).Trim(); if (str == string.Empty) { return(new UnrestrictedKeyGesture(Key.None)); } int index = str.IndexOf(','); if (index >= 0) { str4 = str.Substring(index + 1).Trim(); str = str.Substring(0, index).Trim(); } else { str4 = string.Empty; } index = str.LastIndexOf('+'); if (index >= 0) { str3 = str.Substring(0, index); str2 = str.Substring(index + 1); } else { str3 = string.Empty; str2 = str; } ModifierKeys none = ModifierKeys.None; object obj3 = keyConverter.ConvertFrom(context, culture, str2); if (obj3 != null) { object obj2 = modifierKeysConverter.ConvertFrom(context, culture, str3); if (obj2 != null) { none = (ModifierKeys)obj2; } return(new UnrestrictedKeyGesture((Key)obj3, none, str4)); } } throw base.GetConvertFromException(source); }
/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context"> An <see cref="System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param> /// <param name="culture"> The <see cref="System.Globalization.CultureInfo" /> to use as the current culture. </param> /// <param name="value"> The <see cref="object" /> to convert. </param> /// <returns> An <see cref="object" /> that represents the converted value. </returns> /// <exception cref="System.NotSupportedException">The conversion cannot be performed.</exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var str = (value as string); if (str != null && !string.IsNullOrEmpty(str)) { var sequences = str.Split(','); string[] keyStrings; var keySequences = new List <KeySequence>(); foreach (var sequence in sequences) { var modifier = ModifierKeys.None; var keys = new List <Key>(); keyStrings = sequence.Split('+'); var modifiersCount = 0; string temp; while ((temp = keyStrings[modifiersCount]) != null && TryGetModifierKeys(temp.Trim(), out var currentModifier)) { modifiersCount++; modifier |= currentModifier; } for (var i = modifiersCount; i < keyStrings.Length; i++) { var keyString = keyStrings[i]; if (keyString != null) { var keyObject = _keyConverter.ConvertFrom(keyString.Trim()) ?? throw new InvalidOperationException("keyObject is null"); var key = (Key)keyObject; keys.Add(key); } } keySequences.Add(new KeySequence(modifier, keys.ToArray())); } return(new MultiKeyGesture(str, keySequences.ToArray())); } throw GetConvertFromException(value); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value != null && value is string source) { if (String.IsNullOrWhiteSpace(source)) { return(KeyCombination.None); } string modifiersString, keyString; // split into modifiers and key int index = source.LastIndexOf(DELIMITER); if (index > 0) { modifiersString = source.Substring(0, index).Trim(); keyString = source.Substring(index + 1).Trim(); } else { modifiersString = String.Empty; keyString = source.Trim(); } var outKey = keyConv.ConvertFrom(context, culture, keyString); if (outKey != null) { if (String.IsNullOrEmpty(modifiersString)) { return(new KeyCombination((Key)outKey)); } else { var outMod = modifiersConv.ConvertFrom(context, culture, modifiersString); if (outMod != null) { return(new KeyCombination((Key)outKey, (ModifierKeys)outMod)); } } } } throw GetConvertFromException(value); }