Last active
August 24, 2021 13:21
-
-
Save Kipjr/58a4ea84b1fb7d96c1bcd855757c2045 to your computer and use it in GitHub Desktop.
Powershell Key Prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Function GetKeyPress([string]$regexPattern='[0-9]', [string]$message=$null, [int]$timeOutSeconds=0){ | |
Function GetKeyPress( [string]$message=$null, [int]$timeOutSeconds=0){ | |
$key = $null | |
$Host.UI.RawUI.FlushInputBuffer() | |
if (![string]::IsNullOrEmpty($message)){ | |
Write-Host -NoNewLine $message | |
} | |
$counter = $timeOutSeconds * 1000 / 250 | |
while($key -eq $null -and ($timeOutSeconds -eq 0 -or $counter-- -gt 0)){ | |
if (($timeOutSeconds -eq 0) -or $Host.UI.RawUI.KeyAvailable){ | |
$key_ = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown,IncludeKeyUp") | |
#if ($key_.KeyDown -and $key_.Character -match $regexPattern){ | |
if ($key_.KeyDown){ | |
$key = $key_ | |
} | |
} else { | |
Write-Host -NoNewline "." | |
Start-Sleep -m 250 # Milliseconds | |
} | |
} | |
if (-not ($key -eq $null)){ | |
Write-Host -NoNewLine "$($key.Character)" | |
} | |
if (![string]::IsNullOrEmpty($message)){ | |
Write-Host "" # newline | |
} | |
#return $(if ($key -eq $null) {$null} else {$key.Character}) | |
return $(if ($key -eq $null) {$null} else {([int]$($key.Character).tostring())}) | |
} | |
$locations=@() | |
$locations+=[pscustomobject]@{id= '0';loc='C:\Temp\'} | |
$locations+=[pscustomobject]@{id= '1';loc='C:\Users\'} | |
$locations+=[pscustomobject]@{id= '2';loc='G:\'} | |
$locations | ft; | |
$key = GetKeyPress "Choose location:" 5 | |
if ($key -eq $null){ | |
set-location -path $($locations[0].loc) | |
} else { | |
set-location -path $($locations[$key].loc) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment