Discussion:
How do you make a batch file accept 1-char keyboard input WITHOUT having to also press carriage return?
Add Reply
Bob J Jones
2018-05-29 00:13:59 UTC
Reply
Permalink
Raw Message
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?

This generic DOS network kill script works great to kill the network,
for example, whenever you install programs that you don't want to
phone home.
https://www.liquidvpn.com/vpn-kill-switches/

All the DOS script does is disable and re-enable the router gateway
(192.168.1.1) in the routing table.

But it's a pain to always have to hit carriage return after pressing
1, 2, or 3. It would be nice if just pressing the 1, 2, or 3
(or "d" for disable and "e" for enable) worked in the script without
the script needing the user to also press the carriage return.

FILE: LiquidVPN-Kill-Switch.bat
LOCATION: https://www.liquidvpn.com/billing/dl.php?type=d&id=49

Do you know DOS batch scripts well enough to tell us how to eliminate
the need to press the carriage return after pressing the
(1) to disable the gateway, the
(2) to re-enable the gateway, or
(3) to set the gateway?

Here is a snippet of that code.
set defgw=192.168.0.1
echo Your routers gateway is probably "%defgw%"
echo -if nothing appears or its incorrect, add it manually (Press '3')
echo.
echo USAGE:
echo.
echo -Press "1" to Enable Kill Switch (IP "%defgw%")
echo -Press "2" to Disable Kill Switch (IP "%defgw%")
echo -Press "3" to manually set default gateway if its not detected above.
echo -Press "h" for Kill Switch Help
echo -Press "x" to exit Kill Switch.
echo.
set /p option=Your option:
if '%option%'=='1' goto :option1
if '%option%'=='2' goto :option2
if '%option%'=='3' goto :option3
if '%option%'=='x' goto :exit
if '%option%'=='h' goto :help
echo Insert 1, 2, x or h
timeout 3
goto start
:option1
route delete 0.0.0.0 %defgw%
echo Default gateway "%defgw%" removed
timeout 3
goto start
:option2
route add 0.0.0.0 mask 0.0.0.0 %defgw%
echo Defaulte gateway "%defgw%" restored
timeout 3
goto start
:option3
echo
set /p defgw=your gw IP (e.g. 192.168.0.1):
goto start
:help
cls

How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
Paul
2018-05-29 08:22:19 UTC
Reply
Permalink
Raw Message
Post by Bob J Jones
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
The options here look pretty miserable.
Still, you could take a browse through it.
There are some helper routines listed
near the end.

http://www.robvanderwoude.com/userinput.php

*******

The only thing I can suggest, is binding some key
to generate a string, such as "x<enter>" such that
the keypress carries out your desired action.

PrintScreen ==> "x<enter>"

And no, I have nothing like that loaded on the
computer here, and I don't know if solving that
problem is any easier.

I'm really surprised they made it that difficult.

HTH,
Paul
Andy Burns
2018-05-29 10:04:52 UTC
Reply
Permalink
Raw Message
Post by Bob J Jones
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
<https://helloacm.com/lost-era-microsoft-dos-com-assembly-8-byte-program-getkey>
Ralph Fox
2018-05-29 10:17:02 UTC
Reply
Permalink
Raw Message
Post by Bob J Jones
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
A) Create one version of the batch file using CHOICE for Win 7-10
(see below), and another version using SET /P for Win XP.

B) Or, if you must have a single version of the batch file, detect
the OS in the batch script and use CHOICE or SET accordingly.
For example:
~~~~~~~~~~~~~~~ EXAMPLE ~~~~~~~~~~~~~~~
IF /I #%USERPROFILE:~3,5%#==#Users# (
CHOICE /C "123hx" /M "Your option - Win7-10"
IF ERRORLEVEL 1 SET option=1
IF ERRORLEVEL 2 SET option=2
IF ERRORLEVEL 3 SET option=3
IF ERRORLEVEL 4 SET option=h
IF ERRORLEVEL 5 SET option=x
) ELSE (
SET /P option=Your option - Win XP:
)
ECHO option selected was %option%
~~~~~~~~~~~~~~~ EXAMPLE ~~~~~~~~~~~~~~~


