Q(uick)BASIC Function: INKEY$
Quick View
INKEY$
A device I/O function that reads a character from the keyboard
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- INKEY$
Description/Parameter(s)
- INKEY$ returns a null string if there is no character to return.
- For standard keys, INKEY$ returns a 1-byte string containing the character read.
- For extended keys, INKEY$ returns a 2-byte string made up of the null character (ASCII 0) and the keyboard scan code.
Example
PRINT "Press Esc to exit..."
DO
LOOP UNTIL INKEY$ = CHR$(27) '27 is the ASCII code for Esc.
See also:
Syntax
- INKEY$
Description/Parameter(s)
The INKEY$ function returns a one- or two-byte string containing a character read from the standard input device. A null string is returned if no character is waiting there. A one-character string contains the actual character read from the keyboard, while a two- character string indicates an extended code, the first character of which is hexadecimal 00.
See ⮜ Keyboard Scan Codes Table ⮞ and the ⮜ ASCII Character Codes Table ⮞ for a complete list of these codes.
The standard input device is usually the keyboard. INKEY$ does not echo characters to the screen; instead, all characters are passed through to the program except for the following:
CTRL+BREAK, | which halts program execution |
CTRL+ALT+DEL, | which does a system reboot |
CTRL+NUMLOCK, | which causes program execution to pause |
PRTSC, | which prints the screen |
Example
This example shows a common use of INKEY$. The program pauses until the user presses a key:
PRINT "Press any key to continue..."
DO
LOOP WHILE INKEY$=""
Syntax
- INKEY$
Description/Parameter(s)
INKEY$ returns a 1- or 2-byte string that contains a character read from the standard input device, which usually is the keyboard:
- A null string is returned if there is no character to return.
- A one-character string contains the actual character read.
- A two-character string indicates an extended ASCII code, the first character of which is hexadecimal 00.
See ⮜ Keyboard Scan Codes ⮞ and the ⮜ ASCII Character Codes ⮞ for a complete list of character codes that can be returned by the INKEY$ function.
Usage Notes
- The character returned by the INKEY$ function is never displayed on the screen; instead, all characters are passed through to the program except for these key combinations:
Character | Running under QBX | Stand-alone .EXE File |
Ctrl+Break | Halts program execution. | Halts program execution if compiled with the /D option; otherwise, passed through to program. |
Ctrl+NumLock | Pauses program execution until a key is pressed; then the next key pressed is passed through to the program. | Same as running under QBX if compiled with /D option; otherwise, ignored (the program does not pause and no keystroke is passed). |
Shift+PrtSc | Prints the screen contents. | Prints the screen contents. |
Ctrl+Alt+Del | Reboots the system. | Reboots the system. |
- If you have assigned a string to a function key using the KEY statement and you press that function key when INKEY$ is waiting for a keystroke, INKEY$ will pass the string to the program. Enabled keystroke trapping takes precedence over the INKEY$ function.
- When you use INKEY$ with ISAM programs, BASIC performs implicit CHECKPOINT operations to minimize data loss in the event of a power failure. The CHECKPOINT is performed if INKEY$ fails to successfully retrieve a character after 65,535 calls, and 20 seconds has expired. A CHECKPOINT writes open database buffers to disk.
Example
This example uses the INKEY$ function to cause a program to pause until a key is pressed.
PRINT "Press any key to continue..."
DO
LOOP WHILE INKEY$ = ""