How do I set a key binding in batch - batch-file

So I'm making a adventure game in .bat, and I want it so that when the player presses - it takes him to the Map frame and when he presses = the player goes to the Journal. I already worked out a way to return to the frame that the player was originally in, but I need to know how to set up key bindings.
Key bindings are a key that you press at anytime, and when you press it something happens, like you go to a map or teleport.

As stated in the comments your best bet is to use choice. The main setup for choice is:
#choice [/c:] [/n] [/t /d ] [/m <"Text">]
The /c attribute lists all the available choices (i.e /c:123) would allow 1, 2 or 3 to be pressed.
The /n specifies not to display the inputted choices. (i.e the choices "123" would show up in console as [123]?) For [123]? to be shown leave out the /n
/t sets the time before a default choice is made
/d sets the default choice to be made after /t seconds
/m sets text to be displayed in the console window
For Example:
#ECHO off
#CHOICE /c:123 /n /t 100 /d 1 /m "TextGoesHere"
if ERRORLEVEL 3 GOTO three
if ERRORLEVEL 2 GOTO two
if ERRORLEVEL 1 GOTO one
goto end
:one
echo You pressed 1!
echo This is also the default choice after 100 seconds
goto end
:two
echo You pressed 2!
goto end
:three
echo You pressed 3!
:end
pause
Would display:
TextGoesHere [1,2,3]?
And if 2 was pressed:
TextGoesHere [1,2,3]?
You have pressed "2"!
press any key to continue...
And of course you could add code to these sections instead of just displaying text.
For more info about "choice" click here

Related

Specific Keystroke for batch

I am trying to find out how to make batch wait for a specific keystroke. I don't want any of those pause>nul echo Press Enter to continue... None of that stuff, I would like it so that way once you press a key (eg. a) then it will go to :Test, if you press another key then it won't react what so ever. And I looked at the first page result, and went to the links on Google.
I believe the solution to your problem is the CHOICE command. This will allow you to set a key, have the batch script wait for that key to be pressed, then continue. It also avoids any cryptic pausing or loops in the script.
Keep in mind that the choice command has a few syntax's
/C - Specifies the list of choices to be created.
/N - Hides the list of choices in the prompt.
/CS- Enables case-sensitive choices to be selected.
/T - The number of seconds to pause before a default choice is made.
/D - Specifies the default choice after timeout seconds.
/M - Specifies the message to be displayed before the prompt.
For the sake of your usage, the following script can be used. Its also important to note you can have multiple keys act as your specific keystroke by doing ex: 12345 for SET KEY=
#ECHO OFF
::Change this key to what ever you wish.
SET KEY=N
:Start
cls
echo Press %KEY% to continue...
choice /c %KEY% /n /cs
if %errorlevel%==1 goto Continue
:Continue
cls
echo Hello World!
pause.

batch file - 'The Syntax of the command is incorrect'

