           I WISH I COULD MAKE THIS THING DO...
                        - or -
                    Using ANSI.SYS


I hesitate to use ANSI.SYS in the title because I fear that
everyone who sees that will delegate this article to the round
file without reading it, because the subject seems to have been
beat to death.  I am registered with the Guiness Book of Records
as one of the worlds fastest page turners when I spot an article
dealing with the more arcane aspects of DOS.  The problem seems
to be not that the equipment/software is incapable of a lot of
things, but that they made it so bloody difficult to get at these
facilities.  I know that I am not the only one to feel this way,
as entire software companies have been launched on such things as
keyboard macro programs and screen format programs, yet DOS has
the software built in to do all of these things in satisfactory
fashion.

I was inspired to take another look at ANSI.SYS after a recent
column in PC Magazine published a program called ANSI.COM as a
replacement for ANSI.SYS on the premise that ANSI.COM is faster,
can be removed from memory when not being used, and supports 43
line EGA displays.  I never got much of a chance to find out if
this is true, because the program did not agree with either of
the keyboard enhancers that I normally use, and I wasn't curious
enough to disable my keyboard enhancer(s) to find out how
ANSI.COM works.  They did make the valid point, though, that most
of us don't use these things because they are too difficult to
get at.  I relate to this: as much as I enjoy digging around in
the operating system software, I have balked at using ANSI.SYS
for any of the things that it is supposed to do because one look
at the command tables (after I found out where to find them) just
gave me the impression of having to learn one more computer
language.  I'm sorry, folks, but 16 or 18 languages is enough. 
I'm not going to plow through this one just to get some screen
colors...

What turned the corner for me was not the article on ANSI.COM
itself, but a little program listing named ESCAPE buried inside
the article that allows you to send escape code sequences to ANSI
directly from the keyboard.  This doesn't sound too exciting in
itself, but allow me to explain the problem before handing out a
solution.

If you have read that ANSI.SYS can be used to set default screen
colors and redefine the keyboard, you may have actually gone to
the DOS manual to try to figure out how to do this.  Look up
ANSI.SYS.  All you will learn is that you must install ANSI.SYS
with a DEVICE command in your CONFIG.SYS file.  Not one word on
how to use it.  You may have read somewhere that to send codes to
ANSI.SYS that you can use the PROMPT command.  Look up PROMPT. 
You will find instructions on how to make "meta-strings" to send
commands to DOS to make custom DOS prompts.  (Come on, IBM, do we
need ANOTHER adluxian obfuscation? (Alright, alright - I made up
at least ONE of those words.))  Seems like we are getting farther
away from what we were trying to find out.  However, buried in
the list of "meta-strings" is what we need to talk to ANSI - a
character that sends the Escape character to ANSI.  There is even
an example of the syntax to send codes to ANSI, but after that
you are on your own - you are referred to the DOS Technical
Reference manual for a discussion of how to use ANSI.SYS.  Worse
yet, you are given none of the ANSI codes that are supposed to do
all of these wonderful things.  Well, the DOS Technical Reference
manual is only $150 or so - I guess that's worth it to get the
half dozen or so codes that I want and will never look at
again... (Seriously, look for a book by Thom Hogan called The
Programmer's PC Sourcebook from Microsoft Press ($24.95 retail)
that has all of the obscure tables from every source imaginable
including ANSI codes.)

O.K., so we now have a method to send control codes to ANSI,
albeit somewhat arcane, but if you are like me, you already have
your PROMPT set and don't want to use the PROMPT command and mess
it up with all this experimenting.  Now we finally get to the
ESCAPE program.

If you are comfortable with DEBUG, fire it up and enter the
program directly, as follows:

   -a <Enter>            ;assemble directly into memory
   xxxx:0100 mov si,0080 ;
   xxxx:0103 mov cl,[si] ;
   xxxx:0105 xor ch,ch   ;
   xxxx:0107 inc cx      ;
   xxxx:0108 mov word ptr [si],5B1B ;
   xxxx:010C lodsb       ;
   xxxx:010D int 29      ;DOS internal screen write routine
   xxxx:010F loop 010C   ;
   xxxx:0111 ret         ;
   xxxx:0112 <Enter>     ;press <Enter> - tells DEBUG you're done
   -n esc.com            ;name the program ESC.COM
   -rcx                  ;read CX register
   CX 0000               ;DEBUG replies with this
   :12                   ;set CX to save 12H bytes
   -w                    ;write the file to disk
   Writing 0012 bytes    ;DEBUG replies with this
   -q                    ;quit DEBUG - back to DOS

This will leave a small program on your disk named ESC.COM that
we will get to in a moment.

If you don't have enough aspirin to deal with DEBUG, you may send
the program to DEBUG as a Script file (ASCII text) and DEBUG will
do the work from that.  From your favorite word processor, type
the following:

   A<Enter>
   MOV SI,0080<Enter>
   MOV CL,[SI]<Enter>
   XOR CH,CH<Enter>
   INC CX<Enter>
   MOV Word Ptr [SI],5B1B<Enter>
   LODSB<Enter>
   INT 29<Enter>
   LOOP 010C<Enter>
   RET<Enter>
   (press <Enter> key once right here)
   N ESC.COM<Enter>
   RCX<Enter>
   12<Enter>
   W<Enter>
   Q<Enter>

The text does not have to be capitalized.  There do have to be
spaces where shown, however.  Press <Enter> at the end of each
line as shown to start a new line for the next command.  The
<Enter> key is pressed once between RET and N ESC.COM to send an
extra <Enter> to DEBUG in the right place.  Save the document in
your word processor's text mode or DOS text mode or non-document
mode or whatever your word processor calls and ASCII file, and
give it the name ESC.SCR.  Check ESC.SCR before sending it to
DEBUG with the TYPE command, thus:

   DOSPROMPT>type esc.scr<Enter>

the screen should look almost exactly like this:

A
MOV SI,0080
MOV CL,[SI]
XOR CH,CH
INC CX
MOV Word Ptr [SI],5B1B
LODSB
INT 29
LOOP 010C
RET

N ESC.COM
RCX
12
W
Q

If it does, the script file should work O.K.  Now, the dangerous
part.  (Not really - if it doesn't work, the worst that will
happen is that you will have to reboot and may have a strange new
file on your disk that probably won't work.  Just delete it, edit
the script file carefully for mistakes, and try again.)

Move ESC.SCR into the same directory as DEBUG.  From the DOS
prompt, enter the following command:

   DOSPROMPT>debug < esc.scr<Enter>

Things will whir and clunk for a bit, then the DOS prompt will
come back if all went well.  If all didn't go well, exercise your
favorite expletives, reboot the computer, edit the script file
for errors, and try again.  When it works, do a DIR command and
look for a new file called ESC.COM.

Now the fun part.  The ESC program may be used to send Escape
codes to ANSI, but what codes are we going to send?  -- thought
you'd never ask...

===============================================================
TABLES OF ANSI CODES TO BE USED WITH ESC.COM

Note - all numbers are DECIMAL because this is what ANSI expects.

Table A - IBM ANSI Set Graphics Rendition control sequences
          (in English, this means screen manipulation codes)

Code      What it does
----------------------------------------------------------------
0 -  All attributes off (normal white on black screen)
1 -  Bold on (high intensity)
4 -  Underline (mono screen) or Blue foreground (color screen)
5 -  Blink on
7 -  Reverse video on
8 -  "Cancelled" on (invisible characters - use for secret stuff)
30 - Black foreground (characters)
31 - Red foreground
32 - Green foreground
33 - Yellow foreground
34 - Underline (mono screen) or Blue foreground (color screen)
35 - Magenta foreground
36 - Cyan foreground
37 - White foreground
40 - Black background
41 - Red background
42 - Green background
43 - Yellow background
44 - Blue background
45 - Magenta background
46 - Cyan background
47 - White background



