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 }