The CHOICE command (below) works in Windows 10 and Windows 7, but is
not supported in Windows XP.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ QUOTE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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."

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ QUOTE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Post by Bob J Jones
This generic DOS network kill script works great to kill the network,
for example, whenever you install programs that you don't want to
phone home.
https://www.liquidvpn.com/vpn-kill-switches/
All the DOS script does is disable and re-enable the router gateway
(192.168.1.1) in the routing table.
But it's a pain to always have to hit carriage return after pressing
1, 2, or 3. It would be nice if just pressing the 1, 2, or 3
(or "d" for disable and "e" for enable) worked in the script without
the script needing the user to also press the carriage return.
FILE: LiquidVPN-Kill-Switch.bat
LOCATION: https://www.liquidvpn.com/billing/dl.php?type=d&id=49
Do you know DOS batch scripts well enough to tell us how to eliminate
the need to press the carriage return after pressing the
(1) to disable the gateway, the
(2) to re-enable the gateway, or
(3) to set the gateway?
Here is a snippet of that code.
set defgw=192.168.0.1
echo Your routers gateway is probably "%defgw%"
echo -if nothing appears or its incorrect, add it manually (Press '3')
echo.
echo.
echo -Press "1" to Enable Kill Switch (IP "%defgw%")
echo -Press "2" to Disable Kill Switch (IP "%defgw%")
echo -Press "3" to manually set default gateway if its not detected above.
echo -Press "h" for Kill Switch Help
echo -Press "x" to exit Kill Switch.
echo.
if '%option%'=='1' goto :option1
if '%option%'=='2' goto :option2
if '%option%'=='3' goto :option3
if '%option%'=='x' goto :exit
if '%option%'=='h' goto :help
echo Insert 1, 2, x or h
timeout 3
goto start
:option1
route delete 0.0.0.0 %defgw%
echo Default gateway "%defgw%" removed
timeout 3
goto start
:option2
route add 0.0.0.0 mask 0.0.0.0 %defgw%
echo Defaulte gateway "%defgw%" restored
timeout 3
goto start
:option3
echo
goto start
:help
cls
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
--
Kind regards
Ralph
JJ
2018-05-29 11:04:03 UTC
Reply
Permalink
Raw Message
Post by Ralph Fox
A) Create one version of the batch file using CHOICE for Win 7-10
(see below), and another version using SET /P for Win XP.
I can't see how SET /P is capable to get a single character input without
the need to press the ENTER key. All from the keyboard, and not from
file-to-input redirection.
Ralph Fox
2018-05-30 07:06:12 UTC
Reply
Permalink
Raw Message
Post by JJ
Post by Ralph Fox
A) Create one version of the batch file using CHOICE for Win 7-10
(see below), and another version using SET /P for Win XP.
I can't see how SET /P is capable to get a single character input without
the need to press the ENTER key. All from the keyboard, and not from
file-to-input redirection.
It isn't capable.

The OP cross-posted to the newsgroups for Win 10, Win XP, and Win 7.

* CHOICE is supported in Windows 7-10 and does not require the ENTER key.

* CHOICE is *not* supported in Windows XP, so the suggested answer
falls back to using SET /P on Windows XP.

That way, the batch script can run on all 3 OSes which the OP cross-posted
to, and does not need the ENTER key in 2 out of 3 OSes. My appologies if
this was not clear and needed spelling out.
--
Kind regards
Ralph

Those who can, do; those who can't, teach.
Fokke Nauta
2018-05-29 10:28:56 UTC
Reply
Permalink
Raw Message
Post by Bob J Jones
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
This generic DOS network kill script works great to kill the network,
for example, whenever you install programs that you don't want to
phone home.
https://www.liquidvpn.com/vpn-kill-switches/
All the DOS script does is disable and re-enable the router gateway
(192.168.1.1) in the routing table.
But it's a pain to always have to hit carriage return after pressing
1, 2, or 3. It would be nice if just pressing the 1, 2, or 3
(or "d" for disable and "e" for enable) worked in the script without
the script needing the user to also press the carriage return.
FILE: LiquidVPN-Kill-Switch.bat
LOCATION: https://www.liquidvpn.com/billing/dl.php?type=d&id=49
Do you know DOS batch scripts well enough to tell us how to eliminate
the need to press the carriage return after pressing the
(1) to disable the gateway, the
(2) to re-enable the gateway, or
(3) to set the gateway?
Here is a snippet of that code.
set defgw=192.168.0.1
echo Your routers gateway is probably "%defgw%"
echo -if nothing appears or its incorrect, add it manually (Press '3')
echo.
echo.
echo -Press "1" to Enable Kill Switch (IP "%defgw%")
echo -Press "2" to Disable Kill Switch (IP "%defgw%")
echo -Press "3" to manually set default gateway if its not detected above.
echo -Press "h" for Kill Switch Help
echo -Press "x" to exit Kill Switch.
echo.
if '%option%'=='1' goto :option1
if '%option%'=='2' goto :option2
if '%option%'=='3' goto :option3
if '%option%'=='x' goto :exit
if '%option%'=='h' goto :help
echo Insert 1, 2, x or h
timeout 3
goto start
:option1
route delete 0.0.0.0 %defgw%
echo Default gateway "%defgw%" removed
timeout 3
goto start
:option2
route add 0.0.0.0 mask 0.0.0.0 %defgw%
echo Defaulte gateway "%defgw%" restored
timeout 3
goto start
:option3
echo
goto start
:help
cls
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
Why not use the JPSoft TCC/LE command processor? Is offers many more
options than the DOS/Windows command processor, although it's very
similar and you can use the same commands (and much more). It's very
simple in a batch file to ask for an input character without the Enter key.
This processor is free. You can write batch files with the .btm extension.
If you wanna give it a try, I can give you help. I have been using it
for years.

Fokke
Java Jive
2018-05-29 13:44:40 UTC
Reply
Permalink
Raw Message
Post by Bob J Jones
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
PAUSE echoes "Press a key to continue ..." and waits for the user to
press any single key.

However, if you want the user to make a choice, then, surprise,
surprise, use the CHOICE command, as in the following W9x example (the
CHOICE command is still available in W7, but its parameters may alter
somewhat between versions of Windows):

CLS
ECHO %Title%
ECHO.
ECHO 0 Quit
ECHO 1 Partition Hard Disk
ECHO 2 Format Hard Disk
ECHO 3 Configure PC Or Netcard
ECHO 4 Image PC To Or From Server
ECHO 5 Exit To DOS
ECHO.
CHOICE /C:012345 /N "Please choose [012345]: "
ECHO.
IF ERRORLEVEL 1 SET Choice=ExitDos
IF ERRORLEVEL 2 SET Choice=StdFDisk
IF ERRORLEVEL 3 SET Choice=Format
IF ERRORLEVEL 4 SET Choice=Setup
IF ERRORLEVEL 5 SET Choice=Image
IF ERRORLEVEL 6 SET Choice=ExitDos
GOTO %Choice%

See also CHOICE /? for help.
Bob J Jones
2018-05-29 21:38:34 UTC
Reply
Permalink
Raw Message
Post by Java Jive
See also CHOICE /? for help.
Thanks.
This CHOICE command seems to be the way to go.

I'm no coder, and I don't have the time I had this weekend, but I'll try it
out when I can and report back if I'm successful.

That way everyone benefits from your kind and helpful expert suggestion!
Ralph Fox
2018-05-30 07:11:39 UTC
Reply
Permalink
Raw Message
Post by Bob J Jones
Post by Java Jive
See also CHOICE /? for help.
Thanks.
This CHOICE command seems to be the way to go.
You cross-posted to the XP newsgroup, so I would guess that you also want
the batch script to run in XP.

Be aware that CHOICE is not supported in Windows XP.
Post by Bob J Jones
I'm no coder, and I don't have the time I had this weekend, but I'll try it
out when I can and report back if I'm successful.
That way everyone benefits from your kind and helpful expert suggestion!
--
Kind regards
Ralph
Bob J Jones
2018-05-30 07:45:54 UTC
Reply
Permalink
Raw Message
Post by Ralph Fox
You cross-posted to the XP newsgroup, so I would guess that you also want
the batch script to run in XP.
Be aware that CHOICE is not supported in Windows XP.
A simple switch can solve that, don't you think?
The default would be set to WINXP compatible.
If the user changes that switch to anything other than WINXP, then the
CHOIC code runs.

Something like this Pseudocode:

