Home > Software engineering >  Set-PSReadLineKeyHandler get registered but cant be triggered
Set-PSReadLineKeyHandler get registered but cant be triggered

Time:08-22

Set-PSReadLineKeyHandler not working. If I for example define:

Set-PSReadLineKeyHandler -Chord shift B -ScriptBlock {
"Line 1"
"Line 2"
"Line 3"
}

And then run Get-PSReadLineKeyHandler just to see the user define actions:

Key Function Description  
--- -------- -----------  
Ctrl j CustomAction User defined action  
Ctrl B CustomAction User defined action  
B CustomAction User defined action

When it comes time to trigger the hotkeys nothing happens. None of the user defined hotkeys for actions trigger. No errors, just nothing.


PSVersion 7.3.0-preview.7

PSReadLine 2.2.6

Windows Terminal Preview 1.15.2003.0

Thanks for helping me.

CodePudding user response:

As Otter's comment says, to write text or move the cursor around you need to call methods to make PSReadLine do those things. See the Set-PSReadLineKeyHandler documentation, e.g. example 2:

This example shows how a single key can be used to run a command. The command binds the key Ctrl B to a script block that clears the line, inserts the word "build", and then accepts the line.

Set-PSReadLineKeyHandler -Chord Ctrl B -ScriptBlock {
    [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert('build')
    [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}

At a PowerShell prompt you can start typing [Microsoft.PowerShell.PSConsoleReadLine]:: and then press Ctrl Space to get an autocomplete view of available methods; they are documented here.

  • Related