INKEY - Get a character from the keyboard
Purpose: Get a single keystroke from the user and store it in an
environment variable.
Format: INKEY [/C /D /K"keys" /P /Wn /X] [prompt ] %%varname
prompt : Optional text that is displayed as a prompt.
varname : The variable that will hold the user's keystroke.
/C(lear buffer) /P(assword)
/D(igits only) /W(ait)
/K(valid keystrokes) /X (no carriage return)
See also: INPUT.
Usage
INKEY optionally displays a prompt. Then it waits for a specified time
or indefinitely for a keystroke, and places the keystroke into an
environment variable. It is normally used in batch files and aliases to
get a menu choice or other single-key input. Along with the INPUT
command, INKEY allows great flexibility in reading input from within a
batch file or alias.
If prompt text is included in an INKEY command, it is displayed while
INKEY waits for input.
The following batch file fragment prompts for a character and stores it
in the variable NUM:
inkey Enter a number from 1 to 9: %%num
INKEY reads standard input for the keystroke, so it will accept
keystrokes from a redirected file or from the KEYSTACK. You can supply a
list of valid keystrokes with the /K option.
Standard keystrokes with ASCII values between 1 and 255 are stored
directly in the environment variable. Extended keystrokes (for example,
function keys and cursor keys) are stored as a string in decimal format,
with a leading @ (for example, the F1 key is @59). The Enter key is
stored as an extended keystroke, with the code @28. See the Reference
Tables for a list of the ASCII and extended key codes.
To test for a non-printing ASCII keystroke returned by INKEY use the
@ASCII function to get the numeric value of the key. For example, to
test for Esc, which has an ASCII value of 27:
inkey Enter a key: %%key
if "%@ascii[%key]" == "27" echo Esc pressed
If you press Ctrl-C or Ctrl-Break while INKEY is waiting for a key,
execution of an alias will be terminated, and execution of a batch file
will be suspended while you are asked whether to cancel the batch job. A
batch file can handle Ctrl-C and Ctrl-Break itself with the ON BREAK
command.
Options
/C: (Clear buffer) Clears the keyboard buffer before INKEY accepts
keystrokes. If you use this option, INKEY will ignore any
keystrokes which you type, either accidentally or
intentionally, before it is ready to accept input.
/D: (Digits only) Prevents INKEY from accepting any keystroke
except a digit from 0 to 9.
/K"keys":
Specifythepermissiblekeystrokes . The
listofvalidkeystrokesshouldbeenclosedindoublequotes . For
alphabetickeysthevaliditytestisnotcasesensitive . You
canspecifyextendedkeysbyenclosingtheirnamesinsquarebrackets(
withinthequotes ) ,forexample :
inkey /k"ab[Alt-F10]" Enter A, B, Alt-F10 %%var
See Keys and Key Names for a complete listing of the key names
you can use within the square brackets, and a description of
the key name format.
If an invalid keystroke is entered, CMD.EXE will echo the
keystroke if possible, beep, move the cursor back one
character, and wait for another keystroke.
/P: (Password) Prevents INKEY from echoing the character.
/W: (Wait) Timeout period, in seconds, to wait for a response. If
no keystroke is entered by the end of the timeout period,
INKEY returns with the variable unchanged. This allows you to
continue the batch file if the user does not respond in a
given period of time. You can specify /W0 to return
immediately if there are no keys waiting in the keyboard
buffer.
For example, the following batch file fragment waits up to 10
seconds for a character, then tests to see if a "Y" was
entered:
set net=N
inkey /K"YN" /w10 Load network (Y/N)? %%net
iff "%net" == "Y" then
rem Commands to load the network go here
endiff
/X: (No carriage return) Prevents INKEY from displaying a carriage
return and line feed after the user's entry.
Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995 Peter Childs