PowerShell WPF UI检测密钥组合

PowerShell WPF UI检测密钥组合,wpf,powershell,xaml,keyboard-events,Wpf,Powershell,Xaml,Keyboard Events,我正在编写一个在WinPE中运行的UI。到目前为止,我的一切都正常工作,我正在努力让用户无法关闭窗口。我已使其全屏显示并禁用了关闭(X)按钮,但用户仍可以按Alt+F4关闭UI。我已经能够使它,如果用户点击F8的用户界面是在前面,所以用户不能Alt+Tab到它。我已经阅读了很多这样做的方法,但是没有任何内容涵盖PowerShell,我不知道如何在脚本中实现它。它不是在运行空间中运行 以下是我尝试过的: $altF4Pressed = { [System.Windows.Input.Key

我正在编写一个在WinPE中运行的UI。到目前为止,我的一切都正常工作,我正在努力让用户无法关闭窗口。我已使其全屏显示并禁用了关闭(X)按钮,但用户仍可以按Alt+F4关闭UI。我已经能够使它,如果用户点击F8的用户界面是在前面,所以用户不能Alt+Tab到它。我已经阅读了很多这样做的方法,但是没有任何内容涵盖PowerShell,我不知道如何在脚本中实现它。它不是在运行空间中运行

以下是我尝试过的:

$altF4Pressed = {
    [System.Windows.Input.KeyEventArgs]$Alt = $args[1]
    if ( ($Alt.Key -eq 'System') -or ($Alt.Key -eq 'F4') ) {
        if ($_.CloseReason -eq 'UserClosing') {
            $UI.Cancel = $true
        }
    }
}

$null = $UI.add_KeyDown($altF4Pressed)
我也读过做这件事(),但这不起作用

#disable closing window using Alt+F4
$UI_KeyDown = [System.Windows.Forms.KeyEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]

    if ($_.Alt -eq $true -and $_.KeyCode -eq 'F4') {
        $script:altF4Pressed = $true;
    }
}

$UI_Closing = [System.Windows.Forms.FormClosingEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]

    if ($script:altF4Pressed)
    {
        if ($_.CloseReason -eq 'UserClosing') {
            $_.Cancel = $true
            $script:altF4Pressed = $false;
        }
    }
    Else{
        [System.Windows.Forms.Application]::Exit();
        # Stop-Process $pid
    }
}


$UI.Add_Closing({$UI_Closing})
$UI.add_KeyDown({$UI_KeyDown})
我也尝试过这样做:

$UI.Add_KeyDown({
    $key = $_.Key
    If ([System.Windows.Input.Keyboard]::IsKeyDown("RightAlt") -OR [System.Windows.Input.Keyboard]::IsKeyDown("LeftAlt")) {
        Switch ($Key) {
            "F4" {
                $script:altF4Pressed = $true;
                write-host "Alt+f4 was pressed"
            }

        }
    }
})

它检测第一次按下键盘,但在按下另一次键盘时检测不到下一次按下。我想我需要改用Keybinding事件,只是不确定如何在Powershell中在应用程序级别(而不是输入级别)实现它。我了解到您可以向XAML代码本身添加键绑定,但如何使用Powershell在UI出现时检测键组合(Alt+F4)?

我发现使用表单或WPF检测键远高于我的技能水平。然而,我能够用不同的方法完成相同的任务。我需要在Windows10中使用安装脚本来实现这一点

我所做的是在脚本运行期间禁用左/右alt键和F4,并在完成后恢复更改、删除键

WinPE的注册表在每次重新启动时都会被“重置”,如果您想在重新启动后取消更改,这是您的电话

# Disable Left & Right ALT keys and F4
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout" -name "Scancode Map" -Value ([byte[]](0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x38,0xe0,0x00,0x00,0x00,0x00))

要重新启用Alt+F4,只需删除“扫描码映射”注册表值并重新启动。
我在这里找到了答案:

您没有可复制的示例,我不想为此编写一个示例,但我认为这可能会有所帮助

$altF4Pressed = {
    [System.Windows.Input.KeyEventArgs]$Alt = $args[1]
    if ( ($Alt.Key -eq 'System') -or ($Alt.Key -eq 'F4') ) {
        if ($_.CloseReason -eq 'UserClosing') {
            $UI.Cancel = $true
            $Alt.Handled = $true
        }
    }
}

$null = $UI.add_KeyDown([System.Windows.Input.KeyEventHandler]::new($altF4Pressed))
对于WPF,您还需要使用
System.Windows.Input
而不是
System.Windows.Forms
,因此,如果更改名称空间,您发布的第二个代码片段实际上可能会起作用