Ansi.sys - The Commands


Ansi.sys Main Page
Entering "Escape Sequences"
Keyboard Key Codes
Index

Once Ansi.sys has been loaded, it's various functions can be accessed at any time by sending one or more "Escape Sequences" to the screen. An Escape Sequence comprises three parts:
The PrefixThis is the two characters represented by ASCII 27 followed by ASCII 91. ASCII 27 is the character returned by the "ESC" key and is represented in the tables below by ASCII 27. ASCII 91 is the left hand square bracket "[".
The CodesDepending on the actual command, these will be a series of numbers or characters that define the new screen colour, keyboard key, or whatever. The default code is 0 and, when that value is specified, can be omitted.
The SuffixA single letter (case sensitive) that defines how Ansi.sys interprets the Codes. ie. whether the cursor is to be moved, a key to be reassigned or whatever.

Note that an "Escape Sequence" cannot be entered directly - see: Entering "Escape Sequences" for further details.

Setting Text and Background Colours

ColourANSI Code
PrefixTextBackgroundSuffix
Blackleft arrow[3040m
Red3141
Green3242
Yellow3343
Blue3444
Magenta3545
Cyan3646
White3747
AttributeANSI Code
PrefixCodeSuffix
All attributes offleft arrow[0m
Bright characters1
Underlined characters4
Blinking characters5
Reverse video characters7
Hidden characters8

Notes

A series of colour/attribute commands can be combined into one by separating the codes with semicolons.

Colour changes only take effect as each character is written following the Ansi command. To change the whole screen to the new colours, add the "2J" sequence to clear the screen (see under Miscellaneous, below) or simply enter CLS on the next line.

Examples

To change the colours to blinking bright green characters on a cyan background:

ECHO %ESC%[46m %ESC%[32m %ESC[5m %ESC%[1m %ESC%[2J
or, more efficiently:
ECHO %ESC%[46;32;5;1m
CLS

Cursor Positioning

PrefixCodeSuffixCursor Position
left arrow[ r;c HMoves the cursor to line r and column c
If omitted, r and c both default to 0 (ie. the cursor moves to the top left corner of the screen)
f
nAMoves the cursor up n lines (or top of screen)
nBMoves the cursor down n lines (or bottom of screen)
nCMoves the cursor right n columns (or right hand side of screen)
nDMoves the cursor left n columns (or left hand side of screen)
sSaves current cursor position
uRestores cursor to previously saved position

Miscellaneous

PrefixCodeSuffixEffect
esc[ 2JClears the screen.1
KClears from the cursor to the end of the line
=7hEnables line wrap
=7lDisables line wrap

Notes

These two groups of commands are primarily used for constructing page headers, footers and menus. See PROMPT for further notes and examples.

1 Actually, the screen is cleared when the Code value is any integer (or is omitted).

To change the Display Mode:

PrefixCodeSuffixText Mode
left arrow[ =0h40 cols
25 lines
Monochrome
=1Colour
=280 cols
25 lines
Monochrome
=3Colour
PrefixCodeSuffixGraphics Mode
left arrow[ =5h320 x 200Monochrome
=44 Colour
=13Colour
=19256 Colour
=6640 x 200Monochrome
=1416 Colour
=15640 x 3502 Colour
=1616 Colour
=17640 x 4802 Colour
=1816 Colour

Notes

I have no real experience with these modes but they rather look as though they are a hangover from the early days of Dos. I cannot think of any situation where changing display mode to one of the above values would be to any advantage.

Keyboard Layout Codes

Ansi.sys can be used to remap the keys of the keyboard. Any key can be redefined to any one or more characters of the extended ASCII character set.
Typical applications of this facility include:

  • Assignment of "non-standard" characters to unused keys
  • Assignment of text phrases to unused keys. This can include DOS commands.
  • Complete remapping of the keyboard - for special data entry functions or obscure foreign language use, perhaps.

Syntax

left arrow[keycode;stringp

keycode The code(s) associated with the given key. Many keys use a code of two numbers separated by a semicolon and these must be used in full.
string
    This can be:
  • The ASCII code for a single character;
  • The code associated with a key; or
  • a text string (of one or more characters) enclosed in inverted commas.

Notes

  1. Most DOS programs (including EDIT) bypass the standard DOS calls when accepting keyboard input and so will ignore Ansi.sys reassignments.

  2. Have a care when assigning environmental variables to a key as these will be parsed as they are fed to the screen. Thus, assigning "ECHO %ESC%[" to a key leaves you with just "[" as the "ECHO" is cleared from the command line by %ESC%. However, it is possible to assign two keys to this: the first is assigned "ECHO %ESC", and the second is assigned "%[". Pressing the two keys sequentially puts "ECHO %ESC%[" onto the command line.

Examples

  1. To assign á to the Alt-A key:
    ESC[0;30;160p

  2. To disable Ctrl-C, assign it to ASCII 255 (Nul)
    ESC[3;255p

  3. To re-enable Ctrl-C, assign it back to 3:
    ESC[3;3p

  4. To assign "FOR %v IN ( ) DO " to Alt-F and have Alt-D append a directory listing of Drive D: to DirD.txt, you have several options including:
    1. Use EDIT to create a file called "Keys.txt" containing the following lines:

      Esc[0;33;"FOR %v IN ( ) DO "p
      Esc[0;32;"DIR D: /S >> DirD.txt";13p

      (where Esc is inserted using Ctrl-P followed by the "ESC" key).

    2. Copy Keys.txt to the screen using either:
      TYPE Keys.txt or
      COPY Keys.txt CON

    Or

    1. Use EDIT to create "Keys.bat" containing the lines:
      ECHO Esc[0;33;"FOR %v IN ( ) DO "p
      ECHO Esc[0;32;"DIR D: /S >> DirD.txt";13p

    2. Run the batch file by entering "Keys" at the command line prompt.

    Either way, the keys will now be reassigned.


Thanks to Bob Blackledge for his advice on default Code values
If you should have any comments or suggestions,
please contact: Bob Watson
.
This page last revised:
November 11, 2000.