Set-PSReadlineKeyHandler fails with certain key chords on Unix
See original GitHub issueFrom @mklement0 on April 17, 2017 19:44
Steps to reproduce
Define <kbd>Control</kbd> and <kbd>Alt</kbd>-key-based chords for doubling characters '
, "
, and (
when typed with <kbd>Control</kbd> or <kbd>Alt</kbd> held down:
Set-PSReadlineKeyHandler -Chord "Ctrl+'" { [Microsoft.PowerShell.PSConsoleReadLine]::Insert("''") }
Set-PSReadlineKeyHandler -Chord 'Ctrl+"' { [Microsoft.PowerShell.PSConsoleReadLine]::Insert('""') }
Set-PSReadlineKeyHandler -Chord 'Ctrl+(' { [Microsoft.PowerShell.PSConsoleReadLine]::Insert('()') }
Set-PSReadlineKeyHandler -Chord "Alt+'" { [Microsoft.PowerShell.PSConsoleReadLine]::Insert("''") }
Set-PSReadlineKeyHandler -Chord 'Alt+"' { [Microsoft.PowerShell.PSConsoleReadLine]::Insert('""') }
Set-PSReadlineKeyHandler -Chord 'Alt+(' { [Microsoft.PowerShell.PSConsoleReadLine]::Insert('()') }
Expected behavior
The definitions should be accepted on all platforms and function as defined.
E.g. <kbd>Ctrl+'</kbd> should result in ''
.
Actual behavior
On Windows, the actual behavior matches the expected behavior.
On Unix platforms (verified on macOS 10.12.4 and Ubuntu 16.04 with the respective default terminal applications), the definitions fail with (repetitions of) the following error messages (note that omitting modifier keys ctrl
and alt
from the definitions would work fine):
Set-PSReadlineKeyHandler : Unrecognized key 'ctrl'. Please use a character literal or a well-known key name from the System.ConsoleKey enumeration.
Set-PSReadlineKeyHandler : Unrecognized key 'alt'. Please use a character literal or a well-known key name from the System.ConsoleKey enumeration.
The error messages suggest that ctrl
/ alt
are not recognized as modifier keys in this case.
Note that something like Ctrl+A
does work, however; the problem appears to be the specific characters ('
, "
, (
) that the modifiers are paired with.
Environment data
PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Microsoft Windows 10 Pro (64-bit; v10.0.14393)
PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Darwin Kernel Version 16.5.0: Fri Mar 3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64
PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Darwin Kernel Version 16.5.0: Fri Mar 3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64
Copied from original issue: PowerShell/PowerShell#3584
Issue Analytics
- State:
- Created 6 years ago
- Comments:14 (7 by maintainers)
Right - an application reading from
stdin
only sees the sequences. Keyboards haven’t always had an <kbd>Alt</kbd> key, so the <kbd>Escape</kbd> key was an obvious way to add useful key bindings, and when keyboards evolved, it made sense to generate the same sequence.PSReadLine
could be implemented as you suggest, but it would take on more complexity than I’m willing to take on and there are issues, some unsolvable, ifPSReadLine
were to rely on sequences everywhere:terminfo
support, sequences would only support the most common terminals.PSReadLine
actually has some support for sequences so that it can run under tmux on WSL (using the Windows version of PowerShell). The code is here, but it is doesn’t (can’t) useterminfo
because Windows doesn’t haveterminfo
support.I’m not perfectly happy with how things are today, but I think I’ve made reasonable choices considering the alternatives.
Not quite - on Linux, <kbd>Ctrl-‘</kbd> is indistinguishable from <kbd>’</kbd>. The command Linux
showkeys -a
is useful to help me figure out what is possible, what might be a CLR bug, or my bug, in this case, it shows those keys generate the samechar
sequence which is what the CLR sees after the key press. The CLR then translates thechar
sequence into aConsoleKeyInfo
.I thought there was an issue for rejecting key bindings that can’t work, but I can’t find it at the moment, and to be honest, I’m not exactly sure how I’d implement that on non-Windows platforms.