The only ways I know to check if a key is pressed in Powershell are $Host.UI.RawUI.ReadKey() and [console]::ReadKey(). I want to make a script that writes in the console "1 is pressed" whenever 1 is pressed and"1 ain't pressed" whenever 1 isn't pressed. However, I am having trouble with how both of these will not give the "1 ain't pressed" output unless a non-1 key is pressed. Is there a way to check if 1 is not pressed without having to press a non-1 key? Here are the scripts I tested:
#Using $Host.UI.RawUI.ReadKey()
for(;;){
$key = $Host.UI.RawUI.ReadKey()
if($key.Character -eq '1'){
Write-Output "1 is pressed"}
else{
Write-Output "1 ain't pressed"}}
#Using [console]::ReadKey()
for(;;){
$key = [console]::ReadKey()
if ($key.Key -eq 'D1') {
Write-Output "1 is pressed"}
else{
Write-Output "1 ain't pressed"}}