Keyboard Input with Traditional Calls

The MS-DOS traditional keyboard functions offer a variety of character and line-oriented services with or without echo and Ctrl-C detection. These functions are summarized on the following page.

Int 21H Function Action Ctrl-C checking

01H Keyboard input with echo Yes

06H Direct console I/O No

07H Keyboard input without echo No

08H Keyboard input without echo Yes

0AH Buffered keyboard input Yes

0BH Input-status check Yes

0CH Input-buffer reset and input Varies

In MS-DOS versions 2.0 and later, redirection of the standard input affects all these functions. In other words, they act as though they were special cases of an Int 21H Function 3FH call using the predefined standard input handle (0).

The character-input functions (01H, 06H, 07H, and 08H) all return a character in the AL register. For example, the following sequence waits until a key is pressed and then returns it in AL:

mov ah,1 ; function 01h = read keyboard

int 21h ; transfer to MS-DOS

The character-input functions differ in whether the input is echoed to the screen and whether they are sensitive to Ctrl-C interrupts. Although MS-DOS provides no pure keyboard-status function that is immune to Ctrl-C, a program can read keyboard status (somewhat circuitously) without interference by using Int 21H Function 06H. Extended keys, such as the IBM PC keyboard's special function keys, require two calls to a character-input function.

As an alternative to single-character input, a program can use buffered-line input (Int 21H Function 0AH) to obtain an entire line from the keyboard in one operation. MS-DOS builds up buffered lines in an internal buffer and does not pass them to the calling program until the user presses the Enter key. While the line is being entered, all the usual editing keys are active and are handled by the MS-DOS keyboard driver. You use Int 21H Function 0AH as follows:

buff db 81 ; maximum length of input

db 0 ; actual length (from MS-DOS)

db 81 dup (0) ; receives keyboard input

.

.

.

mov ah,0ah ; function 0ah = read buffered line

mov dx,seg buff ; DS:DX = buffer address

mov ds,dx

mov dx,offset buff

int 21h ; transfer to MS-DOS

.

.

.

Int 21H Function 0AH differs from Int 21H Function 3FH in several important ways. First, the maximum length is passed in the first byte of the buffer, rather than in the CX register. Second, the actual length is returned in the second byte of the structure, rather than in the AX register. Finally, when the user has entered one less than the specified maximum number of characters, MS-DOS ignores all subsequent characters and sounds a warning beep until the Enter key is pressed.

For detailed information about each of the traditional keyboard-input functions, see Section II of this book, "MS-DOS Functions Reference."