使用WPF和Powershell的键盘快捷键

时间:2018-05-24 17:06:36

标签: 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

'Ctrl+Q'部分不起作用。我怎么能做这个工作?

2 个答案:

答案 0 :(得分:3)

你在这里:

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

简单:

void run_awk(char *command, char *out, char *err){
    int in_pipe[2]; //send input string from parent to child --> input
    int out_pipe[2]; //receive awk's stdout from child --> send back to client
    int err_pipe[2]; //receive awk's stderr from child --> write on fd 4, send back to client with errline
    pid_t p;
    if((pipe(in_pipe)+pipe(out_pipe)+pipe(err_pipe)) < 0) syscallerror("pipe");
    if((p = fork()) < 0) syscallerror("fork");
    if(p > 0){
        close(in_pipe[0]);  // Close reading end of first pipe
        close(out_pipe[1]); // Close writing end of second pipe
        write(in_pipe[1], command, strlen(command)+1);
        printf("parent process %d: sent input %s\n", getpid(), command);
        close(in_pipe[1]);
        // Wait for child to send out and err
        wait(NULL);
        char s_out[301];
        char s_err[301];
        int n = read(out_pipe[0], s_out, 300);
        int k = read(err_pipe[0], s_err, 300);
        if(n > 0) printf("parent %d: received output %s\n", getpid(), s_out);
        if(k > 0) printf("parent %d: received error %s\n", getpid(), s_err);
        close(out_pipe[0]);
        close(err_pipe[0]);
        close(err_pipe[1]);
    }
    else if(p == 0){
        close(in_pipe[1]);  // Close writing end of first pipe
        // Read a string using first pipe
        char buf[300];
        read(in_pipe[0], buf, 300);
        printf("child %d: received input %s\n", getpid(), buf);
        // Close all reading ends
        close(in_pipe[0]);
        close(out_pipe[0]);
        close(err_pipe[0]);
        dup2(out_pipe[1], 1); //set out_pipe[1] as stdout
        dup2(err_pipe[1], 2); //set err_pipe[1] as stderr
        close(err_pipe[1]);
        close(out_pipe[1]);
        execl("usr/bin/awk", "awk", buf, (char *)0); //at this point awk's
        exit(0);               //out and err should be redirected to the pipes
    }
}

答案 1 :(得分: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