Answer the question
In order to leave comments, you need to log in
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question