question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set-PSReadlineKeyHandler fails with certain key chords on Unix

See original GitHub issue

From @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:closed
  • Created 6 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
lzybkrcommented, Apr 11, 2018

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, if PSReadLine were to rely on sequences everywhere:

  • Sequences are somewhat limiting, e.g. some popular key bindings on Windows can’t work on Linux, see #638 and #639.
  • Sequences would not work on Win7, Win8, or earlier Win10 systems because the console does not provide a way to generate sequences.
  • Without complex 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) use terminfo because Windows doesn’t have terminfo support.

I’m not perfectly happy with how things are today, but I think I’ve made reasonable choices considering the alternatives.

1reaction
lzybkrcommented, Apr 10, 2018

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 same char sequence which is what the CLR sees after the key press. The CLR then translates the char sequence into a ConsoleKeyInfo.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set-PSReadlineKeyHandler fails with certain key chords ...
Alt -based key chords (mapped from Esc -based key sequences):. showkey -a tells us that something like Alt-" is translated into a key...
Read more >
Set-PSReadLineKeyHandler issues on Linux? : r/PowerShell
Has anyone tried using Set-PSReadLineKeyHandler on Linux? Have you run into any issues? I found that certain things don't work.
Read more >
Let PSReadline Handle PowerShell - Part 5
PowerShell teacher Jeff Hicks demonstrates how to take advantage of PSReadline key handlers to optimize your console experience.
Read more >
Set-PSReadLineKeyHandler (PSReadLine) - PowerShell
The Set-PSReadLineKeyHandler cmdlet customizes the result when a key or sequence of keys is pressed. With user-defined key bindings, you can do almost ......
Read more >
Stopping a powershell script with a key combo
If you have PSReadLine, you could try Set-PSReadlineKeyHandler to handle key chords. Not sure if you can easily use it to break out...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found