Choice listen to spacebar

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
psychoshooter
Posts: 1
Joined: 28 Oct 2009 03:10

Choice listen to spacebar

#1 Post by psychoshooter » 28 Oct 2009 03:27

hello everyone,

Im trying to write a batch file that waits till spacebar is hit and then does a certain thing or if nothing happens in 10 seconds it goes back to start.
Now i tried to use choice but i dont know how it listens to spacebar.
Does anybody know how to use it or has another idea?

this is my code now:

Code: Select all

CHOICE /C:1{herethespacebarthen??} /N /T:1,10 press space to continue
IF ERRORLEVEL == 2 GOTO 2
IF ERRORLEVEL == 1 GOTO 1


Kind regards,
Martijn

taersious
Posts: 1
Joined: 25 Sep 2013 11:03
Location: United States
Contact:

Re: Choice listen to spacebar

#2 Post by taersious » 25 Sep 2013 11:12

You cannot. Choice will not listen for anything not in the list specified by the /c command argument besides Escape and Ctrl+Break.

Code: Select all

:start
CHOICE /C:10 /N /T:1,10 /m "Press Esc to Continue"
IF ERRORLEVEL == 2 GOTO start
IF ERRORLEVEL == 1 GOTO end
IF ERRORLEVEL == 0 GOTO ESC
:end
echo.
echo You pressed 1
goto exit
:ESC
echo.
echo You pressed Escape
:exit
echo End of program

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Choice listen to spacebar

#3 Post by jeb » 25 Sep 2013 14:50

You can't solve it with choice but with XCOPY :!:

Not obviously, but it's not necessary to understand it to use it.

Code: Select all

setlocal EnableDelayedExpansion
:waitForSpace
call :GetKey myKey
if "!myKey!" NEQ " " goto :waitForSpace
echo end
exit /b

:GetKey
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
  if not defined key set "key=%%L"
)
set "%~1=!key:~-1!"
exit /b

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Choice listen to spacebar

#4 Post by aGerman » 26 Sep 2013 13:32

@jeb

Maybe an off topic question ...
If you deviate from the defaults then usually not without a deeper meaning. What's the advantage of "USEBACKQ" in that case? Is there any risk to fail without it?

Regards
aGerman

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Choice listen to spacebar

#5 Post by jeb » 27 Sep 2013 01:34

Hi aGerman,

aGerman wrote:If you deviate from the defaults then usually not without a deeper meaning. What's the advantage of "USEBACKQ" in that case? Is there any risk to fail without it?

In this case the code is the result of a very long trail and error process, beginning with xcopy /p, multithreading pipes and so on.
I made so many different tests that the end result works, but it's neither optimized nor full stable, it's a proof of concept.
So in this case there isn't a deeper meaning :)

But I saw that carlos (or someone else) build a stable function with the xcopy technic (all characters, delayed expansion Dis/enable independent).
Sorry I can't remember where I saw this, but a small example how to use a SO: colored password input with this technic

jeb

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Choice listen to spacebar

#6 Post by aGerman » 27 Sep 2013 11:02

Thanks jeb,
it was only out of curiosity :wink:

Sorry I can't remember where I saw this

I also don't know the origin thread but we discussed the script there:
how to display password in asterisk

I thought I already posted a CHOICE emulation but I don't even remember where. So what ...

Code: Select all

@echo off &setlocal
<nul set /p "=Yes or No [yn]: "
call :choice yn
echo Option #%errorlevel% was chosen.
pause
goto :eof


:choice
setlocal DisableDelayedExpansion
set "n=0" &set "c=" &set "e=" &set "map=%~1"
if not defined map endlocal &exit /b 0
for /f "delims=" %%i in ('2^>nul xcopy /lw "%~f0" "%~f0"') do if not defined c set "c=%%i"
set "c=%c:~-1%"
if defined c (
  for /f delims^=^ eol^= %%i in ('cmd /von /u /c "echo(!map!"^|find /v ""^|findstr .') do (
    set /a "n += 1" &set "e=%%i"
    setlocal EnableDelayedExpansion
    if /i "!e!"=="!c!" (
      echo(!c!
      for /f %%j in ("!n!") do endlocal &endlocal &exit /b %%j
    )
    endlocal
  )
)
endlocal &<nul set /p "=" &goto choice

... where I hope the invisible ASCII 0x07 (beep character) next to the equal sign in the last line is working again if you copy the code out of the forum ...

Regards
aGerman

cbudin
Posts: 1
Joined: 18 May 2016 09:47

Re: Choice listen to spacebar

#7 Post by cbudin » 18 May 2016 09:52

Hi,

This post is very helpful. But I'm looking for a couple of modifications
to the :choice subroutine posted above.

What I'd like to do is be able to tell if the user hit any key on the keyboard,
and also have it time out after a set number of seconds and be able to
detect that as well (like the choice /t option).

There's some pretty complex stuff going on in that script with xcopy and
such and I'd prefer not to have to wade through it all to make what seem
like simple changes.

Can anyone help me out?

Thanks,
Clay Budin
NYC

Post Reply