CFOG's PIP, April 1990, Volume 9 No. 2, Whole No. 74, page 18

Console Help

by Allen J. Browne

[This is a reprint adapted from the January 1990 issue of TOGGLE, the newsletter for Tacoma-Seattle CP/M and MS-DOS computer users. -- bhc]

DOS screen work is not noted for its aesthetic appeal. Your color screen represents a significant investment, but you are probably not getting much color at the DOS command level. Even we monochrome purists have screens which could display text in reverse video or high intensity, but I have seen very few batch files which actually do so.

Gaining control of keyboard and screen attributes is neither difficult nor expensive. The ANSI.SYS file came with your computer. The documentation for ANSI.SYS is sketchy and lacking in examples. The following ideas may help.

First, to ensure that ANSI.SYS is active check your CONFIG.SYS file for a line that looks like this:

DEVICE=ANSI.SYS

If it's not there, add it and reboot your computer before trying the tips that follow.

The Colorful Screen

To alter screen attributes, ANSI.SYS wants an ESCape Sequence, a sequence of keystrokes that starts with an [ESC]ape character.

For example, to achieve reverse video, ANSI.SYS wants four characters:

[ESC][7m

That's an [ESC]ape, a left square bracket -- [ --, the numeral 7, and a lower case letter m. Put these four characters in a file, let's call it REVERSE.TXT, TYPE it to the screen, and whatever follows will be in reverse video. Things will stay that way until ANSI.SYS sees the three character cancel reverse video command:

[ESC][m

You may be able to insert control characters in files with your usual editor or word processor. With Sidekick and other Borland editors, press CTRL+P and then (without releasing CTRL) hit [ESC]. [WordStar and VDE will do this, too, even if you do release the CTRL before pressing [ESC]. -- bhc] Edlin will embed an [ESC]ape character in a file if you press CTRL+V followed by a left square bracket.

Once you have the REVERSE.TXT file you can use it to create text files with any screen attributes you want. To create a batch file containing a warning message in reverse video, enter ECHO, a space, read in the REVERSE.TXT file, enter the message you want on the screen, and follow it with the cancel reverse sequence:

ECHO [ESC][7m WARNING: Any files of the same name will be destroyed [ESC][m

To vary the effect, try these:

2 = faint
3 = italics
4 = underscored
5 = blink
6 = rapid blink
1 = high intensity

Colors 30 to 37 are for foreground color and 40 to 47 are for background color. The second digit corresponds to the following colors:

0 = black
1 = red
2 = green
3 = yellow
4 = blue
5 = magenta
6 = cyan
7 = white

You can change both foreground and background colors by separating them with a semi-colon:

[ESC][33,42m

puts yellow characters on a green background.

If you have more than two or three ECHO statements in a row, it becomes more efficient to collect them into a short text file and TYPE the file instead of watching your ECHOes gradually dribble onto the screen. So text files are an obvious target for ANSI.SYS [ESC]ape sequences. For example, if your AUTOEXEC.BAT file assigns a logical drivename E: to the C:\WORDSTAR directory, and defines function key F9 as DIR /P and F10 as MENU you can create a file called START.MSG with a message like this:

 FREDDIE THE COP'S COMPUTER
F9 = DIR /P
F10= MENU
LOGICAL DRIVE E: =C:\WORDSTAR

Now you know how to set this message up so with lines of asterisks above and below the text in reverse, the second line in blinking magenta, and lines 3-5 in magenta on white.

Defining Function Keys

Defining function keys is also done through ANSI.SYS. The job can be done as a series of PROMPT commands. For several definitions it is much faster to enter them into a file and TYPE it. The general format is:

[ESC][<key_value>;<redefinition>p

The key_value is a two byte number for function keys, starting with 0;59 for F1, 0;60 for F2, and so on through 0;68 for F10. For the redefinition, enter the command in quotation marks or as a series of ASCII values separated by semicolons. For example:

[ESC][0;50;type C:\START.MSG;13p
[ESC][0;67;DIR /P;13p
[ESC][0;68;MENU;13p[ESC][3A
[ESC][1mFUNCTION 4 KEYS DEFINED
[ESC][m(Press F1 for HELP)

[The last two lines should be on one, but our format isn't wide enough. -- bhc]

Now pressing F1 will display your START.MSG help file on the screen, F9 will display the directory contents, and F10 will load your menu.

The 13 at the end of the line before the "p" is the ASCII code for ^M or a [RETURN]. The [ESC][3A at the end of the third line moves the cursor up three lines since the first three lines won't display anything on the screen but will move the cursor down. This way the final line will appear as if it were the only line in the file. The first part of the last line in high intensity.

Just as [ESC][nA moves the cursor up n lines, [ESC][nB moves the cursor down n lines, [ESC][nC moves the cursor right n columns, and [ESC][nD moves the cursor left (or backwards) n columns. You can clear the screen with [ESC][2J and then move the cursor to the location you want with [ESC][h#;v#F, where h# is the horizontal location you want and v# is the vertical location you want. You can use [ESC][K to erase the current line.

Having created a file to define your function keys and whatever screen colors you desire, include TYPE DEFINE.KEY in your AUTOEXEC.BAT file. You'll be amazed how quickly these definitions will execute. If you need to undefine the keys again (for programs such as Norton Utilities which are sensitive to 'DOS's definitions, your should create an UNDEFINE.KEY file that's something like this:

[ESC][0;59;0;59p[ESC][0;67;
0;67p[ESC][0;68;0;68p
[ESC][1mFUNCTION KEYS
NOW UNDEFINED[ESC][m

[Again, each of the above pairs of two lines should be on one line, but our format isn't wide enough. -- bhc]

If you become addicted to function key programming, you may want to set up SHIFT function keys. The codes for those start with 0;84 for SHIFT-F1, 0;85 for SHIFT-F2, etc. For CTRL function keys, start with 0;94 for CTRL-F1, 0;95 for CTRL-F2, etc. The ALT function key sequence starts at 0;104 for ALT-F1, 0;105 for ALT-F2, etc.

The Extended Keyboard

For those who own an extended keyboard whith F11 and F12 function keys, try entering 0;133 for F1 and 0;134 for F12. This will work with some XTs and ATs, but others need a patch to drive those keys. For those addicts who must know, SHIFT-F11 is 0;135, SHIFT-F12 is 0;136, CTRL-F11 is 0;137, CTRL-F12 is 0;138, ALT-F11 is 0;139, and ALT-F12 is 0;140.

Patch for F11 and F12

[If your machine won't respond to the values given for F11 and F12, there's a GWBASIC program listed in the original article. For a copy, send a stamped addressed business size envelope to PIP, Box 1674, Chicago 60690.]