Chapter 17. Understanding ANSI.SYS

What Is ANSI.SYS?

The term ANSI refers to the American National Standards Institute. ANSI is one of several sets of computer standards established by the institute to specify the codes that computer manufacturers can use to control video displays and keyboard mapping.

ANSI.SYS is a device driver, which means that it gives DOS additional control of the screen and keyboard devices beyond the control features built into the operating system. You use ANSI.SYS to enhance the functions of your video screen and keyboard. With ANSI.SYS, you can set screen colors, use graphics, and specify other video attributes. You can provide that personal touch to your DOS prompt. You even can change the assignments of keys on your keyboard.

The ANSI.SYS file supplied with MS-DOS contains a subset of the ANSI standards. Third-party suppliers of other ANSI.SYS files might include more features in their versions of this file, but those features are not necessary for most users.

Installing ANSI.SYS

The only way you can install the ANSI.SYS driver is to include it in your CONFIG.SYS file. The format of the line must be one of the following:

DEVICE = C:DOSANSI.SYS /X /K /R

DEVICEHIGH = C:DOSANSI.SYS /X /K /R

The /X switch enables you to remap extended keys if you are using a 101-key keyboard. The /K switch treats 101-key keyboards as if they were 84-key keyboards, ignoring extended keys. The /R switch slows screen scrolling for improved readability.

Tip

The /X and /K switches are mutually exclusive because you cannot define a 101-key and 84-key keyboard at the same time.

When you install MS-DOS on your computer, DOS places the ANSI.SYS file in the DOS subdirectory. If ANSI.SYS is located in the root directory of your boot drive, you don't have to include its drive and directory name in the command line. If you placed the ANSI.SYS file in another directory or on another drive, however, be sure that you specify the exact location of ANSI.SYS in the CONFIG.SYS statement.

In the following sections, you will learn how to use the ANSI.SYS driver and take advantage of its features.

Using ANSI.SYS

You must place the ANSI.SYS command in the CONFIG.SYS file to load the driver and reboot the system so that the new configuration takes effect, you must issue commands that tell DOS to use the ANSI features.

Because some software is written to use ANSI codes, one or more of your programs might require that ANSI.SYS be loaded. The installation procedure for this type of software notifies you of this requirement. (The program might even install the command in the CONFIG.SYS file for you.)

