N
N
NewDevLab2020-01-31 14:31:54
PowerShell
NewDevLab, 2020-01-31 14:31:54

How to navigate with arrows in PS?

I want to make an interactive script - a menu, which, in addition to 1 ... N, takes another control from the keyboard, such as arrows, etc. to move through the menu. Is there anything similar?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxKozlov, 2020-02-04
@NewDevLab

Something like that ?

function ShowMenu([array]$Menu, [int]$Default)
{
  $minY = [Console]::CursorTop
  $y = [Math]::Max([Math]::Min($Default, $Menu.Count), 0)
  do {
    [Console]::CursorTop = $minY
    [Console]::CursorLeft = 0
    $i = 0
    foreach ($item in $Menu) {
      $colors = @{
         BackgroundColor = if ($i -ne $y) { [Console]::BackgroundColor } else { 'Cyan' }
         ForegroundColor = if ($i -ne $y) { [Console]::ForegroundColor } else {' Blue' }
      }
      Write-Host (' {0}. {1} ' -f ($i+1), $item) @colors
      $i++
    }
    $k = [Console]::ReadKey()
    switch ($k.Key) {
      'UpArrow' { if ($y -gt 0) { $y-- } }
      'DownArrow' { if ($y -lt ($menu.Count - 1)) { $y++ } }
      'Enter' { return $Menu[$y] }
    }
  } while ($k.Key -notin ([ConsoleKey]::Escape, [ConsoleKey]::Enter))
}

$Menu = 'test1','text2','menu3','result4'

ShowMenu $menu 2

Reaction to other buttons can be modified to taste

E
Eugene, 2020-01-31
@BeatHazard

Try to use the instructions from this article scriptcoding.ru/2013/06/25/wscript-shell-sendkeys

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question