Skip to content

Commit 3d246d9

Browse files
authored
Remove Keys.ShouldInsert (#822)
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
1 parent ade4831 commit 3d246d9

File tree

5 files changed

+27
-41
lines changed

5 files changed

+27
-41
lines changed

PSReadLine/KeyBindings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ void SetDefaultWindowsBindings()
245245
{ Keys.PageDown, MakeKeyHandler(ScrollDisplayDown, "ScrollDisplayDown") },
246246
{ Keys.CtrlPageUp, MakeKeyHandler(ScrollDisplayUpLine, "ScrollDisplayUpLine") },
247247
{ Keys.CtrlPageDown, MakeKeyHandler(ScrollDisplayDownLine, "ScrollDisplayDownLine") },
248+
{ Keys.VolumeDown, MakeKeyHandler(Ignore, "Ignore") },
249+
{ Keys.VolumeUp, MakeKeyHandler(Ignore, "Ignore") },
250+
{ Keys.VolumeMute, MakeKeyHandler(Ignore, "Ignore") },
248251
};
249252

250253
// Some bindings are not available on certain platforms
@@ -334,6 +337,9 @@ void SetDefaultEmacsBindings()
334337
{ Keys.CtrlAltY, MakeKeyHandler(YankNthArg, "YankNthArg") },
335338
{ Keys.PageUp, MakeKeyHandler(ScrollDisplayUp, "ScrollDisplayUp") },
336339
{ Keys.PageDown, MakeKeyHandler(ScrollDisplayDown, "ScrollDisplayDown") },
340+
{ Keys.VolumeDown, MakeKeyHandler(Ignore, "Ignore") },
341+
{ Keys.VolumeUp, MakeKeyHandler(Ignore, "Ignore") },
342+
{ Keys.VolumeMute, MakeKeyHandler(Ignore, "Ignore") },
337343
};
338344

339345
// Some bindings are not available on certain platforms

PSReadLine/KeyBindings.vi.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ private void SetDefaultViBindings()
8686
{ Keys.ShiftF3, MakeKeyHandler(CharacterSearchBackward,"CharacterSearchBackward") },
8787
{ Keys.CtrlAltQuestion, MakeKeyHandler(ShowKeyBindings, "ShowKeyBindings") },
8888
{ Keys.CtrlR, MakeKeyHandler(ViSearchHistoryBackward,"ViSearchHistoryBackward") },
89-
{ Keys.CtrlS, MakeKeyHandler(SearchForward, "SearchForward") }
89+
{ Keys.CtrlS, MakeKeyHandler(SearchForward, "SearchForward") },
90+
{ Keys.VolumeDown, MakeKeyHandler(Ignore, "Ignore") },
91+
{ Keys.VolumeUp, MakeKeyHandler(Ignore, "Ignore") },
92+
{ Keys.VolumeMute, MakeKeyHandler(Ignore, "Ignore") },
9093
};
9194

9295
// Some bindings are not available on certain platforms
@@ -205,7 +208,10 @@ private void SetDefaultViBindings()
205208
{ Keys.Minus, MakeKeyHandler(PreviousHistory, "PreviousHistory") },
206209
{ Keys.Period, MakeKeyHandler(RepeatLastCommand, "RepeatLastCommand") },
207210
{ Keys.Semicolon, MakeKeyHandler(RepeatLastCharSearch, "RepeatLastCharSearch") },
208-
{ Keys.Comma, MakeKeyHandler(RepeatLastCharSearchBackwards, "RepeatLastCharSearchBackwards") }
211+
{ Keys.Comma, MakeKeyHandler(RepeatLastCharSearchBackwards, "RepeatLastCharSearchBackwards") },
212+
{ Keys.VolumeDown, MakeKeyHandler(Ignore, "Ignore") },
213+
{ Keys.VolumeUp, MakeKeyHandler(Ignore, "Ignore") },
214+
{ Keys.VolumeMute, MakeKeyHandler(Ignore, "Ignore") },
209215
};
210216

211217
// Some bindings are not available on certain platforms

