Parsonage
Parsonage

Reputation: 1

trying to write if, else statement and detect keypress

Hi I'm trying to write a batch file that when it gets to that area of the code waits for 10 seconds and then if a certain key is pressed exits, otherwise it goes to another area of the code. Here is what I got so far

SLEEP 10
IF
exit
else if
goto start

sorry, I don't know if this is correct. I'm just learning Lua and while similar to DOS they aren't quite the same. If anyone can fill in the gaps and fix the mistakes I would much appreciate it. The key I want to be pressed is either any or a specific key ID which

Upvotes: 0

Views: 4529

Answers (1)

Bali C
Bali C

Reputation: 31231

You can do this in batch using this

@echo off
choice /c abcd /n /t 5 /d d
if %errorlevel%==1 echo You chose a
if %errorlevel%==2 goto :CONTINUE
if %errorlevel%==3 echo You chose c
if %errorlevel%==4 exit >nul

:CONTINUE
REM Continue code
pause >nul

Usage:

In this script your options are a, b, c, and d.

Use the %errorlevel% with incrementing numbers to get the choice selected.

The /t switch is the timeout in seconds, in this it is 5 seconds.

The /d switch is the default option, use this to automatically make a choice if the command times out. In this case d will be the time out choice, which will exit the script.

Just tweak to fit your needs.

Upvotes: 1

Related Questions