Skip to content

ReadKey() functions misread arrow keys as ESC on 7.2.0 / Linux #16443

Closed

Description

@oranki

Prerequisites

Steps to reproduce

After upgrading from 7.1.5 to 7.2.0 on Linux, both $Host.UI.RawUI.ReadKey() and [Console]::ReadKey() functions return keycode 27 (ESC) when any of the arrow keys are pressed.

Expected behavior

$host.UI.RawUI.ReadKey()

VirtualKeyCode Character ControlKeyState KeyDown
-------------- --------- --------------- -------
            37                        0    True

When arrow key left is pressed; other arrows produce codes 38,39,40

Actual behavior

---clip---
$host.UI.RawUI.ReadKey()

VirtualKeyCode Character ControlKeyState KeyDown
-------------- --------- --------------- -------
            27                        0    True
---clap---
When any of the four arrow keys is pressed, result is always above.

Additionally, character 'A' is typed on next cmdline when up arrow is pressed, other arrows type chars C,B,D:

---clip---
PS > $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

VirtualKeyCode Character ControlKeyState KeyDown
-------------- --------- --------------- -------
            27         [0m               0    True

PS > A
---clap---
Pressing enter produces result as if I had typed the 'A' myself:

---clip---
A: The term 'A' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
---clap---

ReadKey() argument "NoEcho" has no effect on above behaviour.

Finally, the problem is not reproducible when running a powershell script from another shell. Executing script below from
either bash or zsh produces correct result, but executing from pwsh does not.

---clip---
#!/usr/bin/pwsh
$host.UI.RawUI.ReadKey()
---clap---

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.0
PSEdition                      Core
GitCommitId                    7.2.0
OS                             Linux 5.10.0-8-amd64 #1 SMP Debian 5.10.46-4 (2021-08-03)
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Nov 11, 2021
  2. added
    Issue-Question
    WG-Engine
    on Nov 11, 2021
  3. added
    WG-Interactive-Console
    and removed
    WG-Engine
    on Nov 12, 2021
  4. ScoutmanPt commented on Jan 24, 2022

    @ScoutmanPt

    Howdy , any update on this?

  5. oranki commented on Mar 2, 2022

    @oranki
    Author

    Issue is still present in version 7.2.1.

  6. mark-hubers commented on May 21, 2022

    @mark-hubers

    Still a problem in version 7.2.4.

  7. mark-hubers commented on May 23, 2022

    @mark-hubers

    I cannot wait for this so I made a hack around this problem for now. Hope it help others as I need to be able to use up and down arrow on Linux/Mac for a console menu.

    $CurrentHost = Get-Host
    $key1 = $CurrentHost.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    
    ## A bug with Linux and Mac where arrow keys are return in 2 chars.  First is esc follow by A,B,C,D
    if ($key1.VirtualKeyCode -eq 0x1B) {
       ## Found that we got an esc chair so we need to grab one more char
       $key2 = $CurrentHost.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    
       ## We just care about up and down arrow mapping here for now.
       if ($key2.VirtualKeyCode -eq 0x41) {
          # VK_UP = 0x26 up-arrow
          $key1.VirtualKeyCode = 0x26
       }
       if ($key2.VirtualKeyCode -eq 0x42) {
          # VK_DOWN = 0x28 down-arrow
          $key1.VirtualKeyCode = 0x28
       }
    }
    
    $key1
    
  8. daxian-dbw commented on Sep 8, 2022

    @daxian-dbw
    Member

    Opened dotnet/runtime#75305 to ask for insights and clarification.

  9. daxian-dbw commented on Sep 16, 2022

    @daxian-dbw
    Member

    This was resolved in .NET 7-RC.1 release. The screenshot below is PowerShell built against .NET 7-RC.1 running on Ubuntu WSL2.

    image

  10. ghost removed
    Needs-Triage
    on Sep 16, 2022
  11. added
    Resolution-External
    and removed
    Issue-Question
    on Sep 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-ExternalWG-Interactive-Console

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions