Skip to content

Ctrl+C can't be intercepted by PowerShell for shutdown #1894

@tillig

Description

@tillig

Environment

Windows build number: 10.0.18362.175
Windows Terminal version: 0.2.1831.0
PowerShell Core 6.2.1
PowerShell 5.1

I'm on a Microsoft Surface Book. There is no Pause key and no Break key. Ctrl+Break is not an option for stopping a program unless I plug in an external keyboard or do some sort of key remapping specifically for this.

Steps to reproduce

Create a script in PowerShell that intercepts Ctrl+C so it can gracefully shut down. Run that script and try to use Ctrl+C. The script can't intercept it.

It behaves the same in PowerShell Core and in PowerShell.

[Console]::TreatControlCAsInput = $True
Start-Sleep -Seconds 1
$Host.UI.RawUI.FlushInputBuffer()
$exiting = $false
while ((-Not $exiting)) {
    Write-Host "Sleeping..."
    Start-Sleep -Seconds 1
    while ($Host.UI.RawUI.KeyAvailable -and ($Key = $Host.UI.RawUI.ReadKey("AllowCtrlC,NoEcho,IncludeKeyUp"))) {
        If ([Int]$Key.Character -eq 3) {
            Write-Warning "Got CTRL-C!"
            $exiting = $true
        }

        $Host.UI.RawUI.FlushInputBuffer()
    }
}
Write-Host "Done."
[Console]::TreatControlCAsInput = $False

Expected behavior

I should be able to hit Ctrl+C to stop the program. In a standard PowerShell terminal (not hosted in the new Microsoft Terminal) this works fine.

Actual behavior

The program pauses, ostensibly to allow copy/paste behavior to work, and if I hit Enter it continues running. There is no way to capture Ctrl+C.

Metadata

Metadata

Assignees

Labels

Area-InputRelated to input processing (key presses, mouse, etc.)Issue-BugIt either shouldn't be doing this or needs an investigation.Priority-2A description (P2)Product-ConptyFor console issues specifically related to conptyResolution-Fix-CommittedFix is checked in, but it might be 3-4 weeks until a release.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions