The timeout command blocks the W key from closing CMD window

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

The timeout command blocks the W key from closing CMD window

#1 Post by DOSadnie » 18 Nov 2023 11:26

As pointed out in this discussion viewtopic.php?f=3&t=10897&p=69149#p69008 apparently the timeout command makes it impossible to press any key to close CMD window, because the W is exempted from that rule. Here is basic example

Code: Select all

@echo off
echo Pressing of any key will close this window - except when you press W

timeout /t 10 | findstr /R ".0$" > nul
&& goto continue || exit

command > nul 2>&1
:continue
How could this be happening - and more importantly: how to fix / workaround this issue?

ShadowThief
Expert
Posts: 1162
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: The timeout command blocks the W key from closing CMD window

#2 Post by ShadowThief » 18 Nov 2023 19:25

How could this be happening
It's obviously a bug in the code. Nobody here is capable of elaborating further because Windows is closed source.
more importantly: how to fix / workaround this issue?
By pressing one of the 100+ other keys on the keyboard. This is such a non-issue that I'm surprised you bothered to make a second topic about it after nobody thought it was interesting enough to post in your original topic. Feel free to reach out to Microsoft if this is genuinely impeding your workflow, but you are literally the only person I've ever seen complain about this, so don't expect it to get any meaningful priority.

koko
Posts: 38
Joined: 13 Oct 2016 00:40

Re: The timeout command blocks the W key from closing CMD window

#3 Post by koko » 21 Nov 2023 17:19

Edit: sorry, overlooked the intent with the timeout tied to the key detection interrupt. Seems indeed any 'w' input from a command to the right of the timeout pipe is eaten, even if handled with something else like Powershell.

Prior post below, edited a little:

---

There's a Powershell way to detect all keypresses, even modifier keys, which I've used before in batch scripts to handle things like choices and/or exiting scripts (I assume you mean not when a process is running since that would be canceled via Ctrl+C).

Example using a CHOICE-like prompt where certain keys are given separate handling, while any other key will exit. To change it to make any key exit the call argument can be removed.

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion

<nul set /p "=Press A/B/C to pause or any other key to exit..."

:: Remove call arguments to disable separate key handling
call :choice "a b c"
if !errorlevel! equ 0 (
    exit
    ) else (
    if !errorlevel! equ 1 echo "Pressed a"
    if !errorlevel! equ 2 echo "Pressed b"
    if !errorlevel! equ 3 echo "Pressed c"
    pause
    )

exit

:choice [choices]
    powershell -Command "$Key = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown').Character ; $Choices = '%1' ; $ErrorLevel = 0 ; foreach ( $_ in $Choices.split() ){ $i++ ; if ( $Key -eq $_ ){ $ErrorLevel = $i } } ; exit($ErrorLevel)"
    exit /b

endlocal

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: The timeout command blocks the W key from closing CMD window

#4 Post by DOSadnie » 09 Dec 2023 09:27

ShadowThief wrote:
18 Nov 2023 19:25
How could this be happening
It's obviously a bug in the code
[...]
Feel free to reach out to Microsoft
[...]
I am unable to do so because when trying to create Microsoft Account nothing happens after I enter the verification code from an e-mail I receive [i.e. there is no next step to be chosen / directed to, just a "Create account" text is shown which is not a link]; while logging in with my GitHub account is impossible due to it using e-mail address that is "a work or school account. You can't use this GitHub account to sign in."]

So once again Microsoft gets another trophy for stupidity

Post Reply