Keyboard Input Methods

Applications running under MS-DOS on IBM PC—compatible machines can use several methods to obtain keyboard input:

MS-DOS handle-oriented functions

MS-DOS traditional character functions

IBM ROM BIOS keyboard-driver functions

These methods offer different degrees of flexibility, portability, and hardware independence.

The handle, or stream-oriented, functions are philosophically derived from UNIX/XENIX and were first introduced in MS-DOS version 2.0. A program uses these functions by supplying a handle, or token, for the desired device, plus the address and length of a buffer.

When a program begins executing, MS-DOS supplies it with predefined handles for certain commonly used character devices, including the keyboard:

Handle Device name Opened to

0 Standard input (stdin) CON

1 Standard output (stdout) CON

2 Standard error (stderr) CON

3 Standard auxiliary (stdaux) AUX

4 Standard printer (stdprn) PRN

These handles can be used for read and write operations without further preliminaries. A program can also obtain a handle for a character device by explicitly opening the device for input or output using its logical name (as though it were a file). The handle functions support I/O redirection, allowing a program to take its input from another device or file instead of the keyboard, for example. Redirection is discussed in detail in Chapter 15.

The traditional character-input functions are a superset of the character I/O functions that were present in CP/M. Originally included in MS-DOS simply to facilitate the porting of existing applications from CP/M, they are still widely used. In MS-DOS versions 2.0 and later, most of the traditional functions also support I/O redirection (although not as well as the handle functions do).

Use of the IBM ROM BIOS keyboard functions presupposes that the program is running on an IBM PC—compatible machine. The ROM BIOS keyboard driver operates at a much more primitive level than the MS-DOS functions and allows a program to circumvent I/O redirection or MS-DOS's special handling of certain control characters. Programs that use the ROM BIOS keyboard driver are inherently less portable than those that use the MS-DOS functions and may interfere with the proper operation of other programs; many of the popular terminate-and-stay-resident (TSR) utilities fall into this category.