3

In the following code:

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

the 'Ctrl+Q' part does not work. How could I make this work?

0

2 Answers 2

5

Here you are:

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

Simpler:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! That's what I've been missing.
0
Add-Type -AssemblyName PresentationFramework
$window = New-Object Windows.Window
$commonKeyEvents = {
    [System.Windows.Input.KeyEventArgs]$e = $args[1]
    if ($e.Key -eq 'Escape') { $this.close() }
    if ([System.Windows.Input.KeyBoard]::Modifiers -eq [System.Windows.Input.ModifierKeys]::Control -and $e.Key -eq 'Q') { $this.close() }
}
$window.add_KeyDown($commonKeyEvents)
$window.ShowDialog() | Out-Null

1 Comment

Also not bad. Looks like a direct translation of some c# code I have seen which is not beautiful but works and serves me maybe to learn to translate the common WPF examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.