I've been making a text-adventure, but towards the end of my coding it came up with this error. Every time I try to continue from scene 5 to scene 5.1 it goes to a different scene and skips through it. Help?
Here is my code:
cls
echo You walk in through the doorframe, and into the dark room. You can't see a thing in the gloom, but when you put your hand on the wall you recognise the shape of a lightswitch.
echo.
pause
goto scene5.1
cls
echo You walk in through the doorframe, and into the dark room. You can't see a thing in the gloom, but when you put your hand on the wall you recognise the shape of a lightswitch.
echo.
pause
goto scene5.1
:scene5.1
cls
echo You flip the lightswitch down, and slowly the glowing bulbs switch on; illuminating the living room. In the gradually increasing light you see a table in the corner, and a large sofa that streches across the back wall. A few metres in front of the couch is a fireplace.
echo.
echo 1) Table
echo 2) Sofa
echo 3) Fireplace
echo 4) Go back
set /p type
if %type%==1 goto scene5.1.1
if %type%==2 goto scene5.1.2
if %type%==3 goto scene5.1.3
if %type%==4 goto scene5.1.4
C:\Windows\system32>echo 4) Go back
4) Go back
C:\Windows\system32>set /p type
The syntax of the command is incorrect.
goto was unexpected at this time.
It's probably the lack of an equals sign in set /p (well I know it is).
PS: Set /p var = PromptText is not the best command. It doesn't prevent wrong entry, and you don't check for it. Use Choice command instead. It only takes allowed entry.
The reason most examples use set /p is because choice was dropped from one windows version 15 years ago. That was along time ago.
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List:
/C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
As help says use errorlevels in descending order
if errorlevel 4 dosomething
if errorlevel 3 dosomething
if errorlevel 2 dosomething
if errorlevel 1 dosomething
if errorlevel 0 UserPressedCtrl+C
or in any order
if errorlevel 1 if not errorlevel 2 dosomething for 1
Do not use %errorlevel% as it can be overwritten by other programs.
#ECHO OFF
SETLOCAL
:scene4.6.1
:scene5.1
SET "scene=5.1"
cls
echo You flip the lightswitch down, and slowly the glowing bulbs switch on; illuminating the living room. In the gradually increasing light you see a table in the corner, and a large sofa that streches across the back wall. A few metres in front of the couch is a fireplace.
echo.
CALL :addchoice Table Sofa Fireplace "Go back"
GOTO makechoice
:scene5.1.1
ECHO AT table
goto :eof
:scene5.1.2
ECHO AT sofa
goto :eof
:scene5.1.3
ECHO AT fireplace
goto :eof
:scene5.1.4
:scene4.6
SET "scene=4.6"
echo You are IN a dank corridor dimly lit by sputtering torches
echo.
CALL :addchoice Doorway Corridor
GOTO makechoice
:makechoice
CHOICE /C %choices%
SET "scene=%scene%.%errorlevel%"
SET /a choicecount=0
SET "choices="
GOTO scene%scene%
:addchoice
SET /a choicecount+=1
ECHO %choicecount%) %~1
SET "choices=%choices%%choicecount%"
SHIFT
IF "%~1" neq "" GOTO addchoice
GOTO :EOF
This may save you a heap of programming. Please do not accept it as an answer since bgalea's answer actually answers your question. This is just an approach to make the construct-an-adventure exercise faster and easier.
For each scene, describe the scene and call :addchoice with parameters of the available choices. If a choice is multiple words, "enclose them in quotes".
The routine assigns 1 to the first choice in the list, 2 to the next and so on. The available choices are then recorded in choices.
go to makechoice. This prompts for an entry using the list in choices. Then append dot+the errorlevel of the response to the current scene number, clear the choices and goto scene+the calculated scene number.
Note that you can use an empy command-sequence to move between scenes by using the structure like in scene5.1.4 to scene4.6 (iow, scene 5.1+choice4 moves to 4.6)
This way, your response-sets become one line after your scene description and you never need to use a series of if-commands to move between scenes.

Batch file with choosing Y/N

Please help, I'm a beginner and I need to create batch-file to delete log-files. But before delete I need to ask user to delete the last log-file or all log-files.
I tried to implement it like:
set /P PARAM_VALUE=Remove only the last log file?(Y/N)?
if /I "%PARAM_VALUE%"=="y" goto DeleteLast
if /I "%PARAM_VALUE%"=="n" goto DeleteAll
But in this case I can enter any values, I need to avoid an others character except Y,y,N,n.
Thx.
One of solutionÑ– in this case is using the CHOICE command.
This tool allows users to select one item from a list of choices and returns the index of the selected choice.
Syntax: CHOICE [ /C choices ] [ /N ] [ /CS ] [ /T timeout /D choice ] [ /M text ]
Parameter List:
/C choices Specifies the list of choices to be created. Default list for English versions is YN
/N Hides the list of choices in the prompt. The message before the prompt is displayed and the choices are still enabled.
/CS Enables case-sensitive choices to be selected. By default, the utility is case-insensitive. Note: DOS and NT Resource Kit versions use /S instead
/T timeout The number of seconds to pause before a default choice
is made. Acceptable values are from 0 to 9999.
If 0 is specified, there will be no pause and the default choice is selected.
Note: DOS and NT Resource Kit versions use /T:default,timeout instead.
/D default Specifies the default choice after timeout seconds.
Character must be in the set of choices specified by /C option and must also specify timeout with /T.
Note: DOS and NT Resource Kit versions use /T:default,timeout instead.
/M text Specifies the message to be displayed before the prompt.
If not specified, the utility displays only a prompt.
For your problem try to use something like:
choice /C:YN /M:"Remove only the last log file? [YN]"
IF ERRORLEVEL ==1 GOTO DeleteLast
IF ERRORLEVEL ==2 GOTO DeleteAll
GOTO next
:DeleteAll
rem delete all
:DeleteLast
rem delete last
:next
rem continue work with Batch file
You can just catch all other inputs like this:
:CONFIRM
if /i "%PARAMVALUE%"=="y" goto DeleteLast
if /i "%PARAMVALUE%"=="n" goto DeleteAll
echo Please enter only Y/y or N/n!
ping localhost -n 2 >nul
goto CONFIRM
I used this too for my own scripts and it works wonderfully and is super easy!
The delete command already offers a prompt N/y
EDIT
del /p
type del /? in console for more information
UPDATE
you can test with this example:
#echo off
SETLOCAL
Echo:
Echo /!\ Creating a bunch of empty files /!\
echo Press any key to continue or CTRL-C to exit...
Pause>nul
for /L %%A in (1,1,4) do (copy /y NUL _temp_file_%%A.ext)
Echo:
Echo /!\ Deleting a bunch of empty files with command prompt /!\
echo Press any key to continue or CTRL-C to exit...
Pause>nul
del /p "_temp_file_?.ext"
Echo:
Pause>nul|(echo Press any key to exit...)

