Skip to content

Support restoring a previously retrieved key handler with Set-PSReadlineKeyHandler #880

Open
@mklement0

Description

@mklement0

The [Microsoft.PowerShell.KeyHandler] instances output by Get-PSReadlineKeyHandler can currently not be used to later restore that handler (after temporarily installing a different handler), if the original handler was based on custom script block rather than a built-in function.

The reason is that the .Function property is a [string] that merely returns 'Custom' for script-block-based handlers, and the script block itself is not returned.

One option is to simply add .ScriptBlock property that contains the original script block (which would only be filled if .Function contains Custom).

Complementarily, it would be nice if Set-PSReadlineKeyHandler supported a -Handler parameter that can directly accept a previously retrieved [Microsoft.PowerShell.KeyHandler] instance.

E.g.:

# Retrieve and store the current handler.
# See also: #879
$prevEscKeyHandler = Get-PSReadLineKeyHandler | ? Key -eq Escape

# Temporarily install different handler.
Set-PSReadLineKeyHandler -Key Escape -ScriptBock { Write-Verbose -vb 'ESC pressed' }
try {
  
  # ... 
  
} finally {
  # Restore previous handler
  # WISHFUL THINKING
  Set-PSReadLineKeyHandler -Handler $prevEscKeyHandler
}

Environment data

PSReadline version: 2.0.0-beta3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions