Unable to read keypresses using ($host.UI.RawUI.ReadKey('NoEcho,IncludeKeyUp')).character #13269

Closed
opened 2026-01-31 03:38:17 +00:00 by claunia · 3 comments
Owner

Originally created by @jameswylde on GitHub (Apr 1, 2021).

Windows Terminal version (or Windows build number)

10.0.18363.0
Latest WT and WT preview

Other Software

Powershell 7.1.3
Powershell 5.1.18362.1171

Tested on both current WT and WT Preview, same results.

Steps to reproduce

Simple TUI to demonstrate:

do
{
     Show-Menu
     $keyPress = ($host.UI.RawUI.ReadKey('NoEcho,IncludeKeyUp')).character
     switch ($keyPress)
     {
           '1' {
                Clear-Host
                functionHere
           } 'Q' {
                return
           }
     }
     pause
}
until ($keyPress -eq 'q')

Expected Behavior

Powershell to progress and ReadKey from input.

Actual Behavior

Input is detected (writes into terminal) however on enter disappears and no action is executed.

Working fine in normal PS shells (version numbers above).

Originally created by @jameswylde on GitHub (Apr 1, 2021). ### Windows Terminal version (or Windows build number) 10.0.18363.0 Latest WT and WT preview ### Other Software Powershell 7.1.3 Powershell 5.1.18362.1171 Tested on both current WT and WT Preview, same results. ### Steps to reproduce Simple TUI to demonstrate: ``` do { Show-Menu $keyPress = ($host.UI.RawUI.ReadKey('NoEcho,IncludeKeyUp')).character switch ($keyPress) { '1' { Clear-Host functionHere } 'Q' { return } } pause } until ($keyPress -eq 'q') ``` ### Expected Behavior Powershell to progress and ReadKey from input. ### Actual Behavior Input is detected (writes into terminal) however on enter disappears and no action is executed. Working fine in normal PS shells (version numbers above).
claunia added the Resolution-By-DesignNeeds-TriageNeeds-Tag-FixNeeds-Attention labels 2026-01-31 03:38:18 +00:00
Author
Owner

@DHowett commented on GitHub (Apr 13, 2021):

Thanks for the report! I don't have a Show-Menu commandlet -- can you point me to a module? Did you author this yourself?

@DHowett commented on GitHub (Apr 13, 2021): Thanks for the report! I don't have a `Show-Menu` commandlet -- can you point me to a module? Did you author this yourself?
Author
Owner

@jameswylde commented on GitHub (Apr 13, 2021):

Apologies Dustin, that's actually a function I named whilst quickly whipping up the above to demonstrate the issue - poorly named. Why I used a cmdlet naming convention I have no idea.

So Show-Menu would simply write to host something like:

function Show-Menu
{
     param (
           [string]$Title = "Title"
     )
     Clear-Host
     Write-Host "Option 1"
     Write-Host "Option 2"
}

Apologies I didn't include, I didn't think it relevant. Let me know if you need anything else.

@jameswylde commented on GitHub (Apr 13, 2021): Apologies Dustin, that's actually a function I named whilst quickly whipping up the above to demonstrate the issue - poorly named. Why I used a cmdlet naming convention I have no idea. So Show-Menu would simply write to host something like: ``` function Show-Menu { param ( [string]$Title = "Title" ) Clear-Host Write-Host "Option 1" Write-Host "Option 2" } ``` Apologies I didn't include, I didn't think it relevant. Let me know if you need anything else.
Author
Owner

@DHowett commented on GitHub (Apr 13, 2021):

Okay, so I think this is another case of ... well, this is pretty curious. When you press enter, we stuff an enter DOWN and an enter UP key into the input buffer. Because you're accepting a read on Key Up, you're immediately getting the "enter key up" event that you get from either pressing enter on the prompt line or enter after pause. If you press enter slowly in the traditional console, you can see this as well.

Recommendations are to either flush input (which I am not sure how to do from PS or .NET), or to not read key up events. They're relatively uncommonly used as far as we can tell. 😄

Marking this by-design. There's a couple other issues floating around for this that I may come back and deduplicate with this one.

Cheers!

@DHowett commented on GitHub (Apr 13, 2021): Okay, so I think this is another case of ... well, this is pretty curious. When you press enter, we stuff an enter DOWN and an enter UP key into the input buffer. Because you're accepting a read on Key Up, you're immediately getting the "enter key up" event that you get from either pressing enter on the prompt line or enter after `pause`. If you press enter slowly in the traditional console, you can see this as well. Recommendations are to either flush input (which I am not sure how to do from PS or .NET), or to not read key _up_ events. They're relatively uncommonly used as far as we can tell. :smile: Marking this by-design. There's a couple other issues floating around for this that I may come back and deduplicate with this one. Cheers!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#13269