set defos=WINXP
if defos not WINXP then run CHOICE commands
else run default SET /P commands

Does that make sense to you as a general game plan?
Ralph Fox
2018-05-30 07:57:59 UTC
Reply
Permalink
Raw Message
Post by Bob J Jones
Post by Ralph Fox
You cross-posted to the XP newsgroup, so I would guess that you also want
the batch script to run in XP.
Be aware that CHOICE is not supported in Windows XP.
A simple switch can solve that, don't you think?
The default would be set to WINXP compatible.
If the user changes that switch to anything other than WINXP, then the
CHOIC code runs.
set defos=WINXP
if defos not WINXP then run CHOICE commands
else run default SET /P commands
Does that make sense to you as a general game plan?
Yes, that makes sense.
--
Kind regards
Ralph
Java Jive
2018-05-30 11:15:43 UTC
Reply
Permalink
Raw Message
Post by Ralph Fox
Be aware that CHOICE is not supported in Windows XP.
Nonsense, as in ...

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\TEMP>choice /?
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]

/C[:]choices Specifies allowable keys. Default is YN
/N Do not display choices and ? at end of prompt string.
/S Treat choice keys as case sensitive.
/T[:]c,nn Default choice to c after nn seconds
text Prompt string to display

ERRORLEVEL is set to offset of key user presses in choices.
Char Jackson
2018-05-30 15:16:35 UTC
Reply
Permalink
Raw Message
Post by Java Jive
Post by Ralph Fox
Be aware that CHOICE is not supported in Windows XP.
Nonsense, as in ...
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\TEMP>choice /?
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
/C[:]choices Specifies allowable keys. Default is YN
/N Do not display choices and ? at end of prompt string.
/S Treat choice keys as case sensitive.
/T[:]c,nn Default choice to c after nn seconds
text Prompt string to display
ERRORLEVEL is set to offset of key user presses in choices.
My XP VM reports the following:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\TEMP>choice /?
'choice' is not recognized as an internal or external command,
operable program or batch file.
Java Jive
2018-05-30 17:09:02 UTC
Reply
Permalink
Raw Message
Post by Java Jive
Post by Java Jive
Post by Ralph Fox
Be aware that CHOICE is not supported in Windows XP.
Nonsense, as in ...
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\TEMP>choice /?
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
/C[:]choices Specifies allowable keys. Default is YN
/N Do not display choices and ? at end of prompt string.
/S Treat choice keys as case sensitive.
/T[:]c,nn Default choice to c after nn seconds
text Prompt string to display
ERRORLEVEL is set to offset of key user presses in choices.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\TEMP>choice /?
'choice' is not recognized as an internal or external command,
operable program or batch file.
You're right.

My build was originally a W2k build on a P4 desktop which was reimaged
onto a laptop and upgraded to XP, so at the time of my previous post, I
made a point of searching the Windows directory for extra files that
might be giving the functionality, but in fact CHOICE.EXE is in the W2k
Resource Kit folder under Program Files, which is on my path. I suspect
that similarly it would be available from the XP Resource Kit, but what
bloody arseholes they are, removing something as *useful* as that!

Apologies for increasing the level of confusion.
Frank Slootweg
2018-05-30 17:14:49 UTC
Reply
Permalink
Raw Message
Post by Java Jive
Post by Java Jive
Post by Ralph Fox
Be aware that CHOICE is not supported in Windows XP.
Nonsense, as in ...
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\TEMP>choice /?
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
/C[:]choices Specifies allowable keys. Default is YN
/N Do not display choices and ? at end of prompt string.
/S Treat choice keys as case sensitive.
/T[:]c,nn Default choice to c after nn seconds
text Prompt string to display
ERRORLEVEL is set to offset of key user presses in choices.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\TEMP>choice /?
'choice' is not recognized as an internal or external command,
operable program or batch file.
Hmmm!? This page is somewhat ambiguous, but says "Windows XP and
earlier syntax" (Generally the ComputerHope 'DOS' pages are quite
accurate.):

<https://www.computerhope.com/choicehl.htm>