PSReadLine/Keys.cs

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ static ConsoleKeyInfo CtrlAlt(char c)
363363
public static ConsoleKeyInfo CtrlAltRBracket = CtrlAlt(']');
364364
public static ConsoleKeyInfo CtrlAltQuestion = CtrlAlt('?');
365365

366+
public static ConsoleKeyInfo VolumeUp = Key(ConsoleKey.VolumeUp);
367+
public static ConsoleKeyInfo VolumeDown = Key(ConsoleKey.VolumeDown);
368+
public static ConsoleKeyInfo VolumeMute = Key(ConsoleKey.VolumeMute);
366369

367370
[DllImport("user32.dll")]
368371
public static extern int VkKeyScan(short wAsciiVal);
@@ -469,6 +472,9 @@ internal static bool IgnoreKeyChar(this ConsoleKeyInfo key)
469472
case ConsoleKey.RightArrow:
470473
case ConsoleKey.Tab:
471474
case ConsoleKey.UpArrow:
475+
case ConsoleKey.VolumeUp:
476+
case ConsoleKey.VolumeDown:
477+
case ConsoleKey.VolumeMute:
472478
return true;
473479
}
474480

@@ -560,43 +566,6 @@ internal static int GetNormalizedHashCode(this ConsoleKeyInfo obj)
560566
return unchecked(((h1 << 5) + h1) ^ h2);
561567
}
562568

563-
internal static bool ShouldInsert(this ConsoleKeyInfo key)
564-
{
565-
var keyChar = key.KeyChar;
566-
if (keyChar == '\0') return false;
567-
foreach (char c in " `~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?") {
568-
// we always want to insert chars essential to the PowerShell experience
569-
if (keyChar == c) { return true; }
570-
}
571-
if (key.Modifiers != 0)
572-
{
573-
// We want to ignore control sequences - but distinguishing a control sequence
574-
// isn't as simple as it could be because we will get inconsistent modifiers
575-
// depending on the OS or keyboard layout.
576-
//
577-
// Windows will give us the key state even if we didn't care, e.g. it's normal
578-
// to see Alt+Control (AltGr) on a Spanish, French, or German keyboard for many normal
579-
// characters. So we just ask the OS what mods to expect for a given key.
580-
// On non-Windows - anything but Shift is assumed to be a control sequence.
581-
ConsoleModifiers expectedMods = default(ConsoleModifiers);
582-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
583-
{
584-
var keyWithMods = WindowsKeyScan(key.KeyChar);
585-
if (keyWithMods.HasValue)
586-
{
587-
expectedMods = keyWithMods.Value.Modifiers;
588-
}
589-
}
590-
else
591-
{
592-
expectedMods = key.Modifiers & ConsoleModifiers.Shift;
593-
}
594-
return key.Modifiers == expectedMods;
595-
}
596-
597-
return true;
598-
}
599-
600569
internal static bool IsUnmodifiedChar(this ConsoleKeyInfo key, char c)
601570
{
602571
return key.KeyChar == c &&

PSReadLine/ReadLine.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ void ProcessOneKey(ConsoleKeyInfo key, Dictionary<ConsoleKeyInfo, KeyHandler> di
600600
handler.Action(key, arg);
601601
}
602602
}
603-
else if (!ignoreIfNoAction && key.ShouldInsert())
603+
else if (!ignoreIfNoAction)
604604
{
605605
SelfInsert(key, arg);
606606
}
@@ -848,6 +848,11 @@ private static void Chord(ConsoleKeyInfo? key = null, object arg = null)
848848
}
849849
}
850850

851+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
852+
private static void Ignore(ConsoleKeyInfo? key = null, object arg = null)
853+
{
854+
}
855+
851856
/// <summary>
852857
/// Abort current action, e.g. incremental history search.
853858
/// </summary>

PSReadLine/Replace.vi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private static void ViReplaceUntilEsc(ConsoleKeyInfo? key, object arg)
4444
_singleton.Render();
4545
}
4646
}
47-
else if (nextKey.ShouldInsert())
47+
else
4848
{
4949
if (_singleton._current >= _singleton._buffer.Length)
5050
{

0 commit comments

Comments
 (0)