WPF 和 Powershell 的键盘快捷键

TNT*_*TNT 3 wpf powershell keyboard-shortcuts

在下面的代码中:

Add-Type -AssemblyName PresentationFramework
$window = New-Object Windows.Window
$commonKeyEvents = {
    [System.Windows.Input.KeyEventArgs] $e = $args[1]
    if ($e.Key -eq 'ESC') { $this.close() }
    if ($e.Key -eq 'Ctrl+Q') { $this.close() }
}
$window.add_PreViewKeyDown($commonKeyEvents)
$window.ShowDialog() | Out-Null
Run Code Online (Sandbox Code Playgroud)

'Ctrl+Q'部分不起作用。我怎样才能做到这一点?

sod*_*low 5

给你:

Add-Type -AssemblyName PresentationFramework
$window = New-Object Windows.Window

$commonKeyEvents = {
    [System.Windows.Input.KeyEventArgs] $e = $args[1]

    if (($e.Key -eq "Q" -and $e.KeyboardDevice.Modifiers -eq "Ctrl") -or
        ($e.Key -eq "ESC")) {            
        $this.Close()
    }
}

$window.Add_PreViewKeyDown($commonKeyEvents)
$window.ShowDialog() | Out-Null
Run Code Online (Sandbox Code Playgroud)

更简单:

Add-Type -AssemblyName PresentationFramework
$window = New-Object Windows.Window

$commonKeyEvents = {
    if (($_.Key -eq "Q" -and $_.KeyboardDevice.Modifiers -eq "Ctrl") -or
        ($_.Key -eq "ESC")) {            
        $this.Close()
    }
}

$window.Add_PreViewKeyDown($commonKeyEvents)
$window.ShowDialog() | Out-Null
Run Code Online (Sandbox Code Playgroud)