[Too much trouble to fetch my dormant XP machine, but if really needed,
I can get it, fire it up and try.]
Sjouke Burry
2018-05-31 00:11:14 UTC
Reply
Permalink
Raw Message
Post by Java Jive
Post by Ralph Fox
Be aware that CHOICE is not supported in Windows XP.
Nonsense, as in ...
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\TEMP>choice /?
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
/C[:]choices Specifies allowable keys. Default is YN
/N Do not display choices and ? at end of prompt string.
/S Treat choice keys as case sensitive.
/T[:]c,nn Default choice to c after nn seconds
text Prompt string to display
ERRORLEVEL is set to offset of key user presses in choices.
???????????????????????????????????????????????????????????
my XP PRO sp3:
C:\01-sjouke\test>choice /?
choice wordt niet herkend als een interne
of externe opdracht, programma of batchbestand.

C:\01-sjouke\test>
Translated:NOT RECOGNIZED!!!!!

In dos 6.22 it does work.

mike
2018-05-29 18:32:31 UTC
Reply
Permalink
Raw Message
Post by Bob J Jones
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
This generic DOS network kill script works great to kill the network,
for example, whenever you install programs that you don't want to
phone home.
https://www.liquidvpn.com/vpn-kill-switches/
All the DOS script does is disable and re-enable the router gateway
(192.168.1.1) in the routing table.
But it's a pain to always have to hit carriage return after pressing
1, 2, or 3. It would be nice if just pressing the 1, 2, or 3
(or "d" for disable and "e" for enable) worked in the script without
the script needing the user to also press the carriage return.
FILE: LiquidVPN-Kill-Switch.bat
LOCATION: https://www.liquidvpn.com/billing/dl.php?type=d&id=49
Do you know DOS batch scripts well enough to tell us how to eliminate
the need to press the carriage return after pressing the
(1) to disable the gateway, the
(2) to re-enable the gateway, or
(3) to set the gateway?
Here is a snippet of that code.
set defgw=192.168.0.1
echo Your routers gateway is probably "%defgw%"
echo -if nothing appears or its incorrect, add it manually (Press '3')
echo.
echo.
echo -Press "1" to Enable Kill Switch (IP "%defgw%")
echo -Press "2" to Disable Kill Switch (IP "%defgw%")
echo -Press "3" to manually set default gateway if its not detected above.
echo -Press "h" for Kill Switch Help
echo -Press "x" to exit Kill Switch.
echo.
if '%option%'=='1' goto :option1
if '%option%'=='2' goto :option2
if '%option%'=='3' goto :option3
if '%option%'=='x' goto :exit
if '%option%'=='h' goto :help
echo Insert 1, 2, x or h
timeout 3
goto start
:option1
route delete 0.0.0.0 %defgw%
echo Default gateway "%defgw%" removed
timeout 3
goto start
:option2
route add 0.0.0.0 mask 0.0.0.0 %defgw%
echo Defaulte gateway "%defgw%" restored
timeout 3
goto start
:option3
echo
goto start
:help
cls
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
Don't have a direct answer to your question, but some things to think about.
I had a problem trying to select the audio stream in VLC.
I needed to send a single character. Didn't have a keyboard.
I wrote a short VB program that waits two seconds, then sends "s"
to the active window. I couldn't figger out how to select the
VLC window in the program because the title bar changes with every video.
The two seconds let me double click the shortcut, then click
the vlc window to accept the "s".

For your purpose, there's another option.
There's a program called FastIPchanger.
It lets you change your network settings with a couple of clicks.
I have tab setting for DHCP.
Second tab setting uses a fixed IP address that happens to be the
same one that DHCP would get per address reservation.
It sets the default gateway to 0.0.0.0.
That disconnects me from the WEB, but keeps access to the local network.
Char Jackson
2018-05-29 22:59:45 UTC
Reply
Permalink
Raw Message
Post by mike
Post by Bob J Jones
How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?
For your purpose, there's another option.
There's a program called FastIPchanger.
It lets you change your network settings with a couple of clicks.
I have tab setting for DHCP.
Second tab setting uses a fixed IP address that happens to be the
same one that DHCP would get per address reservation.
It sets the default gateway to 0.0.0.0.
That disconnects me from the WEB, but keeps access to the local network.
0.0.0.0 isn't a valid gateway, so it's equivalent to simply removing the
gateway, which is what his script already does. I'd prefer removing the
entry rather than changing it to something that's invalid, but the
result is the same either way.
Loading...