fix(menu): 队列窗口只认 Esc / Enter 关窗

根因:队列跑完后的等待循环只要 KeyAvailable 就 break,任意键都会把窗口关掉,
误触字母键也会让汇总一闪而过。

改动:ReadKey 后比对 VirtualKeyCode,仅 Esc(27)/Enter(13) 关窗,其它键忽略;
提示文案同步去掉「按任意键」。KeyInfo 没有 ConsoleKey 属性,只能比
VirtualKeyCode,已加注释避免再踩。

实测:PowerShell AST 解析 0 错误;用真实 KeyInfo 对象验证 27/13 命中、
字母/空格/F1 不命中;用户本机手测按键行为通过。
This commit is contained in:
ali
2026-11-20 14:51:57 +08:00
parent 0a71cac9d0
commit 04a6370b83
+8 -6
View File
@@ -229,25 +229,27 @@ function Invoke-Worker {
continue continue
} }
# 队列已空:只显示一次汇总,然后等按键关闭窗口 # 队列已空:只显示一次汇总,然后等 Esc / Enter 关窗
if (-not $summaryShown) { if (-not $summaryShown) {
Write-Host ('📊 本次共处理 {0} 项:成功 {1},取消 {2},失败 {3}' -f ` Write-Host ('📊 本次共处理 {0} 项:成功 {1},取消 {2},失败 {3}' -f `
($done + $cancelled + $failed), $done, $cancelled, $failed) -ForegroundColor Cyan ($done + $cancelled + $failed), $done, $cancelled, $failed) -ForegroundColor Cyan
Write-Host ' 按任意键(Esc / Enter)关闭窗口;期间有新任务会接着处理(2 分钟无操作自动关闭)' -ForegroundColor DarkGray Write-Host ' 按 Esc / Enter 关闭窗口;期间有新任务会接着处理(2 分钟无操作自动关闭)' -ForegroundColor DarkGray
Write-WorkerLog ("队列处理完毕:成功 {0},取消 {1},失败 {2}" -f $done, $cancelled, $failed) Write-WorkerLog ("队列处理完毕:成功 {0},取消 {1},失败 {2}" -f $done, $cancelled, $failed)
$summaryShown = $true $summaryShown = $true
$closeDeadline = (Get-Date).AddSeconds(120) $closeDeadline = (Get-Date).AddSeconds(120)
} }
$pressed = $false # 只认 Esc / Enter 关窗:其它键(含误触的字母键)一律忽略,避免窗口被顺手按掉
$closeRequested = $false
try { try {
if ($Host.UI.RawUI.KeyAvailable) { if ($Host.UI.RawUI.KeyAvailable) {
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') $key = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
$pressed = $true # KeyInfo 没有 ConsoleKey 属性,只能比 VirtualKeyCode(Esc=27 / Enter=13)
$closeRequested = $key.VirtualKeyCode -in @([int][ConsoleKey]::Escape, [int][ConsoleKey]::Enter)
} }
} }
catch { } catch { }
if ($pressed) { break } if ($closeRequested) { break }
if ($null -ne $closeDeadline -and (Get-Date) -ge $closeDeadline) { break } if ($null -ne $closeDeadline -and (Get-Date) -ge $closeDeadline) { break }
Start-Sleep -Milliseconds 150 Start-Sleep -Milliseconds 150
} }