public static string ToGestureString(this ConsoleKeyInfo key) { var mods = key.Modifiers; var sb = new StringBuilder(); if (key.Modifiers.HasFlag(ConsoleModifiers.Control)) { sb.Append("Ctrl"); } if (key.Modifiers.HasFlag(ConsoleModifiers.Alt)) { if (sb.Length > 0) { sb.Append("+"); } sb.Append("Alt"); } char c = ConsoleKeyChordConverter.GetCharFromConsoleKey(key.Key, (mods & ConsoleModifiers.Shift) != 0 ? ConsoleModifiers.Shift : 0); if (char.IsControl(c) || char.IsWhiteSpace(c)) { if (key.Modifiers.HasFlag(ConsoleModifiers.Shift)) { if (sb.Length > 0) { sb.Append("+"); } sb.Append("Shift"); } if (sb.Length > 0) { sb.Append("+"); } sb.Append(key.Key); } else { if (sb.Length > 0) { sb.Append("+"); } sb.Append(c); } return(sb.ToString()); }
private void SetKeyHandlerInternal(string[] keys, Action <ConsoleKeyInfo?, object> handler, string briefDescription, string longDescription, ScriptBlock scriptBlock) { foreach (var key in keys) { var chord = ConsoleKeyChordConverter.Convert(key); if (chord.Length == 1) { _dispatchTable[chord[0]] = MakeKeyHandler(handler, briefDescription, longDescription, scriptBlock); } else { _dispatchTable[chord[0]] = MakeKeyHandler(Chord, "ChordFirstKey"); Dictionary <ConsoleKeyInfo, KeyHandler> secondDispatchTable; if (!_chordDispatchTable.TryGetValue(chord[0], out secondDispatchTable)) { secondDispatchTable = new Dictionary <ConsoleKeyInfo, KeyHandler>(); _chordDispatchTable[chord[0]] = secondDispatchTable; } secondDispatchTable[chord[1]] = MakeKeyHandler(handler, briefDescription, longDescription, scriptBlock); } } }
private void RemoveKeyHandlerInternal(string[] keys) { foreach (var key in keys) { var chord = ConsoleKeyChordConverter.Convert(key); if (chord.Length == 1) { _dispatchTable.Remove(chord[0]); } else { Dictionary <ConsoleKeyInfo, KeyHandler> secondDispatchTable; if (_chordDispatchTable.TryGetValue(chord[0], out secondDispatchTable)) { secondDispatchTable.Remove(chord[1]); if (secondDispatchTable.Count == 0) { _dispatchTable.Remove(chord[0]); } } } } }