示例#1
0
        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());
        }
示例#2
0
 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");
             if (!_chordDispatchTable.TryGetValue(chord[0], out var secondDispatchTable))
             {
                 secondDispatchTable           = new Dictionary <ConsoleKeyInfo, KeyHandler>(ConsoleKeyInfoComparer.Instance);
                 _chordDispatchTable[chord[0]] = secondDispatchTable;
             }
             secondDispatchTable[chord[1]] = MakeKeyHandler(handler, briefDescription, longDescription, scriptBlock);
         }
     }
 }
示例#3
0
 private void RemoveKeyHandlerInternal(string[] keys)
 {
     foreach (var key in keys)
     {
         var chord = ConsoleKeyChordConverter.Convert(key);
         if (chord.Length == 1)
         {
             _dispatchTable.Remove(chord[0]);
         }
         else
         {
             if (_chordDispatchTable.TryGetValue(chord[0], out var secondDispatchTable))
             {
                 secondDispatchTable.Remove(chord[1]);
                 if (secondDispatchTable.Count == 0)
                 {
                     _dispatchTable.Remove(chord[0]);
                 }
             }
         }
     }
 }
示例#4
0
        /// <summary>
        /// Return key handlers bound to specified chords.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable <PowerShell.KeyHandler> GetKeyHandlers(string[] Chord)
        {
            var boundFunctions = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            if (Chord == null || Chord.Length == 0)
            {
                yield break;
            }

            foreach (string Key in Chord)
            {
                ConsoleKeyInfo[] consoleKeyChord = ConsoleKeyChordConverter.Convert(Key);
                PSKeyInfo        firstKey        = PSKeyInfo.FromConsoleKeyInfo(consoleKeyChord[0]);

                if (_singleton._dispatchTable.TryGetValue(firstKey, out KeyHandler entry))
                {
                    if (consoleKeyChord.Length == 1)
                    {
                        yield return(new PowerShell.KeyHandler
                        {
                            Key = firstKey.KeyStr,
                            Function = entry.BriefDescription,
                            Description = entry.LongDescription,
                            Group = GetDisplayGrouping(entry.BriefDescription),
                        });
                    }
                    else
                    {
                        PSKeyInfo secondKey = PSKeyInfo.FromConsoleKeyInfo(consoleKeyChord[1]);
                        if (_singleton._chordDispatchTable.TryGetValue(firstKey, out var secondDispatchTable) &&
                            secondDispatchTable.TryGetValue(secondKey, out entry))
                        {
                            yield return(new PowerShell.KeyHandler
                            {
                                Key = firstKey.KeyStr + "," + secondKey.KeyStr,
                                Function = entry.BriefDescription,
                                Description = entry.LongDescription,
                                Group = GetDisplayGrouping(entry.BriefDescription),
                            });
                        }
                    }
                }

                // If in Vi mode, also check Vi's command mode list.
                if (_singleton._options.EditMode == EditMode.Vi)
                {
                    if (_viCmdKeyMap.TryGetValue(firstKey, out entry))
                    {
                        if (consoleKeyChord.Length == 1)
                        {
                            if (entry.BriefDescription == "Ignore")
                            {
                                continue;
                            }

                            yield return(new PowerShell.KeyHandler
                            {
                                Key = "<" + firstKey.KeyStr + ">",
                                Function = entry.BriefDescription,
                                Description = entry.LongDescription,
                                Group = GetDisplayGrouping(entry.BriefDescription),
                            });
                        }
                        else
                        {
                            PSKeyInfo secondKey = PSKeyInfo.FromConsoleKeyInfo(consoleKeyChord[1]);
                            if (_viCmdChordTable.TryGetValue(firstKey, out var secondDispatchTable) &&
                                secondDispatchTable.TryGetValue(secondKey, out entry))
                            {
                                if (entry.BriefDescription == "Ignore")
                                {
                                    continue;
                                }

                                yield return(new PowerShell.KeyHandler
                                {
                                    Key = "<" + firstKey.KeyStr + "," + secondKey.KeyStr + ">",
                                    Function = entry.BriefDescription,
                                    Description = entry.LongDescription,
                                    Group = GetDisplayGrouping(entry.BriefDescription),
                                });
                            }
                        }
                    }
                }
            }
        }
示例#5
0
 public ConsoleKeyInfo AsConsoleKeyInfo()
 {
     return(ConsoleKeyChordConverter.Convert(KeyStr)[0]);
 }