        This  article was made for the novice and the expert at ANSI.  It          is an attempt to explain how  I  use  the ANSI.SYS driver to configure          the function keys on your computer, and to control the screen.  I have          used these techniques on my computer  for  years,  and find them to be          convenient and effective.  ANSI  is not  widely  used by microcomputer          fans because the documentation supplied  by IBM on how to send control          codes to the ANSI driver is  among  the  most  cryptic  ever  produced          by IBM.  I learned them by  reading  computer  magazines,  programming          software documentation,  and slowly figured  out how it could be done.          Everything  in  this  article has been tested  before  releasing  this          article,  so if you are having problems,  please  refer  to the end of          this article to find                                                            out where to reach me.                                                                                                                                               This article  covers  only  IBM Personal Computers (PC, XT or AT)          running DOS 3.x or greater.  These  techniques  were  tested on an AST          IBM Compatible 286 and a Dell 386.  I  can  not  assure you that these          techniques will work  with  all  compatible systems,  so you may be on          your own if you try to use these techniques on a compatible computer.                                                                                                                                                                                                                                                                                                                                            Loading The ANSI Driver                                                                                                                                                                                                                                                               In order to use any of the techniques  in  this  essay,  you must          first have loaded the  ANSI.SYS  driver  into  your  computer's memory          using  your  CONFIG.SYS  file.   You  do  this  my  adding  the  line,          DEVICE=ANSI.SYS somewhere in  the  CONFIG.SYS  file and rebooting your          computer.                                                                                                                                                                                                                                                                                                                                                                                            Keyboard Reassignment with ANSI                                                                                                                                                                                                                                                   Before we get to specific ways to  send control codes to the (now          loaded) ANSI driver, you must first  know  what those codes mean.  For          the function keys  the codes  are  listed  on  the chart  below  which          first appeared in SOFTALK  magazine.  Each  function  key  is assigned          an "extended function code" which  DOS  will  use  to recognize that a          function key has been pressed and in what shifted mode,  if any.  Each          number is expressed as a 0  followed by a semi-colon,  then the number          from the chart below.  For a more  detailed  list  please refer to our          "Extended ASCII Key Codes" article, also included in this newsletter.                                                                                                        KEY     NORMAL  SHIFT   CONTROL  ALT                                            F1      59      84      94      104                                             F2      60      85      95      105                                             F3      61      86      96      106                                             F4      62      87      97      107                                             F5      63      88      98      108                                             F6      64      89      99      109                                             F7      65      90      100     110                                             F8      66      91      101     111                                             F9      67      92      102     112                                             F10     68      93      103     113                                                                                                                     Accordingly,  the way to designate the F5 key would be 0;63 while          the F10 key would be designated  by  0;68 or 0;113 if shifted with the          ALT key.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             If  you  examine  the DOS Technical  Reference  Manual  (not  the          Technical  Manual  for  PC  hardware),  you  will  find  a  section on          SCREEN/KEYS.  This section was contained in the DOS 2.0 documentation,          but IBM removed it in  later  editions.  Here  is  a  summary  of  its          contents relative to keyboard redefinition.                                                                                                                          To change one key to have the meaning of another, enter:                                                                                                                                 <Esc>[#;#p                                                                                                                             where the first # is the ASCII value of the key being changed and          the second # is the ASCII value  of  the new definition.  For example,          "A" has the ASCII value of 65 and "Q" has the value of 81.  So:                                                                                                                               <Esc>[65;81p                                                                                                                           will result in "A" being redefined as "Q." It is also possible to          redefine a key to have the meaning  of a  string  of characters.  This          is done by enclosing the string in quotes.  So:                                                                                                                                               <Esc>[65;"The Product"p                                                                                                                would change the "A" key to  have  the  meaning of "The Product".          If the first value for the first # is a 0 however, DOS knows that what          is being  changed  is  not  an  ASCII  value  but  the  meaning  of an          extended function code.  So if you were to enter:                                                                                                                                             <Esc>[0;68;"The Product"p                                                                                                              DOS would know to change the meaning of the function key (in this          case F10)  to  the  sting enclosed in  quotes.  This  is  the  key  to          redefining your function keys to perform much used commands: like DIR,          CHKDSK, COPY *.* B: etc. or to load programs from disk.                                                                                                              There is a final  trick  here.  If  you  end  the  escape command          sequence with the characters  ";13p"  instead  of just "p" the command          will self-execute, just as if you pressed the <Enter> key.                                                                                                           The IBM documentation tells the  user  to preface each command by          an <Esc> command,  and I have represented this in the above paragraphs          by writing  the characters  "<Esc>."  at  the  start  of  each control          code sequence mentioned.  Most  users assume that this  means to press          the <Esc> key on the  keyboard  when  entering  the commands.  Not so.          To get the Escape Sequence to the  ANSI driver you must enter it using          a text editor,  such  as  Q-Edit,  prompt  command  or  write  a  .COM          file.  For  example to configure  the F1  key  (extended function code          59) to have the meaning in DOS of  "autoexec"  with an <Enter> command          at the end of it you cannot type:                                                                                                                                                            <Esc>[0;59;"autoexec";13p                                                                                                               as the <Esc> will not be recognized by DOS as an escape sequence.          What DOS will recognize as an  escape  sequence is the characters "$e"          although this surely looks strange at  first.  Users familiar with the          PROMPT command will notice that the  "$"  character is what the PROMPT          command uses as an escape sequence,  and that is precisely how we will          get the  redefinition  to  be  recognized  by  DOS.  If  you enter the          following command:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           PROMPT $e[0;59;"autoexec";13p                                                                                                           you will see that it  works  perfectly.  You  now have the secret          to redefining the function keys in  DOS.  Simply write and run a batch          file with a list  of PROMPT commands and  you will have done  it.  One          precaution, ECHO must be ON,  otherwise  DOS  will suppress the PROMPT          command and the escape sequences will not get through.                                                                                                               As an example, let's create a  batch  file  called KEYON.BAT that          will set F1 as QEDIT <Enter>, F2 as TM <Enter>, F3 as THEDRAW <Enter>,          F4 as DPA <Enter>, F5 as BGANS  <Enter>,  F6  as GWS <Enter>, F7 as NU          <Enter>, F8 as DIR without the <Enter>,  F9 to run a batch file called          MENUOFF.BAT <Enter> and F10 to run a batch file called MENUON.BAT               <Enter>.  It would be as follows:                                                                                                                                            echo on                                                                         PROMPT $e[0;59;"QEDIT";13p                                                      PROMPT $e[0;60;"TM";13p                                                         PROMPT $e[0;61;"THEDRAW";13p                                                    PROMPT $e[0;62;"DPA";13p                                                        PROMPT $e[0;63;"BGANS";13p                                                      PROMPT $e[0;64;"GWS";13p                                                        PROMPT $e[0;65;"NU";13p                                                         PROMPT $e[0;66;"DIR"p                                                           PROMPT $e[0;67;"MENUOFF";13p                                                    PROMPT $e[0;68;"MENUON";13p                                                     prompt                                                                          cls                                                                                                                                                     You would  also want to  create  another  file called  KEYOFF.BAT          which resets  the function key  definitions to DOS normal.  The format          would be:                                                                                                                                                                    echo on                                                                         PROMPT $e[0;59;0;59p                                                            PROMPT $e[0;60;0;60p                                                            PROMPT $e[0;61;0;61p                                                            PROMPT $e[0;62;0;62p                                                            PROMPT $e[0;63;0;63p                                                            PROMPT $e[0;64;0;64p                                                            PROMPT $e[0;65;0;65p                                                            PROMPT $e[0;66;0;66p                                                            PROMPT $e[0;67;0;67p                                                            PROMPT $e[0;68;0;68p                                                            prompt                                                                          cls                                                                                                                                                     I should mention that  the  purpose  of  the  final  blank PROMPT          command in each of these batch files is  to reset the DOS prompt to A>          or  whatever  your  default  prompt  is.  It  serves  no  redefinition          purpose,  but does keep the screen looking clean.                                                                                                                                                                                                                                                                                                                                              Using DEBUG To Load The ANSI Driver                                                                                                                                                                                                                                             While there is no reason why  we  could not continue to configure          our function  keys  by  batch  files  consisting  of  lists  of PROMPT          commands,  this is a clumsy way to  proceed.  It  is easier to use the          DEBUG utility supplied with DOS to create  a  .COM  file  that will do          the job for you quickly and  directly,  without  sending  any input to          screen.  To my knowledge this technique was first published by Michael          J. Grabel in the December 1984 edition of PC WORLD.                                                                                                                  Place a formatted DOS disk  containing  the  DEBUG utility in the          default drive, and follow the script  below.  As you do so hexadecimal          numbers will appear  on  the  left  hand  side of your  screen.  These          numbers will  vary depending  on  the  configuration  of  your system.          For  our  purposes here I  will  represent the  numbers  in  the  form          xxxx:nnnn.  What you will see on your screen will be different.                                                                                               A>DEBUG <Enter>                                                                 -A 100 <Enter>                                                                  MOV AH,9 <Enter>                                                                MOV DX,109 <Enter>                                                              INT 21 <Enter>                                                                  INT 20 <Enter>                                                                  xxxx:nnnn DB 1B'[0;59;"QEDIT";13p' <Enter>                                      xxxx:nnnn DB 1B'[0;60;"TM";13p' <Enter>                                         xxxx:nnnn DB 1B'[0;61;"THEDRAW";13p' <Enter>                                    xxxx:nnnn DB 1B'[0;62;"DPA";13p' <Enter>                                        xxxx:nnnn DB 1B'[0;63;"BGANS";13p' <Enter>                                      xxxx:nnnn DB 1B'[0;64;"GWS";13p' <Enter>                                        xxxx:nnnn DB 1B'[0;65;"NU";13p' <Enter>                                         xxxx:nnnn DB 1B'[0;66;"DIR"p' <Enter>                                           xxxx:nnnn DB 1B'[0;67;"MENUOFF";13p' <Enter>                                    xxxx:nnnn DB 1B'[0;68;"MENUON";13p' <Enter>                                     xxxx:nnnn DB 1B '$' <Enter>                                                                                                                                            As soon as you have entered the previous line, your computer will          respond with  a  number  in  the  form  of  xxxx:nnnn.  Copy  down the          portion of the number that is being represented  here as "nnnn" as you          will need it later.  Once  you  have  copied  the  number down,  press          <Enter>.                                                                                                                                                      xxxx:nnnn <Enter>                                                               -N KEYON.COM <Enter>                                                            -R BX <Enter>                                                                                                                                                          When you have entered the command  above,  your computer will re-          spond with the following line and a colon as a prompt.  At this prompt          enter 0 and press <Enter>.                                                                                                                                    BX:0000                                                                         :0 <Enter>                                                                      -R CX <Enter>                                                                                                                                                          When you enter the R CX command above,  the computer will respond          with the following line and a colon as a prompt.  At this prompt enter          the number, "nnnn" you copied down above and press <Enter>.                                                                                                   CX 0000                                                                         :nnnn <Enter>                                                                   -W <Enter>                                                                                                                                                          The computer will respond with the following.                                                                                                               WRITING nnnn bytes                                                              -Q <Enter>                                                                                                                                                             As soon as you enter the Q command (for Quit) you will be back at          the DOS prompt, and there will be a new file on disk called KEYON.COM.          Simply type it at the DOS prompt  and  your function keys will be con-          figured.  It is a good  idea  to  use  this  same  procedure  to write          another .COM file called  KEYOFF.COM  which  will  restore the keys to          their native DOS definitions.  The procedure for  this  is the same as          the above, except that the definition section should be:                                                                                                      xxxx:nnnn DB 1B'[0;59;0;59p' <Enter>                                            xxxx:nnnn DB 1B'[0;60;0;60p' <Enter>                                            xxxx:nnnn DB 1B'[0;61;0;61p' <Enter>                                            xxxx:nnnn DB 1B'[0;62;0;62p' <Enter>                                            xxxx:nnnn DB 1B'[0;63;0;63p' <Enter>                                            xxxx:nnnn DB 1B'[0;64;0;64p' <Enter>                                            xxxx:nnnn DB 1B'[0;65;0;65p' <Enter>                                            xxxx:nnnn DB 1B'[0;66;0;66p' <Enter>                                            xxxx:nnnn DB 1B'[0;67;0;67p' <Enter>                                            xxxx:nnnn DB 1B'[0;68;0;68p' <Enter>                                            xxxx:nnnn DB 1B '$' <Enter>                                                                                                                                            If you find  that KEYON.COM doesn't  work  correctly,  reboot the          machine to clear  the  definitions  and  try  again.  The  most common          mistakes are  typing  errors  (ie. entering a colon instead of a semi-          colon,  or  misplaces  upper  or  lowercase letters.)  Another  source          of difficulty will arise if  you  have  another  file on disk to start          with called  KEYON.COM  or  KEYOFF.COM.   DEBUG  bypasses  the  normal          file allocation of DOS and writes  directly  to the disk.  If you have          another file on disk with the same name,  DEBUG will overwrite it, but          unless the other file was exactly  the  same  size  as  the new one or          smaller, there may be a piece  of  the old file left  over attached to          the end of the new one.  As  a precaution, always  erase  old versions          of the .COM files,  or  better  yet  give  each  one a unique name and          rename it later using the DOS Rename command.                                                                                                                                                                                                                                                                                                                                                                      Some Additional Tricks                                                                                                                                                                                                                                                                Here are  some  additional  control  codes for the  ANSI  driver,          summarized from the IBM material.                                                                                                                                                                                                             1. CURSOR POSITIONING                                                                                                                                               To move the cursor  to  a  specified  position: <Esc>[#;#H where the            first # is the desired line number and the second the desire column.                                                                                            To move the cursor  up  without  changing  columns: <Esc>[#A where #            specifies the number of lines moved.                                                                                                                            To move the cursor to a specified  horizontal and vertical position:            <Esc>[#;#f where # means  first  the  line  number  and secondly the            column number.                                                                                                                                                  To get a device status report: <Esc>[6n                                                                                                                         To  get  a  cursor position  report: <Esc>[#;#R  where  the first  #            specifies the current line and  the  second  # specifies the current            column                                                                                                                                                          To move the cursor  down:  <Esc>[#B  where  #  specifies  the number            of lines moved down.                                                                                                                                            To move the cursor forward:  <Esc>[#C where  #  specifies the number            of columns moved.                                                                                                                                               To move the cursor  backward: <Esc>[#D  where # specifies the number            of columns moved.                                                                                                                                               To save the cursor position: <Esc>[s and to restore it: <Esc>[u.                                                                                            2. ERASING                                                                                                                                                          To do a CLS (erase screen move cursor to home position): <Esc>[2J                                                                                               To erase from cursor to end of line: <Esc>[K                                                                                                                                                                                                3. COLOR GRAPHICS                                                                                                                                                   To set the color/graphics  attributes,  enter  <Esc>[#;#m  where the            first # is  the  desired  foreground  color  and  the  second is the            desired background color.  Select colors from the list below:                                                                                                   30  black foreground                                                            31  red foreground                                                              32  green foreground                                                            33  yellow foreground                                                           34  blue foreground                                                             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                                                                                                                                            To set additional attributes enter:  <Esc>[#m  where # is the number            of the desired attribute.  Select attributes from the list below:                                                                                               0  all attributes off (white on black)                                          1  bold on                                                                      4  underscore (on IBM Monochrome Display)                                       5  blink                                                                        7  reverse video                                                                8  invisible                                                                                                                                                       To  give  an  example of what can be done with  these  additional          codes, a batch file called MENUOFF.BAT containing only the line:                                                                                                                     PROMPT $e[2J$e[30;40m$h                                                                                                                         should blank a color display completely.  It does a CLS, sets the          display to a black foreground  and  background  and  the with the "$h"          performs a backspace to erase the  blinking cursor (the "$h command is          documented in the DOS manual under PROMPT).  Another batch file called          MENUON.BAT containing the lines:                                                                                                                                   PROMPT $e[0m                                                                    prompt                                                                          cls                                                                                                                                                               Would  reset  a  color  display  to   restore  the  screen  after          MENUOFF.BAT had been run.                                                                                                                                            Enjoy ANSI!  It is a  wonderful tool,  and can be a lot of fun to          use.  It's not a keyboard enhancer,  and  if  you  load it up with too          many  keyboard  redefinitions   at   one  time  you  will  run  out of          environment space.  This is harmless  and simply  means that  ANSI  is          full.  But it will work fine to define  your function keys and control          your screen.                                                                                                                                                 