0

I found using choice command is easy .. but what if i want to use it in a clock so i have a looping clock that goes on forever but at the time when i press "m" i want it to open a menu

@Echo off

:clock

::do stuff here

choice /c m /n

::Errorlevelstuff

goto :clock

but choice makes it wait .. the only solution i came up is using /t but it adds a time which i dont want "i am using another variable to set clocks tick rate" so

is there a way to check if someone pressed the key in a clock loop

xDeathwing
  • 139
  • 13

1 Answers1

1

Simple Answer

Use choice /t as your clock tick.

@echo off
setlocal
set "count=0"
:clock
echo Seconds: %count%
choice /t 1 /c am /d a >nul
if %ErrorLevel% equ 2 goto done
set /a "count+=1"
goto clock
:done
echo Menu
pause
endlocal

Note: the timeout granularity for choice is limited to seconds with a range from 1 to 9999 seconds. Also, there will be time decay using a software based clock. From a rough quick test this has about a 1/10th second per second decay. Meaning that ever 10 seconds this clock will loose 1 second.

OP's Related Posts


Decay-less Clock Example

Since my answer to your other question, I have created this clock that is not susceptible to script decay, as it adjusts based upon the OS clock every tick. Meaning, even if there is decay in the script, the clock will not display the wrong time.

@echo off
setlocal
:initialize
set "s=10"
set "m=10"
:: Note Offset
set "xs=%time:~6,1%" & set "sx=%time:~7,1%"
set "xm=%time:~3,1%" & set "mx=%time:~4,1%"
set "so=%xs:0=%%sx%" & set "mo=%xm:0=%%mx%"
echo Offset: %mo% : %so%
:process
:: Calculate Changes
set "xs=%time:~6,1%" & set "sx=%time:~7,1%"
set "xm=%time:~3,1%" & set "mx=%time:~4,1%"
set "xs=%xs:0=%" & set "xm=%xm:0=%"
if %xs%%sx% lss %so% set "xs=60+%xs%"
if %xm%%mx% lss %mo% set "xs=60+%xm%"
set /a "s=%xs%%sx%-%so%"
if %s% equ 0 set /a "m=%xm%%mx%-%mo%"
:: Pause Processing supports millisecond delay but not input checking
::ping 192.0.2.2 -n 1 -w 200 >nul
:: Break Check supports input checking but not millisecond delay
choice /T 1 /C am /D a >nul
if %ErrorLevel% equ 2 goto menu
:display
:: Detect Lag
set "w="
if %s%0 equ %bs%0 goto process
if %s%0 lss %bs%0 set "w=lag detected"
if %s%0 geq %es%0 set "w=lag detected"
:: Format Output
set "mm=00%m%"
set "ss=00%s%"
set "bm=%m%"
set "bs=%s%"
set /a "es=%s%+2"
:: Display
echo Clock: %mm:~-2% : %ss:~-2% %w%
goto process
:menu
echo Menu
endlocal
exit /b 0

Note: This is based upon en-US time format.

Output Example

Offset: 45 : 10
Clock: 00 : 01 lag detected
Clock: 00 : 02
Clock: 00 : 03
Clock: 00 : 04
Clock: 00 : 05
Clock: 00 : 06
Clock: 00 : 07
Clock: 00 : 08
Clock: 00 : 09
Clock: 00 : 11 lag detected
Clock: 00 : 12
Clock: 00 : 13
Clock: 00 : 14
Clock: 00 : 15
Clock: 00 : 16
Clock: 00 : 17
Clock: 00 : 18
Clock: 00 : 19
Clock: 00 : 20
Clock: 00 : 21
Menu
Community
  • 1
  • 1
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • Is choice more accurrate than ping/timeout ? btw the thing is i need to be able to use ms as my tick rate D: but using t=1 and adding ms to it is ok – xDeathwing Feb 25 '14 at 21:53
  • and i am using the method you gave me but sometimes it crashes while sometimes it works D: – xDeathwing Feb 25 '14 at 21:54
  • @xDeathwing Choice is worse than ping/timeout, but for a single threaded application it is the only way that I can think of at the moment to check for input during each tick. The granularity is limited to seconds for the choice command, milliseconds are not allowed. Are there any error messages to the crash? – David Ruhmann Feb 25 '14 at 22:01
  • the system can not find batch label specified :menu the thing is if i time it correctly it works .. else it stops the clock and if i press m again it breaks up or even sometimes it randomly breaks up D: – xDeathwing Feb 25 '14 at 22:03
  • thnx :) but this is not like a normal clock :P i am working on one that u can toggle how fast it is . but i will try to integrate checking from %date% .. thx for the idea again :) – xDeathwing Feb 25 '14 at 22:16