Real time keyboard inputs in batch

This link
https://www.youtube.com/watch?v=eULe3DNS8DM
Shows a game made in batch. The game, called viewpoint, allows you to move around and shoot at enemies. This game seems to accept keyboard input in real time to perform actions. Is there any way I can get my batch file to accept keyboard input in real time as well?
(Guessing by the "Core" folder I saw in the game folder, there might be some other program or command involved.)
You could use the choice command (which is what snake.bat uses) however it can only take alpha-numeric input one at a time.
Here is a quick program I whipped up which uses choice to take WASD input.
#setlocal enableextensions enabledelayedexpansion
#echo off
prompt $$$G
title movement
color 0a
set length=
for %%a in (1 2 3) do (
set "length=!length! "
)
:controls
cls
echo Use WASD to move your character ([]).
echo.
echo %length%[]
choice /c wasd /n
if %errorlevel% equ 1 call:up
if %errorlevel% equ 2 call:left
if %errorlevel% equ 3 call:down
if %errorlevel% equ 4 call:right
:left
set length=!length:~0,-1!
goto controls
:right
set "length= %length%"
goto controls
:up
set length=!length:~0,-80!
goto controls
:down
set "length= %length%"
goto controls
The main thing you should focus on is the :controls label and how it uses the choice command to read input.

Batch Script Game was not expected at this time

I'm a new batch programmer and am creating a batch "start menu". I'm using the following text and it's saying "X" was not expected at this time. X was 1, game, text, and other things. I can't figure it out. HELP! I'm running XP SP3 and using the command prompt. Also, if anyone spots any other mistakes please inform me. e-mail me at superzemus#hotmail.com
cls
#echo off
echo Welcome to the Start Menu!
echo.
echo Press ctrl-z to exit. Press G to play Adventure. Press T to enter Text Editor.
pause
set Game= %gme%
set Text= %txt%
IF select Game goto gme
IF select Text goto txt
:txt
echo You have chosen to enter the Text editor.
pause
start edit
:gme
echo You have chosen to play Adventure.
pause
start C:\Adventure.exe
What you want here is probably either set /p:
set /p choice=Press ctrl-z to exit. Press G to play Adventure. Press T to enter Text Editor.
and then compare as follows:
if /i %choice%==G goto Game
if /i %choice%==T goto Text
goto :eof
Or use choice which doesn't require you to press Return:
choice /m "Press Q to exit. Press G to play Adventure. Press T to enter Text Editor." /c GTQ /n
if errorlevel 3 goto :eof
if errorlevel 2 goto Text
if errorlevel 1 goto Game
goto :eof
There is also another problem with your code: You should exit the batch file after starting the editor to avoid starting the game too:
:Text
start edit
goto :eof
:Game
rem That's a horrible place to put something
start C:\Adventure.exe
goto :eof

Resources