[console]::TreatControlCAsInput or $Host.UI.RawUI.ReadKeynot working correctly in new terminal #3948

Closed
opened 2026-01-30 23:33:59 +00:00 by claunia · 1 comment
Owner

Originally created by @tonyt73 on GitHub (Sep 20, 2019).

Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.18362.0 Microsoft Windows NT 10.0.18362.0

Steps to reproduce

Test-CTRLC.ps1

[console]::TreatControlCAsInput = $true
Start-Sleep -Seconds 1
$Host.UI.RawUI.FlushInputBuffer()
Do {
    if ($Host.UI.RawUI.KeyAvailable) {
        $Key = [int]$Host.UI.RawUI.ReadKey("AllowCtrlC,IncludeKeyUp,NoEcho").Character
        Write-Host "Key available - $Key"
        if (3 -eq $Key -or  '  ' -eq $Key) {
            [console]::TreatControlCAsInput = $false
            Write-Host "Ctrl-C detected, exiting script..." -ForegroundColor Yellow
            Break
        }
        #$Host.UI.RawUI.FlushInputBuffer()
    }
} While ($true)

Expected behavior

Ctrl+C should be readable from the $Host.UI.RawUI.KeyAvailable and $Host.UI.RawUI.ReadKey functions

Actual behavior

[console]::TreatControlCAsInput = $true Does turn off Ctrl+C
Either $Host.UI.RawUI.KeyAvailable or $Host.UI.RawUI.ReadKey is not working correctly

What works

Start PowerShell console outside of Windows Terminal. aka. standard powershell console window
Run the above style of code and Ctrl+C is detected

Images

PowerShell 6 (x64) Console (works)

image

PowerShell 7 in Windows Terminal

image

Originally created by @tonyt73 on GitHub (Sep 20, 2019). ``` Platform ServicePack Version VersionString -------- ----------- ------- ------------- Win32NT 10.0.18362.0 Microsoft Windows NT 10.0.18362.0 ``` # Steps to reproduce ## Test-CTRLC.ps1 ``` [console]::TreatControlCAsInput = $true Start-Sleep -Seconds 1 $Host.UI.RawUI.FlushInputBuffer() Do { if ($Host.UI.RawUI.KeyAvailable) { $Key = [int]$Host.UI.RawUI.ReadKey("AllowCtrlC,IncludeKeyUp,NoEcho").Character Write-Host "Key available - $Key" if (3 -eq $Key -or ' ' -eq $Key) { [console]::TreatControlCAsInput = $false Write-Host "Ctrl-C detected, exiting script..." -ForegroundColor Yellow Break } #$Host.UI.RawUI.FlushInputBuffer() } } While ($true) ``` # Expected behavior Ctrl+C should be readable from the `$Host.UI.RawUI.KeyAvailable` and `$Host.UI.RawUI.ReadKey` functions # Actual behavior `[console]::TreatControlCAsInput = $true` Does turn off Ctrl+C Either `$Host.UI.RawUI.KeyAvailable` or `$Host.UI.RawUI.ReadKey` is not working correctly # What works Start PowerShell console outside of Windows Terminal. aka. standard powershell console window Run the above style of code and Ctrl+C is detected # Images ## PowerShell 6 (x64) Console (works) ![image](https://user-images.githubusercontent.com/21229341/65292478-f5651600-db9a-11e9-8677-b764c98a3957.png) ## PowerShell 7 in Windows Terminal ![image](https://user-images.githubusercontent.com/21229341/65292520-19c0f280-db9b-11e9-97f2-a16ca2b3ee84.png)
claunia added the Needs-TriageResolution-Duplicate labels 2026-01-30 23:33:59 +00:00
Author
Owner

@DHowett-MSFT commented on GitHub (Sep 23, 2019):

Looks like a duplicate of #1894.

@DHowett-MSFT commented on GitHub (Sep 23, 2019): Looks like a duplicate of #1894.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#3948