PowerShell WPF UI 检测组合键
PowerShell WPF UI detect key combination
我正在 WinPE 中将 UI 写入 运行。到目前为止,我一切正常,并且正在努力使用户无法关闭 window。我已将其设为全屏并禁用关闭 (X) 按钮,但用户仍可以按 Alt+F4 来关闭 UI。我已经能够做到,如果用户按 F8,UI 就在前面,所以用户不能按 Alt+Tab 键。我已经阅读了很多方法来执行此操作,但没有任何内容适用于 PowerShell 我不确定如何在脚本中实现它。它不在运行空间中 运行ning。
这是我尝试过的:
$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)
我也读过这样做(Disabling Alt+F4 in a Powershell form),但这不起作用。
#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"
}
}
}
})
它检测到第一次键盘按下,但在另一个被按下时检测不到下一个。我想我需要改用键绑定事件,只是不确定如何在 App 级别(而非输入级别)在 Powershell 中实现它。我读到您可以将键绑定添加到 XAML 代码本身,但是如何使用 Powershell 在 UI 出现时检测组合键 (Alt+F4)?
我发现使用窗体或 WPF 检测按键远远超出我的技能水平。但是,我能够使用不同的方法完成相同的任务。我需要使用 Windows 10 中的安装脚本来完成此操作。
我所做的是在脚本 运行 期间同时禁用 left/right 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,只需删除 "Scancode Map" 注册表值并再次重新启动。
我在这里找到了答案:http://www.edugeek.net/forums/windows-server-2008-r2/121900-disable-alt-f4.html
您没有可复制的示例,我不想为此编写示例,但我认为这可能会有所帮助。
$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 的 IIRC,您需要使用 System.Windows.Input
而不是 System.Windows.Forms
,因此如果您更改命名空间,您发布的第二个片段实际上可能会起作用。
我正在 WinPE 中将 UI 写入 运行。到目前为止,我一切正常,并且正在努力使用户无法关闭 window。我已将其设为全屏并禁用关闭 (X) 按钮,但用户仍可以按 Alt+F4 来关闭 UI。我已经能够做到,如果用户按 F8,UI 就在前面,所以用户不能按 Alt+Tab 键。我已经阅读了很多方法来执行此操作,但没有任何内容适用于 PowerShell 我不确定如何在脚本中实现它。它不在运行空间中 运行ning。
这是我尝试过的:
$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)
我也读过这样做(Disabling Alt+F4 in a Powershell form),但这不起作用。
#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"
}
}
}
})
它检测到第一次键盘按下,但在另一个被按下时检测不到下一个。我想我需要改用键绑定事件,只是不确定如何在 App 级别(而非输入级别)在 Powershell 中实现它。我读到您可以将键绑定添加到 XAML 代码本身,但是如何使用 Powershell 在 UI 出现时检测组合键 (Alt+F4)?
我发现使用窗体或 WPF 检测按键远远超出我的技能水平。但是,我能够使用不同的方法完成相同的任务。我需要使用 Windows 10 中的安装脚本来完成此操作。
我所做的是在脚本 运行 期间同时禁用 left/right 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,只需删除 "Scancode Map" 注册表值并再次重新启动。
我在这里找到了答案:http://www.edugeek.net/forums/windows-server-2008-r2/121900-disable-alt-f4.html
您没有可复制的示例,我不想为此编写示例,但我认为这可能会有所帮助。
$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 的 IIRC,您需要使用 System.Windows.Input
而不是 System.Windows.Forms
,因此如果您更改命名空间,您发布的第二个片段实际上可能会起作用。