From 910f48b859920cbffcf2073664b378bbc5d07d2b Mon Sep 17 00:00:00 2001 From: Jason shirk Date: Sat, 1 Dec 2018 23:06:15 -0800 Subject: [PATCH 1/2] Remove Keys.ShouldInsert The method Keys.ShouldInsert was a misguided attempt to avoid inserting keys where there is no binding but the key really doesn't make sense as input. Unfortunately it is just causing more pain and probably wasn't addressing a serious problem, so I'm removing it. Fix #798 --- PSReadLine/Keys.cs | 37 ------------------------------------- PSReadLine/ReadLine.cs | 2 +- PSReadLine/Replace.vi.cs | 2 +- 3 files changed, 2 insertions(+), 39 deletions(-) diff --git a/PSReadLine/Keys.cs b/PSReadLine/Keys.cs index cc3e54c02..5027c71d6 100644 --- a/PSReadLine/Keys.cs +++ b/PSReadLine/Keys.cs @@ -560,43 +560,6 @@ internal static int GetNormalizedHashCode(this ConsoleKeyInfo obj) return unchecked(((h1 << 5) + h1) ^ h2); } - internal static bool ShouldInsert(this ConsoleKeyInfo key) - { - var keyChar = key.KeyChar; - if (keyChar == '\0') return false; - foreach (char c in " `~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?") { - // we always want to insert chars essential to the PowerShell experience - if (keyChar == c) { return true; } - } - if (key.Modifiers != 0) - { - // We want to ignore control sequences - but distinguishing a control sequence - // isn't as simple as it could be because we will get inconsistent modifiers - // depending on the OS or keyboard layout. - // - // Windows will give us the key state even if we didn't care, e.g. it's normal - // to see Alt+Control (AltGr) on a Spanish, French, or German keyboard for many normal - // characters. So we just ask the OS what mods to expect for a given key. - // On non-Windows - anything but Shift is assumed to be a control sequence. - ConsoleModifiers expectedMods = default(ConsoleModifiers); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - var keyWithMods = WindowsKeyScan(key.KeyChar); - if (keyWithMods.HasValue) - { - expectedMods = keyWithMods.Value.Modifiers; - } - } - else - { - expectedMods = key.Modifiers & ConsoleModifiers.Shift; - } - return key.Modifiers == expectedMods; - } - - return true; - } - internal static bool IsUnmodifiedChar(this ConsoleKeyInfo key, char c) { return key.KeyChar == c && diff --git a/PSReadLine/ReadLine.cs b/PSReadLine/ReadLine.cs index 8bc80644b..42aab723d 100644 --- a/PSReadLine/ReadLine.cs +++ b/PSReadLine/ReadLine.cs @@ -600,7 +600,7 @@ void ProcessOneKey(ConsoleKeyInfo key, Dictionary di handler.Action(key, arg); } } - else if (!ignoreIfNoAction && key.ShouldInsert()) + else if (!ignoreIfNoAction) { SelfInsert(key, arg); } diff --git a/PSReadLine/Replace.vi.cs b/PSReadLine/Replace.vi.cs index 54b321cb2..b29b13c64 100644 --- a/PSReadLine/Replace.vi.cs +++ b/PSReadLine/Replace.vi.cs @@ -44,7 +44,7 @@ private static void ViReplaceUntilEsc(ConsoleKeyInfo? key, object arg) _singleton.Render(); } } - else if (nextKey.ShouldInsert()) + else { if (_singleton._current >= _singleton._buffer.Length) { From 43c98f2a9ac478cc34d8ea703eba9c7118c993c7 Mon Sep 17 00:00:00 2001 From: Jason shirk Date: Sun, 2 Dec 2018 07:06:41 -0800 Subject: [PATCH 2/2] Bring back Ignore for Volume* keys --- PSReadLine/KeyBindings.cs | 6 ++++++ PSReadLine/KeyBindings.vi.cs | 10 ++++++++-- PSReadLine/Keys.cs | 6 ++++++ PSReadLine/ReadLine.cs | 5 +++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/PSReadLine/KeyBindings.cs b/PSReadLine/KeyBindings.cs index 56aaa32be..d637e7849 100644 --- a/PSReadLine/KeyBindings.cs +++ b/PSReadLine/KeyBindings.cs @@ -245,6 +245,9 @@ void SetDefaultWindowsBindings() { Keys.PageDown, MakeKeyHandler(ScrollDisplayDown, "ScrollDisplayDown") }, { Keys.CtrlPageUp, MakeKeyHandler(ScrollDisplayUpLine, "ScrollDisplayUpLine") }, { Keys.CtrlPageDown, MakeKeyHandler(ScrollDisplayDownLine, "ScrollDisplayDownLine") }, + { Keys.VolumeDown, MakeKeyHandler(Ignore, "Ignore") }, + { Keys.VolumeUp, MakeKeyHandler(Ignore, "Ignore") }, + { Keys.VolumeMute, MakeKeyHandler(Ignore, "Ignore") }, }; // Some bindings are not available on certain platforms @@ -334,6 +337,9 @@ void SetDefaultEmacsBindings() { Keys.CtrlAltY, MakeKeyHandler(YankNthArg, "YankNthArg") }, { Keys.PageUp, MakeKeyHandler(ScrollDisplayUp, "ScrollDisplayUp") }, { Keys.PageDown, MakeKeyHandler(ScrollDisplayDown, "ScrollDisplayDown") }, + { Keys.VolumeDown, MakeKeyHandler(Ignore, "Ignore") }, + { Keys.VolumeUp, MakeKeyHandler(Ignore, "Ignore") }, + { Keys.VolumeMute, MakeKeyHandler(Ignore, "Ignore") }, }; // Some bindings are not available on certain platforms diff --git a/PSReadLine/KeyBindings.vi.cs b/PSReadLine/KeyBindings.vi.cs index ef1b744be..40068b1f8 100644 --- a/PSReadLine/KeyBindings.vi.cs +++ b/PSReadLine/KeyBindings.vi.cs @@ -86,7 +86,10 @@ private void SetDefaultViBindings() { Keys.ShiftF3, MakeKeyHandler(CharacterSearchBackward,"CharacterSearchBackward") }, { Keys.CtrlAltQuestion, MakeKeyHandler(ShowKeyBindings, "ShowKeyBindings") }, { Keys.CtrlR, MakeKeyHandler(ViSearchHistoryBackward,"ViSearchHistoryBackward") }, - { Keys.CtrlS, MakeKeyHandler(SearchForward, "SearchForward") } + { Keys.CtrlS, MakeKeyHandler(SearchForward, "SearchForward") }, + { Keys.VolumeDown, MakeKeyHandler(Ignore, "Ignore") }, + { Keys.VolumeUp, MakeKeyHandler(Ignore, "Ignore") }, + { Keys.VolumeMute, MakeKeyHandler(Ignore, "Ignore") }, }; // Some bindings are not available on certain platforms @@ -205,7 +208,10 @@ private void SetDefaultViBindings() { Keys.Minus, MakeKeyHandler(PreviousHistory, "PreviousHistory") }, { Keys.Period, MakeKeyHandler(RepeatLastCommand, "RepeatLastCommand") }, { Keys.Semicolon, MakeKeyHandler(RepeatLastCharSearch, "RepeatLastCharSearch") }, - { Keys.Comma, MakeKeyHandler(RepeatLastCharSearchBackwards, "RepeatLastCharSearchBackwards") } + { Keys.Comma, MakeKeyHandler(RepeatLastCharSearchBackwards, "RepeatLastCharSearchBackwards") }, + { Keys.VolumeDown, MakeKeyHandler(Ignore, "Ignore") }, + { Keys.VolumeUp, MakeKeyHandler(Ignore, "Ignore") }, + { Keys.VolumeMute, MakeKeyHandler(Ignore, "Ignore") }, }; // Some bindings are not available on certain platforms diff --git a/PSReadLine/Keys.cs b/PSReadLine/Keys.cs index 5027c71d6..cc09829a8 100644 --- a/PSReadLine/Keys.cs +++ b/PSReadLine/Keys.cs @@ -363,6 +363,9 @@ static ConsoleKeyInfo CtrlAlt(char c) public static ConsoleKeyInfo CtrlAltRBracket = CtrlAlt(']'); public static ConsoleKeyInfo CtrlAltQuestion = CtrlAlt('?'); + public static ConsoleKeyInfo VolumeUp = Key(ConsoleKey.VolumeUp); + public static ConsoleKeyInfo VolumeDown = Key(ConsoleKey.VolumeDown); + public static ConsoleKeyInfo VolumeMute = Key(ConsoleKey.VolumeMute); [DllImport("user32.dll")] public static extern int VkKeyScan(short wAsciiVal); @@ -469,6 +472,9 @@ internal static bool IgnoreKeyChar(this ConsoleKeyInfo key) case ConsoleKey.RightArrow: case ConsoleKey.Tab: case ConsoleKey.UpArrow: + case ConsoleKey.VolumeUp: + case ConsoleKey.VolumeDown: + case ConsoleKey.VolumeMute: return true; } diff --git a/PSReadLine/ReadLine.cs b/PSReadLine/ReadLine.cs index 42aab723d..11c10c0f5 100644 --- a/PSReadLine/ReadLine.cs +++ b/PSReadLine/ReadLine.cs @@ -848,6 +848,11 @@ private static void Chord(ConsoleKeyInfo? key = null, object arg = null) } } + [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")] + private static void Ignore(ConsoleKeyInfo? key = null, object arg = null) + { + } + /// /// Abort current action, e.g. incremental history search. ///