public static string ToShortcutString(this WpfKey key, WpfModifier modifier, string separator = "+") { var sb = new StringBuilder(); if (modifier.HasFlag(WpfModifier.Control)) { AppendSeparator(sb, separator, "Ctrl"); } if (modifier.HasFlag(WpfModifier.Shift)) { AppendSeparator(sb, separator, "Shift"); } if (modifier.HasFlag(WpfModifier.Alt)) { AppendSeparator(sb, separator, "Alt"); } string val; if (keymap.TryGetValue(key, out val)) { AppendSeparator(sb, separator, val); } else { AppendSeparator(sb, separator, key.ToString()); } return(sb.ToString()); }
public static Key Convert(swi.Key key, swi.ModifierKeys modifier) { var keys = key.ToString() .Split(new[] { ", " }, StringSplitOptions.None) .Select(v => (swi.Key)Enum.Parse(typeof(swi.Key), v)); Key ret = Key.None; foreach (var val in keys) { ret |= Find(val); } if (modifier.HasFlag(swi.ModifierKeys.Alt)) { ret |= Key.Alt; } if (modifier.HasFlag(swi.ModifierKeys.Control)) { ret |= Key.Control; } if (modifier.HasFlag(swi.ModifierKeys.Shift)) { ret |= Key.Shift; } if (modifier.HasFlag(swi.ModifierKeys.Windows)) { ret |= Key.Application; } return(ret); }
private static System.Windows.Forms.Keys ConvertInputKeyToFormsKeys(System.Windows.Input.Key key) { if (Enum.GetNames(typeof(System.Windows.Forms.Keys)).Contains(key.ToString())) { return((System.Windows.Forms.Keys)Enum.Parse(typeof(System.Windows.Forms.Keys), key.ToString())); } else { return(Keys.None); } }
/// <summary> /// Get Unicode Key Type from its Windows Input Key Enum /// </summary> /// <remarks> /// Taken from: /// > http://stackoverflow.com/questions/5825820/how-to-capture-the-character-on-different-locale-keyboards-in-wpf-c /// > http://web.archive.org/web/20111229040043/http://huddledmasses.org/how-to-get-the-character-and-virtualkey-from-a-wpf-keydown-event /// > Dependencies: /// o using System.Windows.Input; /// o Reference: WindowsBase (user32.lib) /// </remarks> /// <param name="inputKey"></param> /// <returns></returns> private static string GetUnicodeValueFromWindowsInputKeyEnum(System.Windows.Input.Key inputKey) { // Initialise .. string keyUnicodeValue = "\0"; StringBuilder stringBuilder = new StringBuilder(2); uint scanCode = 0; int result = 0; // Get keyboard state .. (detect any modifier keys, caps lock, depressed key, etc) byte[] keyboardState = new byte[256]; GetKeyboardState(keyboardState); // Find its virtual key code .. int virtualKey = KeyInterop.VirtualKeyFromKey(inputKey); // Translate virtual key code to scan code .. scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC_EX); // Translate virtual key code coupled with keyboard state to corresponding unicode character(s) .. result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0); // Handle non-printable keys .. if (stringBuilder.Length > 0) { keyUnicodeValue = stringBuilder[0].ToString(); if (keyUnicodeValue.Trim().Length == 0) { keyUnicodeValue = inputKey.ToString(); } } // Handle return (ToUnicode result can be: -1, 0, 1) .. if (result == 1) { return(keyUnicodeValue); } else { return(inputKey.ToString()); } }
//キーが押されるまで待つ処理 private void KeySetTimerTick(object sender, EventArgs e) { if (cancelClicked > -1) { baseData.Timer.Tick -= KeySetTimerTick; locLabelDic["keyBackLabel" + configIndex.ToString()].Background = Brushes.Transparent; configIndex = -1; locLabelDic["message"].Content = messageDic["cancel"]; return; } if (configKey != null) { if (BaseData.judgingButton.ContainsValue((SysKey)configKey)) { locLabelDic["message"].Content = messageDic["usedKey"]; return; } SysKey key = (SysKey)configKey; if (baseData.KeyToNumDic.ContainsKey(key)) { int oldIndex = baseData.KeyToNumDic[key]; locLabelDic["keyTextLabel" + oldIndex.ToString()].Content = ""; baseData.NumToKeyDic.Remove(oldIndex); baseData.KeyToNumDic.Remove(key); if (baseData.NumToJoyDic.ContainsKey(oldIndex)) { Joystick joystick = baseData.NumToJoyDic[oldIndex]; joystick.JoystickKey = null; } } if (baseData.NumToKeyDic.ContainsKey(configIndex)) { SysKey oldKey = baseData.NumToKeyDic[configIndex]; baseData.NumToKeyDic.Remove(configIndex); baseData.KeyToNumDic.Remove(oldKey); } baseData.KeyToNumDic.Add(key, configIndex); baseData.NumToKeyDic.Add(configIndex, key); locLabelDic["keyTextLabel" + configIndex.ToString()].Content = key.ToString(); if (baseData.NumToJoyDic.ContainsKey(configIndex)) { Joystick joystick = baseData.NumToJoyDic[configIndex]; joystick.JoystickKey = key; } baseData.Timer.Tick -= KeySetTimerTick; locLabelDic["keyBackLabel" + configIndex.ToString()].Background = Brushes.Transparent; configIndex = -1; locLabelDic["message"].Content = messageDic["ok"]; } }
/// <summary>Combines the modifier and key to a shortcut. /// Changes Control;Shift;Alt;T to Control + Shift + Alt + T /// </summary> /// <param name="mod">The modifier.</param> /// <param name="key">The key.</param> /// <returns>A string representation of the modifier and key.</returns> public static string CombineShortcut(ModifierKeys mod, System.Windows.Input.Key key) { string hotkey = ""; foreach (ModifierKeys a in new HotKeyShared.ParseModifier((int)mod)) { hotkey += a.ToString() + " + "; } if (hotkey.Contains(ModifierKeys.None.ToString())) { hotkey = ""; } hotkey += key.ToString(); return(hotkey); }
public static string GetHotKeyString(uint hotkeyModifiers, System.Windows.Input.Key key) { var hotKeyString = ""; if ((hotkeyModifiers & (uint)GlobalHotkeyHooker.HotkeyModifiers.Shift) > 0) { hotKeyString += GlobalHotkeyHooker.HotkeyModifiers.Shift.ToString() + " + "; } if ((hotkeyModifiers & (uint)GlobalHotkeyHooker.HotkeyModifiers.Ctrl) > 0) { hotKeyString += GlobalHotkeyHooker.HotkeyModifiers.Ctrl.ToString() + " + "; } if ((hotkeyModifiers & (uint)GlobalHotkeyHooker.HotkeyModifiers.Alt) > 0) { hotKeyString += GlobalHotkeyHooker.HotkeyModifiers.Alt.ToString() + " + "; } if ((hotkeyModifiers & (uint)GlobalHotkeyHooker.HotkeyModifiers.Win) > 0) { hotKeyString += GlobalHotkeyHooker.HotkeyModifiers.Win.ToString() + " + "; } if ( key == Key.LeftCtrl || key == Key.RightCtrl || key == Key.LeftAlt || key == Key.RightAlt || key == Key.LeftShift || key == Key.RightShift || key == Key.LWin || key == Key.RWin || key == Key.Clear || key == Key.OemClear || key == Key.Escape || key == Key.Apps) { } else { hotKeyString += key.ToString(); } return(hotKeyString); }
public bool IsThisKeyPressed(System.Windows.Input.Key key) { return(keyName_.Equals(key.ToString())); }
/// <summary> /// Returns friendly name of given WPF <see cref="wpf.Key"/>. /// </summary> /// <param name="key">Forms <see cref="forms.Keys"/>.</param> public static string GetFriendlyName(wpf.Key key) { if (!Enum.IsDefined(typeof(wpf.Key), key)) { return("None"); } if (key == wpf.Key.None) { return("None"); } int value = (int)key; if (34 < value && value < 44) { return((value - 34).ToString()); } switch (value) { case 34: return("0"); case 89: return("NumPadSlash"); case 84: return("NumPadStar"); case 87: return("NumPadMinus"); case 85: return("NumPadPlus"); case 88: return("NumPadPeriod"); case 146: return("~"); case 143: return("-"); case 141: return("+"); case 2: return("Backspace"); case 6: return("Enter"); case 149: return("["); case 151: return("]"); case 8: return("CapsLock"); case 140: return(";"); case 152: return("\""); case 150: return(@"\"); case 142: return(","); case 144: return("."); case 145: return("?"); case 23: return("LeftArrow"); case 24: return("UpArrow"); case 25: return("RightArrow"); case 26: return("DownArrow"); } return(key.ToString()); }