Computer Hope
Microsoft => Microsoft DOS => Topic started by: Sky Ninja on October 10, 2008, 08:23:16 PM
-
I need to make a batch file react to button pushes, but most people who get my file won't have the choice command available. I don't want them to have to go through installing it, so is there a way other than choice /c:?
-
Nope, you cannot make a button like on the Internet.
-
???
What I meant is a button on the keyboard. I need an alternative to choice /c:*whatever*.
I know you can't do things such as clickable buttons. That's only available through .com or .exe. Edit.com for example.
-
Oh sorry, do you mean Keyboard presses? :D
-
Yep.
-
I think there might be a way, but you will have to wait for our Batch Gurus to assist. ;)
-
@echo off
REM Create getkey.com
echo hD1X-s0P_kUHP0UxGWX4ax1y1ieimnfeinklddmemkjanmndnadmndnpbbn>getkey.com
echo hhpbbnpljhoxolnhaigidpllnbkdnhlkfhlflefblffahfUebdfahhfkokh>>getkey.com
echo l/QnKE@HB61H.>>getkey.com
REM Run getkey.com which...
REM ...waits for a keypress
getkey.com
REM ASCII value of key is contained in %errorlevel%
echo %errorlevel%
-
Hi
Immediately below the "Logout" button you will find a Search box
If you search for "choice" you get more hits than I care to list.
The first Hit takes you to "http://www.computerhope.com/choicehl.htm"
This links to "http://www.computerhope.com/sethlp.htm", which should give more than enough help.
If you want further help, or want to see examples of how to do it, then Hit 2, or any one of more than 50 other hits, should do the job.
Regards
Alan
-
Just getting all of the key numbers, watch this space.
-
The pause would help :P
I guess if you run the batch by double clicking it in Windows Explorer, yes, it would help by stopping the command window abruptly closing. I am used to trialling new batch files by having a command window open and typing the batch name at the prompt, to avoid exactly that problem.
Also, it strikes me that it might be handy to know the actual character of the key which has been pressed, so I whipped up a simple vbscript to return that
@echo off
echo hD1X-s0P_kUHP0UxGWX4ax1y1ieimnfeinklddmemkjanmndnadmndnpbbn>getkey.com
echo hhpbbnpljhoxolnhaigidpllnbkdnhlkfhlflefblffahfUebdfahhfkokh>>getkey.com
echo l/QnKE@HB61H.>>getkey.com
echo Wscript.echo Chr^(WScript.Arguments^(0^)^)>CharFromASCII.vbs
Echo Please press a key
getkey.com
set ascii=%errorlevel%
for /f "delims==" %%C in ('cscript //nologo CharFromASCII.vbs %ascii%') do set char=%%C
del getkey.com
del CharFromASCII.vbs
echo You pressed this key: %char%
REM Complete with PAUSE at the end...
REM Thanks to Jacob.
PAUSE
-
The pause would help :P
I guess if you run the batch by double clicking it in Windows Explorer, yes, it would help by stopping the command window abruptly closing. I am used to trialling new batch files by having a command window open and typing the batch name at the prompt, to avoid exactly that problem.
Also, it strikes me that it might be handy to know the actual character of the key which has been pressed, so I whipped up a simple vbscript to return that
@echo off
echo hD1X-s0P_kUHP0UxGWX4ax1y1ieimnfeinklddmemkjanmndnadmndnpbbn>getkey.com
echo hhpbbnpljhoxolnhaigidpllnbkdnhlkfhlflefblffahfUebdfahhfkokh>>getkey.com
echo l/QnKE@HB61H.>>getkey.com
echo Wscript.echo Chr^(WScript.Arguments^(0^)^)>CharFromASCII.vbs
Echo Please press a key
getkey.com&for /f "delims==" %%C in ('cscript //nologo CharFromASCII.vbs %errorlevel%') do set char=%%C
del getkey.com&del CharFromASCII.vbs
echo You pressed this key: %char%
REM Complete with PAUSE at the end...
REM Thanks to Jacob.
PAUSE
Just as I was trying to do it the hard way:
-
Yes, my first thought was to do it the long way, of testing for every errorlevel possible, then I remembered how many keys there are on a keyboard, and I thought that nobody will want to add several hundred lines to their batch file.
-
This is just every letter in case anybody wants it:
@echo off
REM Create getkey.com
echo hD1X-s0P_kUHP0UxGWX4ax1y1ieimnfeinklddmemkjanmndnadmndnpbbn>getkey.com
echo hhpbbnpljhoxolnhaigidpllnbkdnhlkfhlflefblffahfUebdfahhfkokh>>getkey.com
echo l/QnKE@HB61H.>>getkey.com
REM Run getkey.com which...
REM ...waits for a keypress
:run
getkey.com
REM ASCII value of key is contained in %errorlevel%
If %errorlevel%==97 (
echo A
)
If %errorlevel%==98 (
echo B
)
If %errorlevel%==99 (
echo C
)
If %errorlevel%==100 (
echo D
)
If %errorlevel%==101 (
echo E
)
If %errorlevel%==102 (
echo F
)
If %errorlevel%==103 (
echo G
)
If %errorlevel%==104 (
echo H
)
If %errorlevel%==105 (
echo I
)
If %errorlevel%==106 (
echo J
)
If %errorlevel%==107 (
echo K
)
If %errorlevel%==108 (
echo L
)
If %errorlevel%==109 (
echo M
)
If %errorlevel%==110 (
echo N
)
If %errorlevel%==111 (
echo O
)
If %errorlevel%==112 (
echo P
)
If %errorlevel%==113 (
echo Q
)
If %errorlevel%==114 (
echo R
)
If %errorlevel%==115 (
echo S
)
If %errorlevel%==116 (
echo T
)
If %errorlevel%==117 (
echo U
)
If %errorlevel%==118 (
echo V
)
If %errorlevel%==119 (
echo W
)
If %errorlevel%==120 (
echo X
)
If %errorlevel%==121 (
echo Y
)
If %errorlevel%==122 (
echo Z
)
goto run
PAUSE >nul
-
1. Your code only works for lowercase letters of the alphabet 'a' to 'z'.
2. But you show uppercase letters to the user. ASCII 97 is 'a' not 'A'. The keypress program gives a different code if a key is shifted or if Caps Lock is operative.
3. By using this format
if test (
do this
)
Rather than this
if test do this
You are adding 2 unnecessary lines for each simple IF test
-
1. Your code only works for lowercase letters of the alphabet 'a' to 'z'.
2. But you show uppercase letters to the user. ASCII 97 is 'a' not 'A'. The keypress program gives a different code if a key is shifted or if Caps Lock is operative.
3. By using this format
if test (
do this
)
Rather than this
if test do this
You are adding 2 unnecessary lines for each simple IF test
Thanks for the unseen problem fix.
;)
@echo off
REM Create getkey.com
echo hD1X-s0P_kUHP0UxGWX4ax1y1ieimnfeinklddmemkjanmndnadmndnpbbn>getkey.com
echo hhpbbnpljhoxolnhaigidpllnbkdnhlkfhlflefblffahfUebdfahhfkokh>>getkey.com
echo l/QnKE@HB61H.>>getkey.com
REM Run getkey.com which...
REM ...waits for a keypress
:run
getkey.com
set key=%errorlevel%
REM ASCII value of key is contained in %Key%
If %Key%==97 echo a [%Key%]
If %Key%==98 echo b [%Key%]
If %Key%==99 echo c [%Key%]
If %Key%==100 echo d [%Key%]
If %Key%==101 echo e [%Key%]
If %Key%==102 echo f [%Key%]
If %Key%==103 echo g [%Key%]
If %Key%==104 echo h [%Key%]
If %Key%==105 echo i [%Key%]
If %Key%==106 echo j [%Key%]
If %Key%==107 echo k [%Key%]
If %Key%==108 echo l [%Key%]
If %Key%==109 echo m [%Key%]
If %Key%==110 echo n [%Key%]
If %Key%==111 echo o [%Key%]
If %Key%==112 echo p [%Key%]
If %Key%==113 echo q [%Key%]
If %Key%==114 echo r [%Key%]
If %Key%==115 echo s [%Key%]
If %Key%==116 echo t [%Key%]
If %Key%==117 echo u [%Key%]
If %Key%==118 echo v [%Key%]
If %Key%==119 echo w [%Key%]
If %Key%==120 echo x [%Key%]
If %Key%==121 echo y [%Key%]
If %Key%==122 echo z [%Key%]
::----------------------------------------------------------
If %Key%==65 echo A [%Key%]
If %Key%==66 echo B [%Key%]
If %Key%==67 echo C [%Key%]
If %Key%==68 echo D [%Key%]
If %Key%==69 echo E [%Key%]
If %Key%==70 echo F [%Key%]
If %Key%==71 echo G [%Key%]
If %Key%==72 echo H [%Key%]
If %Key%==73 echo I [%Key%]
If %Key%==74 echo J [%Key%]
If %Key%==75 echo K [%Key%]
If %Key%==76 echo L [%Key%]
If %Key%==77 echo M [%Key%]
If %Key%==78 echo N [%Key%]
If %Key%==79 echo O [%Key%]
If %Key%==80 echo P [%Key%]
If %Key%==81 echo Q [%Key%]
If %Key%==82 echo R [%Key%]
If %Key%==83 echo S [%Key%]
If %Key%==84 echo T [%Key%]
If %Key%==85 echo U [%Key%]
If %Key%==86 echo V [%Key%]
If %Key%==87 echo W [%Key%]
If %Key%==88 echo X [%Key%]
If %Key%==89 echo Y [%Key%]
If %Key%==90 echo Z [%Key%]
::----------------------------------------------------------
If %Key%==32 echo SpaceBar [%Key%]
::ECHO [%KEY%]
goto run
PAUSE >nul
-
Dias, this interests me the most.
REM Create getkey.com
echo hD1X-s0P_kUHP0UxGWX4ax1y1ieimnfeinklddmemkjanmndnadmndnpbbn>getkey.com
echo hhpbbnpljhoxolnhaigidpllnbkdnhlkfhlflefblffahfUebdfahhfkokh>>getkey.com
echo l/QnKE@HB61H.>>getkey.com
What and how is this done?
-
A .com program is an 8086 assembly language program for running under MS-DOS. The effect of the lines you quoted is to create such a (previously written) program byte by byte. What the program does is
1. wait for a keypress
2. place the ascii value into errorlevel
3. exit
-
1. Your code only works for lowercase letters of the alphabet 'a' to 'z'.
2. But you show uppercase letters to the user. ASCII 97 is 'a' not 'A'. The keypress program gives a different code if a key is shifted or if Caps Lock is operative.
Would the use of /I make uppercase and lowercase the same?
-
Would the use of /I make uppercase and lowercase the same?
The use where?
-
Nevermind, it wouldn't. I meant in the if's.
Anyway, I figured out how to get new errorlevel settings.
set key=%errorlevel%
[b]echo %errorlevel% >C:\errorlevel.txt[/b]
REM ASCII value of key is contained in %Key%
Press a known key, then press an unknown key, and if it's different from the known key, you've got a new errorlevel.
If %Key%==48 echo 0 [%Key%]
If %Key%==49 echo 1 [%Key%]
If %Key%==50 echo 2 [%Key%]
If %Key%==51 echo 3 [%Key%]
If %Key%==52 echo 4 [%Key%]
If %Key%==53 echo 5 [%Key%]
If %Key%==54 echo 6 [%Key%]
If %Key%==55 echo 7 [%Key%]
If %Key%==56 echo 8 [%Key%]
If %Key%==57 echo 9 [%Key%]
::----------------------------------------------------------
If %Key%==13 echo Enter [%Key%]
::----------------------------------------------------------
If %Key%==27 echo Escape [%Key%]
-
Without wishing to be horrible, I will point out that code which contains a zillion lines like this:
IF X = 1 goto one
IF X = 2 goto two
[...]
IF X = 254 goto twofivefour
IF X = 255 goto twofivefive
(etc)
... is generally the mark of the beginning coder. Sometimes it cannot be avoided, but it is much better to find a more compact solution, which I already have shown.
-
Hey, this is just an addition to Jacob's code.