Computer Hope

Microsoft => Microsoft DOS => Topic started by: Sky Ninja on October 10, 2008, 08:23:16 PM

Title: Make batch react to buttons
Post 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:?
Title: Re: Make batch react to buttons
Post by: Carbon Dudeoxide on October 10, 2008, 08:35:12 PM
Nope, you cannot make a button like on the Internet.
Title: Re: Make batch react to buttons
Post by: Sky Ninja on October 10, 2008, 08:49:51 PM
 ???
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.
Title: Re: Make batch react to buttons
Post by: Carbon Dudeoxide on October 10, 2008, 08:52:30 PM
Oh sorry, do you mean Keyboard presses?  :D
Title: Re: Make batch react to buttons
Post by: Sky Ninja on October 10, 2008, 08:53:41 PM
Yep.
Title: Re: Make batch react to buttons
Post by: Carbon Dudeoxide on October 10, 2008, 09:33:39 PM
I think there might be a way, but you will have to wait for our Batch Gurus to assist. ;)
Title: Re: Make batch react to buttons
Post by: Dias de verano on October 11, 2008, 02:49:09 AM
Code: [Select]
@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%
Title: Re: Make batch react to buttons
Post by: ALAN_BR on October 11, 2008, 02:50:55 AM
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
Title: Re: Make batch react to buttons
Post by: Jacob on October 11, 2008, 02:59:59 AM
Just getting all of the key numbers, watch this space.
Title: Re: Make batch react to buttons
Post by: Dias de verano on October 11, 2008, 03:12:24 AM
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

Code: [Select]
@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
Title: Re: Make batch react to buttons
Post by: Jacob on October 11, 2008, 03:18:52 AM
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

Code: [Select]
@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:
Title: Re: Make batch react to buttons
Post by: Dias de verano on October 11, 2008, 03:25:24 AM
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.
Title: Re: Make batch react to buttons
Post by: Jacob on October 11, 2008, 03:26:33 AM
This is just every letter in case anybody wants it:
Code: [Select]
@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
Title: Re: Make batch react to buttons
Post by: Dias de verano on October 11, 2008, 03:31:08 AM
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



Title: Re: Make batch react to buttons
Post by: Jacob on October 11, 2008, 03:40:34 AM
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.
 ;)

Code: [Select]
@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
Title: Re: Make batch react to buttons
Post by: Jacob on October 11, 2008, 03:51:20 AM
Dias, this interests me the most.
Code: [Select]
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?
Title: Re: Make batch react to buttons
Post by: Dias de verano on October 11, 2008, 05:47:14 AM
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
Title: Re: Make batch react to buttons
Post by: Sky Ninja on October 11, 2008, 10:06:52 AM
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?
Title: Re: Make batch react to buttons
Post by: Dias de verano on October 11, 2008, 10:35:56 AM
Would the use of /I make uppercase and lowercase the same?

The use where?

Title: Re: Make batch react to buttons
Post by: Sky Ninja on October 12, 2008, 01:09:37 PM
Nevermind, it wouldn't. I meant in the if's.
Anyway, I figured out how to get new errorlevel settings.
Code: [Select]
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.
Code: [Select]
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%]
Title: Re: Make batch react to buttons
Post by: Dias de verano on October 12, 2008, 01:21:55 PM
Without wishing to be horrible, I will point out that code which contains a zillion lines like this:

Code: [Select]
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.

 
Title: Re: Make batch react to buttons
Post by: Sky Ninja on October 13, 2008, 10:21:38 PM
Hey, this is just an addition to Jacob's code.