<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.18362.0 Microsoft Windows NT 10.0.18362.0"><pre class="notranslate"><code class="notranslate">Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.18362.0 Microsoft Windows NT 10.0.18362.0
</code></pre></div>
<h1 dir="auto">Steps to reproduce</h1>
<h2 dir="auto">Test-CTRLC.ps1</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[console]::TreatControlCAsInput = $true
Start-Sleep -Seconds 1
$Host.UI.RawUI.FlushInputBuffer()
Do {
    if ($Host.UI.RawUI.KeyAvailable) {
        $Key = [int]$Host.UI.RawUI.ReadKey(&quot;AllowCtrlC,IncludeKeyUp,NoEcho&quot;).Character
        Write-Host &quot;Key available - $Key&quot;
        if (3 -eq $Key -or  '  ' -eq $Key) {
            [console]::TreatControlCAsInput = $false
            Write-Host &quot;Ctrl-C detected, exiting script...&quot; -ForegroundColor Yellow
            Break
        }
        #$Host.UI.RawUI.FlushInputBuffer()
    }
} While ($true)
"><pre class="notranslate"><code class="notranslate">[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)

</code></pre></div>
<h1 dir="auto">Expected behavior</h1>
<p dir="auto">Ctrl+C should be readable from the <code class="notranslate">$Host.UI.RawUI.KeyAvailable</code> and <code class="notranslate">$Host.UI.RawUI.ReadKey</code> functions</p>
<h1 dir="auto">Actual behavior</h1>
<p dir="auto"><code class="notranslate">[console]::TreatControlCAsInput = $true</code> Does turn off Ctrl+C<br>
Either <code class="notranslate">$Host.UI.RawUI.KeyAvailable</code> or <code class="notranslate">$Host.UI.RawUI.ReadKey</code> is not working correctly</p>
<h1 dir="auto">What works</h1>
<p dir="auto">Start PowerShell console outside of Windows Terminal. aka. standard powershell console window<br>
Run the above style of code and Ctrl+C is detected</p>
<h1 dir="auto">Images</h1>
<h2 dir="auto">PowerShell 6 (x64) Console (works)</h2>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/21229341/65292478-f5651600-db9a-11e9-8677-b764c98a3957.png"><img src="https://user-images.githubusercontent.com/21229341/65292478-f5651600-db9a-11e9-8677-b764c98a3957.png" alt="image" style="max-width: 100%;"></a></p>
<h2 dir="auto">PowerShell 7 in Windows Terminal</h2>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/21229341/65292520-19c0f280-db9b-11e9-97f2-a16ca2b3ee84.png"><img src="https://user-images.githubusercontent.com/21229341/65292520-19c0f280-db9b-11e9-97f2-a16ca2b3ee84.png" alt="image" style="max-width: 100%;"></a></p>
<h1 dir="auto">Environment</h1>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Windows build number: 10.0.18362.175
Windows Terminal version: 0.2.1831.0
PowerShell Core 6.2.1
PowerShell 5.1"><pre lang="none" class="notranslate"><code class="notranslate">Windows build number: 10.0.18362.175
Windows Terminal version: 0.2.1831.0
PowerShell Core 6.2.1
PowerShell 5.1
</code></pre></div>
<p dir="auto">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.</p>
<h1 dir="auto">Steps to reproduce</h1>
<p dir="auto">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.</p>
<p dir="auto">It behaves the same in PowerShell Core and in PowerShell.</p>
<div class="highlight highlight-source-powershell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="[Console]::TreatControlCAsInput = $True
Start-Sleep -Seconds 1
$Host.UI.RawUI.FlushInputBuffer()
$exiting = $false
while ((-Not $exiting)) {
    Write-Host &quot;Sleeping...&quot;
    Start-Sleep -Seconds 1
    while ($Host.UI.RawUI.KeyAvailable -and ($Key = $Host.UI.RawUI.ReadKey(&quot;AllowCtrlC,NoEcho,IncludeKeyUp&quot;))) {
        If ([Int]$Key.Character -eq 3) {
            Write-Warning &quot;Got CTRL-C!&quot;
            $exiting = $true
        }

        $Host.UI.RawUI.FlushInputBuffer()
    }
}
Write-Host &quot;Done.&quot;
[Console]::TreatControlCAsInput = $False"><pre class="notranslate">[<span class="pl-k">Console</span>]::TreatControlCAsInput <span class="pl-k">=</span> <span class="pl-c1">$True</span>
<span class="pl-c1">Start-Sleep</span> <span class="pl-k">-</span>Seconds <span class="pl-c1">1</span>
<span class="pl-c1">$Host<span class="pl-smi">.UI.RawUI.FlushInputBuffer</span></span>()
<span class="pl-smi">$exiting</span> <span class="pl-k">=</span> <span class="pl-c1">$false</span>
<span class="pl-k">while</span> ((<span class="pl-k">-Not</span> <span class="pl-smi">$exiting</span>)) {
    <span class="pl-c1">Write-Host</span> <span class="pl-s"><span class="pl-pds">"</span>Sleeping...<span class="pl-pds">"</span></span>
    <span class="pl-c1">Start-Sleep</span> <span class="pl-k">-</span>Seconds <span class="pl-c1">1</span>
    <span class="pl-k">while</span> (<span class="pl-c1">$Host<span class="pl-smi">.UI.RawUI.KeyAvailable</span></span> <span class="pl-k">-and</span> (<span class="pl-smi">$Key</span> <span class="pl-k">=</span> <span class="pl-c1">$Host<span class="pl-smi">.UI.RawUI.ReadKey</span></span>(<span class="pl-s"><span class="pl-pds">"</span>AllowCtrlC,NoEcho,IncludeKeyUp<span class="pl-pds">"</span></span>))) {
        <span class="pl-k">If</span> ([<span class="pl-k">Int</span>]<span class="pl-smi">$Key<span class="pl-smi">.Character</span></span> <span class="pl-k">-eq</span> <span class="pl-c1">3</span>) {
            <span class="pl-c1">Write-Warning</span> <span class="pl-s"><span class="pl-pds">"</span>Got CTRL-C!<span class="pl-pds">"</span></span>
            <span class="pl-smi">$exiting</span> <span class="pl-k">=</span> <span class="pl-c1">$true</span>
        }

        <span class="pl-c1">$Host<span class="pl-smi">.UI.RawUI.FlushInputBuffer</span></span>()
    }
}
<span class="pl-c1">Write-Host</span> <span class="pl-s"><span class="pl-pds">"</span>Done.<span class="pl-pds">"</span></span>
[<span class="pl-k">Console</span>]::TreatControlCAsInput <span class="pl-k">=</span> <span class="pl-c1">$False</span></pre></div>
<h1 dir="auto">Expected behavior</h1>
<p dir="auto">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.</p>
<h1 dir="auto">Actual behavior</h1>
<p dir="auto">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.</p>