You can activate ANSI.SYS features also by issuing the ANSI commands yourself. However, you cannot simply type the commands at the DOS prompt; you must enter an Escape sequence. All ANSI.SYS sequences begin with the Escape character (ASCII value 27), followed by a left bracket ([). If you type this sequence at the prompt, DOS understands the Escape character to be a command to cancel the current operation; DOS cancels the operation, displays a backslash (), and moves the cursor down to the next line.

If ANSI.SYS codes are not included in a program, you can send these codes to DOS in three ways: by executing a batch file, by typing a text file (that is, by using the DOS TYPE command), or by including the codes in a PROMPT command. When you embed ANSI codes in Escape sequences, the ANSI.SYS device driver intercepts the codes and executes the appropriate commands, ignoring any characters that are not preceded by the proper codes.

You can create a text file or batch file or set up your PROMPT format in the AUTOEXEC.BAT file by using the MS-DOS editor (EDIT) or another text editor. Whatever utility you use must be capable of entering the Escape character, which is beyond the capability of many word processing programs and older text editors.

To enter the Escape character while using EDIT, hold down the Ctrl key, press P, and then press Esc.

→ For information on using EDIT, see Chapter 15, “Using the DOS Editor,” p. 361.

Note

Some text editors display the Escape character as ^[. Because the ANSI code sequence requires a left bracket after the Escape character, the ANSI sequence might appear as ^[[.

Issuing ANSI.SYS Codes in Batch Files

The key to using ANSI.SYS is to have the Escape sequences sent to the display screen. This procedure is the only way to ensure that the device driver properly intercepts and executes the commands. Batch files send these sequences to the screen through the ECHO command, which tells DOS to display all commands on the screen.

→ For more information on batch files, see Chapter 16, “Understanding Batch Files,” p. 389.

Note

By default, ECHO is set ON unless you explicitly issue an ECHO OFF command. If you do use ECHO OFF, any ANSI code line you enter thereafter must begin with the ECHO command.

Suppose that you want to use a batch file to set up formatting for a double-density disk (360KB) in a 5 1/4-inch high-density (1.2MB) drive. You use EDIT to create the batch file containing the instruction for the special effect, such as changing the color of the screen. Entering the following batch file and giving it the name NEWDISK.BAT do the trick:

@ECHO OFF
ECHO <Esc>[37;41m
ECHO THIS FUNCTION FORMATS DOUBLE DENSITY DISKETTES (360K) IN DRIVE A:
FORMAT A: /F:360
ECHO <Esc>[37;40m

The first line in the batch file tells DOS not to display any of the commands in the batch file unless specifically instructed to do so through the ECHO command. The leading character (@) instructs DOS not to display the ECHO OFF command.

The second line is an ANSI.SYS code sequence that sets the color of the screen (red, with white characters). Replace <Esc> with the actual Escape code (ASCII value 27). The ANSI code 37 produces a white foreground; 41 is the ANSI code for a red background. The character m indicates that screen attribute codes are being issued. The last line of the batch file resets the screen to white on black—the default setting. (Make sure that you use the lowercase character m in the ANSI.SYS Escape sequences.)

Tip

If you have a monochrome monitor, you can substitute 7m for 37;41m in the second line of the batch file. The code 7 tells ANSI.SYS to set reverse video. If you use 7m instead of 37;41m, substitute 0m for 37;40m in the last line to set the screen back to normal.

To see why you must use the ECHO command in the ANSI Escape sequence, remove the ECHO command from the last line of the batch file, and then run the file again. The ECHO command is required to pass the codes to the display. If it is not available, DOS informs you that it received a bad command or filename, and the screen color does not change to normal.

Issuing ANSI.SYS Codes in Text Files

Issuing ANSI codes in text files is similar to using the codes in batch files, except that you do not use an ECHO command in a text file. Also, for the commands to take effect, you must use the TYPE command to pass the codes to the display for DOS to execute. To look at a sample text file, create a SCREEN.TXT file that contains the following line:

<Esc>[37;41m

After you create and save this file, you can change the screen to red with white characters by entering the following command at the DOS prompt:

TYPE SCREEN.TXT

ANSI.SYS intercepts the characters of the file when being sent to the screen with the TYPE command. The Escape sequence, followed by the bracket, causes the device driver to execute the ANSI commands instead of passing the characters through to DOS. Your screen is now red with white characters.

You can return your screen to normal as follows. In the same way that you created SCREEN.TXT, create NORMAL.TXT with the following single line:

<Esc>[37;40M

After you enter this line, save the file. At the DOS prompt, enter the following to return your screen to normal:

TYPE NORMAL.TXT

Issuing ANSI.SYS Codes with the PROMPT Command

The third way to issue ANSI.SYS instructions is to include these instructions in a PROMPT command. You learned about the PROMPT command in Chapter 11, “Controlling Your Environment.” In short, the PROMPT command alters the way the DOS prompt appears. The normal syntax of the command is as follows:

PROMPT string

The string consists of a set of characters that might or might not include an ANSI Escape sequence. The characters you enter tell DOS how you want the DOS prompt to look. For full information on the characters you can use with the command, refer to Chapter 11. This section focuses on using the ANSI sequences with the PROMPT command.

To add more pizzazz to your DOS prompt, you can add color, as outlined in the following examples. If you have a color monitor, enter the following command, using nine spaces for the nine periods:

PROMPT $E[1;37;44mTime: $T$H$H$H$H$H$H.........$E[40m$E[K$_$E[1;44mDate:
Issuing ANSI.SYS Codes with the PROMPT Command $D$E[0;40m$E[K$_[DOS 6.2] $P$G

The display shows the time and date in bright white text on a blue background, returning to normal white on black for the prompt.

If you have a monochrome monitor, enter the following command, using nine spaces for the nine periods:

PROMPT $E[7mTime: $T$H$H$H$H$H$H.........$E[0m$E[K$_$E[7mDate: $D$E[0m$E[K$_[DOS 6.2] $P$G

The display now shows the time and date in black characters on a white background, returning to normal white on black for the prompt.

As the inclusion of [DOS 6.2] in these two examples indicates, you also can display a message in the DOS prompt. (The brackets are included for cosmetic purposes only. They are not required.) Be creative. Beam me up, Scotty; The BRAIN; USA Forever; and BOOM! are a few examples of DOS users' humor and imagination.

Controlling Your Screen with ANSI.SYS

Now that you understand how to issue ANSI.SYS commands in batch files, in text files, and as part of a PROMPT command, you're ready to use ANSI.SYS commands to control and customize your screen display.

All ANSI screen commands begin with the normal Escape code and a left bracket ([) and end with a letter of the alphabet. The letter depends on the type of command you are asking ANSI.SYS to execute. In all instances, the capitalization of the ending letter is important. As you are working through the examples in this chapter, pay strict attention to the capitalization.

You can use as many ANSI control codes as you require on the same line, as long as the codes are separated by semicolons (;) and no code contradicts another code. The order of the codes is not important.

The following sections detail the general functions that can be performed with ANSI.SYS.

Cursor Movement

If you end the Escape sequence with the uppercase letter A, ANSI.SYS assumes you want to move the cursor up a given number of lines on the screen. For instance, the following line moves the cursor up four lines on the screen:

<Esc>[4A

The horizontal position of the cursor is not affected; it only moves up. If the cursor is already at the top of the screen, the Escape code is ignored.

If you end the Escape sequence with a B (instead of an A), the cursor is moved down on the screen. Again, if the cursor is already at the bottom of the screen, the sequence is ignored.

Ending the Escape sequence with a C moves the cursor to the right by the number of indicated spaces; a D moves the cursor left. For example, the following line moves the cursor 23 spaces to the right:

<Esc>[23C

Cursor Positioning

To position the cursor at a given character location on the screen, you can use either the H or f codes. For instance, either of the following moves the cursor to line 10, column 32:

<Esc>[10;32H

<Esc>[10;32f

Note that the upper-left corner of the screen is considered position 0,0.

To save the current cursor position (in preparation for moving and later restoring it), you use the s code, as shown here:

<Esc>[s

There are no other parameters; the cursor position is immediately saved. When you want to restore the cursor to the saved position, use the u code:

<Esc>[u

Setting the Screen Mode

You can use ANSI.SYS to change the display mode of your system. You do so by providing a screen code and ending the sequence with the h code. The screen codes are listed in Table 17.1.

Table 17.1. ANSI Screen Display Codes

CodeScreen Type
0 40×25 characters, monochrome
1 40×25 characters, color
2 80×25 characters, monochrome
3 80×25 characters, color
4 320×200 pixels, monochrome
5 320×200 pixels, color
6 640×200 pixels, monochrome
7 Turns word wrap on and off
14 640×200 pixels, color
15 640×360 pixels, monochrome
16 640×360 pixels, color
17 640×480 pixels, monochrome
18 640×480 pixels, color
19 320×200 pixels, color

For example, the following line sets the screen mode to 80×25 color text:

<Esc>[3h

You can end the sequence also with a lowercase l; the only difference is in how screen code 7 is handled. With the h code, line wrap is turned on; with l, it is turned off. For instance, the following turns on 80×25 monochrome text with screen wrapping disabled:

<Esc>[2;7l

Setting the Text Attributes

Text attributes include color, bold, or reverse video. To set these attributes, list the desired attribute codes, separated by semicolons, and then terminate the Escape sequence with the m code. Table 17.2 lists the ANSI codes for setting the character attributes.

Table 17.2. Character Attribute Codes

Code Effect
0 Normal display (the default)
1 High-intensity text
4 Underlined text (monochrome)
5 Blinking text
7 Reverse video (black on white)
8 Hidden text (black on black)
30 Black foreground (character) color
31 Red foreground (character) color
32 Green foreground (character) color
33 Yellow foreground (character) color
34 Blue foreground (character) color
35 Magenta foreground (character) color
36 Cyan foreground (character) color
37 White foreground (character) color
40 Black background color
41 Red background color
42 Green background color
43 Yellow background color
44 Blue background color
45 Magenta background color
46 Cyan background color
47 White background color

Note

Some screen effects depend on the hardware. Underlined text, for example, cannot be displayed on all monitors.

You can use the codes in Table 17.2 to customize your system. You can, for example, use ANSI codes to set the default screen colors to bright green on black:

<Esc>[1;32;40m

Screen Control

Besides the functions discussed so far, ANSI.SYS also provides functions that you can use to control your screen. You can use the following ANSI code to clear the screen. It is the equivalent of the CLS command:

<Esc>[2J

If you want to erase only to the end of the current line, the following does the trick:

<Esc>[K

Customizing Your Keyboard with ANSI.SYS

Computers use a set of 256 codes to indicate specific characters. These codes make up the ASCII character set. (ASCII is an acronym for the American Standard Code for Information Interchange.) A space, for example, is ASCII code 48; the uppercase A is ASCII code 65; and the lowercase a is ASCII code 97. You already know that the Escape code used in an ANSI control sequence has an ASCII value of 27. (See Appendix E,“ASCII and Extended ASCII Codes,” for a list of ASCII codes.)

Every time you press a key on your keyboard, the system's circuits send a code to DOS. This code interprets the keystroke and displays the appropriate character. Not every code in the ASCII set, however, has a corresponding keyboard key. Moreover, some keys represent more than one character.

Keyboards actually send individual codes to DOS for each key or key combination you press on the keyboard. These codes are known as scan codes. Many scan codes correspond to ASCII codes. With the key combinations that involve the Ctrl, Alt, Shift, and Num Lock keys, however, more than 256 keystrokes (the ASCII code limit) are possible. This problem is solved by preceding some scan codes with a 0 entry.

When DOS receives a scan code from the keyboard, the program uses a built-in table to ascertain the proper character. When the ANSI.SYS device driver has been loaded through the CONFIG.SYS file, the ANSI driver takes over this chore. The driver also enables you to modify the table and assign different characters to the scan codes.

Suppose that you must prepare documents or data files that include fractions. You can assign ASCII codes for some fractions to keys that you normally don't use. ASCII code 171, for example, stands for 1/2, and code 172 stands for 1/4. (See Appendix E for a complete list of ASCII codes.) Using ANSI.SYS makes key reassignments easy.

To assign the 1/2 fraction (ASCII 171) to the Shift+6 key combination, which produces the character ^ (ASCII 94), you can use either of the following commands:

<Esc>["^";"1/2"p

<Esc>[94;171p

The ANSI.SYS keyboard-assignment Escape sequence starts with the standard Escape character, followed by a left bracket ([). The next character is the ASCII code, scan code, or key representation in quotation marks. Then you specify the new ASCII code or key representation in quotation marks. The two characters are separated by a semicolon. The sequence ends with the lowercase p code. Thus, in the first example, the key representation ^ is entered with quotation marks ("^") and is changed to "1/2". In the second example, scan code 171 replaces scan code 94.

Caution

Do not use spaces in the ANSI.SYS code sequence. If you use spaces, DOS might not interpret the sequence properly.

Remember that the first specification is for the key to be assigned. To restore the original key assignment, enter the code twice, as in this example:

<Esc>[94;94p

A key assignment does not need to be a single character. You also can assign a text message to a keystroke. This process is referred to as a macro substitution. If you often need to type the same sequence of characters, you can easily set up a macro substitution that enters those characters for you. Don't confuse this with the macro capability of DOSKEY, however. Macros that can be created with DOSKEY are much more powerful than those created with ANSI.SYS.

In the following example, the words Heigh-Ho, Silver! are assigned to the F7 key, which has the scan code 0;65:

<Esc>[0;65;"Heigh-Ho, Silver!"p

After you issue this ANSI.SYS instruction, either in a text file you type or a batch file, DOS (through ANSI.SYS) displays the message Heigh-Ho, Silver! whenever you press F7.

Because no scan code or ASCII code of 0 exists, ANSI.SYS understands that the second number indicates the extended scan code for the F7 key instead of the ASCII code for the uppercase A, which is 65. If you want to add a carriage return (ASCII code 13) after the message, include that code in the Escape sequence as follows:

<Esc>[0;65;"Heigh-Ho, Silver!";13p

You can enter many of the ASCII character codes on your keyboard by holding down the Alt key while you type the ASCII code for the character you want to display. If you reassign keys, however, you no longer can enter certain key characters in this manner. Further, reassigned keys no longer function the same way. If you reassign the backslash key (), for example, you no longer can use the backslash character to access subdirectories.

Reassigning Character Keys

ANSI.SYS reassignmentof normal character keys on your keyboard is not a good idea. Such a reassignment interferes with the normal operation of DOS and with many, if not all, of your software programs.

Caution

When you use ANSI.SYS to reassign keys, keep in mind that DOS allows a total of 200 characters. If your key reassignments take more than 200 bytes, you overwrite part of the command processor in memory and your system might lock up.

ANSI Control Codes

All ANSI commands are prefixed by two characters: the Escape character (ASCII 27) and the left square bracket (ASCII 91). Because the Escape keypress usually is interpreted as a command, inserting it in a text file might require you to enter a special command, such as Ctrl+P, before your text editor will accept it as text.

Uppercase and lowercase are significant in these commands, so you must type them exactly as indicated. In commands that allow a variable number of codes, separate each code with a semicolon.

ANSI Set and Reset Display Mode Control Codes

Set mode codes end in a lowercase h as shown. Reset mode codes substitute a lowercase l for the h. Not all displays support all screen modes, and other modes might be available for your display:

<Esc>[=0h Text mode 40×25 Monochrome
<Esc>[=1h Text mode 40×25 Color
<Esc>[=2h Text mode 80×25 Monochrome
<Esc>[=3h Text mode 80×25 Color
<Esc>[=4h Graphics mode 320×200 4 color
<Esc>[=5h Graphics mode 320×200 Monochrome
<Esc>[=6h Graphics mode 640×200 Monochrome
<Esc>[=13h Graphics mode 320×200 Color
<Esc>[=14h Graphics mode 640×200 16 color
<Esc>[=15h Graphics mode 640×350 2 color
<Esc>[=16h Graphics mode 640×350 16 color
<Esc>[=17h Graphics mode 640×480 2 color
<Esc>[=18h Graphics mode 640×480 16 color
<Esc>[=19h Graphics mode 320×200 256 color

ANSI Display Color and Attribute Control Codes

Background color, foreground color, and screen attribute codes may be combined in a single command. If any of the choices conflict, the rightmost color or attribute in the command is set. Not all colors or attributes are available on all displays:

<Esc>[code;...;codem Set Display Color/Attribute
0 All attributes off
1 Bold or bright characters
4 Underlined characters (monochrome display adapter only)
5 Blinking characters
7 Reverse video characters
8 Hidden or invisible characters
30 Foreground color: black
31 Foreground color: red
32 Foreground color: green
33 Foreground color: yellow
34 Foreground color: blue
35 Foreground color: magenta
36 Foreground color: cyan
37 Foreground color: white
40 Background color: black
41 Background color: red
42 Background color: green
43 Background color: yellow
44 Background color: blue
45 Background color: magenta
46 Background color: cyan
47 Background color: white

ANSI Cursor Control Codes

<Esc>[row;colH Cursor position. Moves the cursor to the specified row and col or to home (0;0) if no position is specified.
<Esc>[row;colF Cursor position. Same as <Esc>[row;colH.
<Esc>[numA Cursor up. Moves the cursor up the specified number of rows or to the first row on the screen.
<Esc>[numB Cursor down. Moves the cursor down the specified number of rows or to the last row on the screen.
<Esc>[numC Cursor right. Moves the cursor right the specified number of columns or to the right side of the screen.
<Esc>[numD Cursor left. Moves the cursor left the specified number of columns or to the left side of the screen.
<Esc>[s Saves current cursor position.
<Esc>[u Returns cursor to saved position.

ANSI Miscellaneous Display Control Codes

<Esc>[2J Clears the entire screen
<Esc>[K Clears from the cursor to the end of the line
<Esc>[=7h Enables line wrapping
<Esc>[=7l Disables line wrapping

ANSI Keyboard Layout Control Codes

By using the following ANSI command, you can redefine the entire keyboard or set simple text macros for use at the command line. Some software application programs ignore keyboard reassignments set this way.

<Esc>[keycode;keytext;...p

keycode indicates one of the key codes listed in the following table. Some keys use two codes, in which case both must be entered, separated by a semicolon as shown. Keycodes that appear in parentheses might not be available on all keyboards. (Do not include the parentheses.) Also, some keyboards use different codes for certain keys. Check your computer's documentation.

keytext is either the ASCII code for a single character or text enclosed in double quotation marks. For example, both 83 and “S” can be used to represent an uppercase S. “Hello” also is allowed; it returns the full word when the key is pressed.

In the following table, keys indicated as (gray) refer to the gray-colored cursor pad area of the keyboard. Keys indicated as (keypad) refer to the numeric keypad area. Key codes that appear in parentheses are not available on all keyboards, and ANSI.SYS might not interpret them if the /X switch is not specified in the device command that loaded it. A blank entry indicates that this keyboard combination doesn't return a valid code.

Key Alone Shift+Key Ctrl+Key Alt+Key
F1 0;59 0;84 0;94 0;104
F2 0;60 0;85 0;95 0;105
F3 0;61 0;86 0;96 0;106
F4 0;62 0;87 0;97 0;107
F5 0;63 0;88 0;98 0;108
F6 0;64 0;89 0;99 0;109
F7 0;65 0;90 0;100 0;110
F8 0;66 0;91 0;101 0;111
F9 0;67 0;92 0;102 0;112
F10 0;68 0;93 0;103 0;113
F11 0;133 0;135 0;137 0;139
F12 0;134 0;136 0;138 0;140
Home 0;71 55 0;119  
Up arrow 0;72 56 (0;141)  
PgUp 0;73 57 0;132  
Left arrow 0;75 52 0;115  
Right arrow 0;77 54 0;116  
End 0;79 49 0;117  
Down arrow 0;80 50 (0;145)  
PgDn 0;81 51 0;118  
Ins 0;82 48 (0;146)  
Del 0;83 46 (0;147)  
PrtSc   0;114  
Pause/Break   0;0  
Backspace 8 8 127 (0)
Enter 13  10 (0;28)
Tab 9 0;15 (0;148) (0;165)
Home (gray) (224;71) (224;71) (224;119) (224;151)
Up arrow (gray) (224;72) (224;72) (224;141) (224;152)
PgUp (gray) (224;73) (224;73) (224;132) (224;153)
Left arrow (gray) (224;75) (224;75) (224;115) (224;155)
Right arrow (gray) (224;77) (224;77) (224;116) (224;157)
End (gray) (224;79) (224;79) (224;117) (224;159)
Down arrow (gray) (224;80) (224;80) (224;145) (224;154)
PgDn (gray) (224;81) (224;81) (224;118) (224;161)
Ins (gray) (224;82) (224;82) (224;146) (224;162)
Del (gray) (224;83) (224;83) (224;147) (224;163)
Enter (keypad) 13  10 (0;166)
/ (keypad) 47 47 (0;142) (0;74)
* (keypad) 42 (0;144) (0;78)  
- (keypad) 45 45 (0;149) (0;164)
+ (keypad) 43 43 (0;150) (0;55)
5 (keypad) (0;76) 53 (0;143)  
A 97 65 1 0;30
B 98 66 2 0;48
C 99 67 3 0;46
D 100 68 4 0;32
E 101 69 5 0;18
F 102 70 6 0;33
G 103 71 7 0;34
H 104 72 8 0;35
I 105 73 9 0;23
J 106 74 10 0;36
K 107 75 11 0;37
L 108 76 12 0;38
M 109 77 13 0;50
N 110 78 14 0;49
O 111 79 15 0;24
P 112 80 16 0;25
Q 113 81 17 0;16
R 114 82 18 0;19
S 115 83 19 0;31
T 116 84 20 0;20
U 117 85 21 0;22
V 118 86 22 0;47
W 119 87 23 0;17
X 120 88 24 0;45
Y 121 89 25 0;21
Z 122 90 26 0;44
0 48 41  0;129
1 49 33  0;120
2 50 64 (0) Null 0;121
3 51 35  0;122
4 52 36  0;123
5 53 37  0;124
6 54 94 30 0;125
7 55 38  0;126
8 56 42  0;127
9 57 40  0;128
Spacebar 32 32   
' (single quote) 39 34  0;40
, (comma) 44 60  0;51
- (hyphen) 45 95 31 0;130
. (period) 46 62  0;52
/ (slash) 47 63  0;53
; (semicolon) 59 58  0;39
= (equal) 61 43  0;131
[ 91 123 27 0;26
(backslash) 92 124 28 0;43
] 93 125 29 0;27
` (accent) 96 126  (0;41)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.181.47