From 04a6370b83587aff5b8c2a9f4153567cb9a87019 Mon Sep 17 00:00:00 2001 From: ali Date: Fri, 20 Nov 2026 14:51:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(menu):=20=E9=98=9F=E5=88=97=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E5=8F=AA=E8=AE=A4=20Esc=20/=20Enter=20=E5=85=B3?= =?UTF-8?q?=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因:队列跑完后的等待循环只要 KeyAvailable 就 break,任意键都会把窗口关掉, 误触字母键也会让汇总一闪而过。 改动:ReadKey 后比对 VirtualKeyCode,仅 Esc(27)/Enter(13) 关窗,其它键忽略; 提示文案同步去掉「按任意键」。KeyInfo 没有 ConsoleKey 属性,只能比 VirtualKeyCode,已加注释避免再踩。 实测:PowerShell AST 解析 0 错误;用真实 KeyInfo 对象验证 27/13 命中、 字母/空格/F1 不命中;用户本机手测按键行为通过。 --- scripts/explorer-media-rename.ps1 | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/scripts/explorer-media-rename.ps1 b/scripts/explorer-media-rename.ps1 index 627ee1b..c005d5e 100644 --- a/scripts/explorer-media-rename.ps1 +++ b/scripts/explorer-media-rename.ps1 @@ -229,25 +229,27 @@ function Invoke-Worker { continue } - # 队列已空:只显示一次汇总,然后等按键关闭窗口 + # 队列已空:只显示一次汇总,然后等 Esc / Enter 关窗 if (-not $summaryShown) { Write-Host ('📊 本次共处理 {0} 项:成功 {1},取消 {2},失败 {3}' -f ` ($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) $summaryShown = $true $closeDeadline = (Get-Date).AddSeconds(120) } - $pressed = $false + # 只认 Esc / Enter 关窗:其它键(含误触的字母键)一律忽略,避免窗口被顺手按掉 + $closeRequested = $false try { if ($Host.UI.RawUI.KeyAvailable) { - $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') - $pressed = $true + $key = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') + # KeyInfo 没有 ConsoleKey 属性,只能比 VirtualKeyCode(Esc=27 / Enter=13) + $closeRequested = $key.VirtualKeyCode -in @([int][ConsoleKey]::Escape, [int][ConsoleKey]::Enter) } } catch { } - if ($pressed) { break } + if ($closeRequested) { break } if ($null -ne $closeDeadline -and (Get-Date) -ge $closeDeadline) { break } Start-Sleep -Milliseconds 150 }