Table B - Video Modes (to set your screen resolutions - if you   
          have the hardware to run the mode you are trying to    
          use.)

Code      What it does
----------------------------------------------------------------
0 -  40x25 black and white
1 -  40x25 color
2 -  80x25 black and white
3 -  80x25 color
4 -  320x200 color
5 -  320x200 black and white
6 -  640x200 black and white
7 -  Wrap at end of line (DOS default - start new line if you go 
     past the end of the one you are on.
14 - 640x200 color EGA
15 - 640x350 mono EGA
16 - 640x350 color EGA
17 - 640x480 color VGA
19 - 320x200 color VGA



Table C - Extended ASCII Codes (to redefine the keyboard keys)
          <NOTE> - These codes require extended format - see text

Code      What key or key combination it belongs to
---------------------------------------------------------------
3 -       Null (does nothing)
15 -      Shift+Tab

Some of the following codes are with the SHIFT, CONTROL, or ALT
key pressed at the same time.  Read the table carefully.

ALT+key - Q   U   E   R   T   Y   U   I   O   P
Code -    16  17  18  19  20  21  22  23  24  25

ALT+key - A   S   D   F   G   H   J   K   L
Code    - 30  31  32  33  34  35  36  37  38

ALT+key - Z   X   C   V   B   N   M
Code    - 44  45  46  47  48  49  50

Key     - F1  F2  F3  F4  F5  F6  F7  F8  F9  F10  F11  F12
Code    - 59  60  61  62  63  64  65  66  67  68   133  134

SHFT+key- F1  F2  F3  F4  F5  F6  F7  F8  F9  F10  F11  F12
Code    - 84  85  86  87  88  89  90  91  92  93   135  136

CTRL+key- F1  F2  F3  F4  F5  F6  F7  F8  F9  F10  F11  F12
Code    - 94  95  96  97  98  99  100 101 102 103  137  138

ALT+key - F1  F2  F3  F4  F5  F6  F7  F8  F9  F10  F11  F12
Code    - 104 105 106 107 108 109 110 111 112 113  139  140

NOTE - Extended ASCII code for use with this table is a two-byte
       code: the byte 00 followed by ";", then the code in this  
     table.  Ex: SHFT+F1 is 00;84.  CTRL+F1 is 00;94

===============================================================


Note that Table C is incomplete, but I have provided the best
keyboard candidates for reassignment, and that there are enough
clues to find the rest of the key codes.

Okay so now what do we do with this?  Well for starters, notice
that DOS make almost no use of the function keys.  There is a
small command line editor assigned to F1 through F4 that almost
no one uses because almost no one knows that it is there.  Try
this - if you have previously put something in the keyboard
buffer (typed in something and pressed <Enter>), the function
keys will do the following for you:

F1 = puts next character from the command buffer on the command  
     line.

F2 = puts all characters from the command buffer up to the next  
     character you type on the command line (e.g. <F2> <s>       
     supplies all characters up to "s").

F3 = puts all characters from the command buffer on the command  
     line. (this is the one everyone has seen)

F4 = Skips all characters from the command buffer up to the next 
     character you type (e.g. <F4> <s> skips to "s").

Ins= Insert a character at the current spot in command buffer.

Del= Delete a character at the current spot in command buffer.

Try this - from the DOS prompt, type in the following:

   DOSPROMPT>difr b:<Enter>      (yes, including the mistake)

For those of you who are perfect, you've never seen the error
message;

   Bad Command or Filename

If I haven't seen it today, its because my computer is unplugged.

Normally, you would have to retype the command, but try this -
press the <F2> key then the <f> key.  The command line now looks
like this:

   DOSPROMPT>di_

The old command line has been placed on the new command line up
to the wrong character (f).  Press the DEL key on the keyboard. 
You just erased the "f" from the command buffer.  Now press the
<F3> key to get the rest of the line.  The command line now looks
like this:

   DOSPROMPT>dir b:

How 'bout that!! correct spelling!  When you press <Enter> the
command will execute. (and you will probably get a new error
because you forgot to put a disk in drive b:)

Trivial example, but try it on something like
CD\DEEP\DARK\DANK\DUNGEON and it saves a lot of retyping.  Only
four commands to learn and it neatly covers 95 percent of the
mistakes I make.  Plus you now find that you don't have to spend
$50 on that command line editor program that will take ANOTHer
20K of memory away from you.

ESC.COM will save you from having to deal with most of the
convolutions of PROMPT commands, and can be tried out from the
keyboard without affecting anything else except what the codes
are intended to affect.  Make sure that ESC.COM is in the current
directory, and we'll play.  A couple of screen examples - from
the DOS prompt, try the following command:

   DOSPROMPT>esc 4m<Enter>   (note the lowercase "m")

The lowercase "m" tells ANSI that the number is supposed to go to
the screen ("m" for "monitor"?).  If you have a color monitor,
you will now see blue characters on a black background.  A mono
monitor will show the same old black and white (or amber), but
the characters will now all be underlined.  do a DIR command and
see what it looks like.

Okay, try this.  From the DOS prompt:

   DOSPROMPT>esc 5m<Enter>    (still note lowercase "m")

Now everything after the last command line should be blinking
(color and mono monitor).  Do a DIR command.  EVERYthing will be
blinking. blinking. blinking. blinking.  Okay, enough of that -
what is the code for Stop Blinking?  Hmmm, nothing in the Table
for that.  There's no 6 code listed, let's try that:

   DOSPROMPT>esc 6m<Enter>

Rats! - no change... how am I going to fix this?  Well, now for
one of ANSI's little mysteries - there are not any codes for
turning a lot of attributes off individually, such as blink, so
you have to turn ALL attributes off, then turn on the ones you
want to keep.  From the DOS prompt:

   DOSPROMPT>esc 0m<Enter>

Ahhh!  Normal screen again.  What else can we get?  Let's try
this...

   DOSPROMPT>esc 33m<Enter>

Yellow Characters! Not bad... try another:

   DOSPROMPT>esc 44m<Enter>

Yellow characters on a blue background!  I could get to like
this.  (Notice that the attributes are cumulative.  The
foreground color was kept, and the background color was just
added to the screen around it. try:

   DOSPROMPT>esc 37m<Enter>

White characters on a blue background.  Easy on the eyes.  What
happens if I try this:

   DOSPROMPT>esc 34m<Enter>

Wait. Whoa! WHAT HAPPENED!!  I can't read ANYTHING now! - well,
that's what happens when you run blue on blue.  It makes it a
little tough to read the characters.  All is not lost, though. 
Type (carefully) (you won't see this on the screen):

             esc 0m<Enter>

and you will be back to good old white on black.  This brings up
a point, however.  Esc 0m is about the only way you have of
resetting attributes.  Erase them all and start over.  Try the
following codes:

   DOSPROMPT>esc 33;5;41m<Enter>

Yellow characters on a red background blinking blinking
blinking...  This is a bit much - let's turn off the blinking,
and keep the colors.  Type in the following command:

   DOSPROMPT>esc 0;33;41m<Enter>

Whew!  Much better.  Colors without the blinking.  A minor
annoyance to have to reset the colors just to get rid of blinking
or bright attributes, but the only things you have to restore are
a foreground and a background color.

Put your favorites in a AUTOEXEC.BAT, and DOS will boot up in
color! and stay that way, even after exiting a program back to
DOS.  If the program is one of those that resets the screen
attributes to 0, fix its wagon with a CLS command from DOS, and
you will have your screen colors back.

Okay what's next?

Hey Bunky!  What's That?  You say that you have typed DIR /P so
many times that you can't read the keytops anymore?  You say that
you put the keyboard through the screen after misspelling
CD\WAY\DOWN\DEEP\SUBDIRECTORY four times?  You say that you can't
remember the syntax for that BACKUP command, and you keep sending
your backup files to the printer?? Is that what's troubling you,
Bunky???  Well, FEAR NOT, friends of the floppy persuasion, help
is here, and it doesn't cost $49.95; it doesn't cost $39.95; it
doesn't even cost $29.95; just sent $19.95 to... never mind, DOS
will do it for free.  Watch this - from the DOS prompt:

   DOSPROMPT>esc 0;63;"dir/p";13p<Enter>

What!  Nothing happened!  (I hope not, anyway.)  Well what DID we
do, then?  It goes like this...

"esc" is the escape program.
"0"   is the extended ASCII code we sent ANSI for the keyboard.
"63"  is the key code for the F5 key (check the table above).
"dir/p" is the command we want the F5 key to pass to DOS.
"13"  is the ASCII code for <Enter>.
"p"   is the code to send the numbers to the keyboard routines.

Look at the command line syntax.  There is a space between "esc"
and the first character, and there is a ";" between each of the
elements.  Press the <F5> key and see what happens.  Wow! a one-
key directory command!  (If nothing happens, check your command
line entry for mistakes - remember the command line editor on F1
through F4?  That's why we put the new command on F5.)  How about
that thorny pathname that is so long?  Try this:

   DOSPROMPT>esc 0;64;"cd\way\deep\down\subdirectory";13p<Enter>

Press the <F6> key, and you should now be in that subdirectory.

How about that Backup command that you can never quite remember
the syntax for?  Dig out the DOS manual, look up those command
switches for the last time, and try this:

   DOSPROMPT>esc 0;113;"backup c:\subdir a:/s/d";13p<Enter>


This puts the BACKUP command on the <ALT><F10> key, where you are
not likely to press it accidentally.

I think you are getting the idea now, but let me leave you with a
couple of things to avoid trouble.  Don't use <CTRL> <letter-key>
combinations for reassignment.  Several of these are used by DOS
for terminal commands (<CTRL><S>, <CTRL><Q>, <CTRL><C>, etc.) and
used by many programs for the same functions, and programs may
not operate correctly without these available the way DOS assigns
them.  Not to overlook the obvious, don't reassign unshifted or
shifted letter keys.  On the other hand, you might take a stab at
creating your own Dvorak keyboard.

DOS assigns only 8 or so of the 40 possible function key
combinations (the 10 function keys, and <CTRL> or <ALT> or <SHFT>
plus the 10 function keys) so most of these are available, but
take care not to reassign so many that you can't remember what
they are.  A solution that is more complicated than the problem
is not a solution.  I am using a scheme where the function key
combinations are used for DOS commands, and the <ALT><letter-
keys> are used for programs, i.e. <ALT><W> is WORDWIMP word
processor, <ALT><L> is LIMPWIMP spreadsheet, <ALT><D> is DATAWIMP
database.  As a farther example, you can put the word processor
subdirectory in your PATH command so that you can run your word
processor from any subdirectory, and what's more, you can run it
with a single keypress combination!

ANSI is finally out of the closet for me.  The ESC.COM program
makes it accessible with a minimum of fuss - just look up the
codes that do what you want and send them to ANSI with ESC.COM. 
Play with it from the keyboard, and put the codes you like in
your AUTOEXEC.BAT file, so they are automatically installed when
you start up your computer.  ESC.COM also works with FANSI,
NANSI, and ZANSI, some common ANSI.SYS substitutes.  Don't let
your wimpy keys push you around - push them back, and get more
from them!


                                      - Jan Fagerholm
                                        PC Clubhouse
                                        BBS (415) 581-8529
                                        BBS II (415) 357-9577
                                        Compuserve 75755,376
                                        GEnie mail jfagerholm
                                                                                