Skip to content

Commit

Permalink
Read up/down keys on *nix properly - fixes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebazzz committed May 25, 2022
1 parent d170bce commit 8e27159
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions PSMenu/Private/Read-VKey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ function Read-VKey() {
$ErrMsg = "Current host '$CurrentHost' does not support operation 'ReadKey'"

try {
# Issues with reading up and down arrow keys
# - https://github.com/PowerShell/PowerShell/issues/16443
# - https://github.com/dotnet/runtime/issues/63387
# - https://github.com/PowerShell/PowerShell/issues/16606
if ($IsLinux -or $IsMacOS) {
## A bug with Linux and Mac where arrow keys are return in 2 chars. First is esc follow by A,B,C,D
$key1 = $CurrentHost.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

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
}
}
Return $key1
}

Return $CurrentHost.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
catch [System.NotSupportedException] {
Expand Down

0 comments on commit 8e27159

Please sign in to comment.