Set-PSReadLineKeyHandler

Binds keys to user-defined or PSReadLine key handler functions.

语法

ScriptBlock

Set-PSReadLineKeyHandler
    [-ScriptBlock] <ScriptBlock>
    [-Chord] <String[]>
    [-BriefDescription <String>]
    [-Description <String>]
    [-ViMode <ViMode>]
    [<CommonParameters>]

Function

Set-PSReadLineKeyHandler
    [-Chord] <String[]>
    [-Function] <String>
    [-ViMode <ViMode>]
    [<CommonParameters>]

说明

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 anything that's possible from within a PowerShell script.

示例

Example 1: Bind the arrow key to a function

This command binds the up arrow key to the HistorySearchBackward function. This function searches command history for command lines that start with the current contents of the command line.

Set-PSReadLineKeyHandler -Chord UpArrow -Function HistorySearchBackward

Example 2: Bind a key to a script block

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()
}

参数

-BriefDescription

A brief description of the key binding. This description is displayed by the Get-PSReadLineKeyHandler cmdlet.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False

参数集

ScriptBlock
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Chord

The key or sequence of keys to be bound to a function or script block. Use a single string to specify a single binding. If the binding is a sequence of keys, separate the keys by a comma. For example: Ctrl+x,Ctrl+l

Letter key references are defined using lowercase letters. If you want to define a chord that uses an uppercase letter, the chord must include the Shift key. For example, Ctrl+Shift+x and Ctrl+x create different bindings.

This parameter accepts an array of strings. Each string is a separate binding, not a sequence of keys for a single binding.

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False
别名:Key

参数集

(All)
Position:0
必需:True
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Description

Specifies a more detailed description of the key binding that's visible in the output of the Get-PSReadLineKeyHandler cmdlet.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False
别名:LongDescription

参数集

ScriptBlock
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Function

Specifies the name of an existing key handler provided by PSReadLine. This parameter lets you rebind existing key bindings, or bind a handler that's currently unbound.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False

参数集

Function
Position:1
必需:True
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-ScriptBlock

Specifies a script block value to run when the chord is entered. PSReadLine passes one or two parameters to this script block. The first parameter is a ConsoleKeyInfo object representing the key pressed. The second argument can be any object depending on the context.

参数属性

类型:ScriptBlock
默认值:None
支持通配符:False
不显示:False

参数集

ScriptBlock
Position:1
必需:True
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-ViMode

Specify which vi mode the binding applies to.

Valid values are:

  • Insert
  • Command

参数属性

类型:ViMode
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

输入

None

You can't pipe objects to this cmdlet.

输出

None

This cmdlet returns no output.