CIS 415L
Telecommunications and Networking

Instructor: Michael P. Harris

ANSI.SYS Escape Sequences


Download:   TheDraw.zip (v4.63)
TheDraw is a powerful screen image file generator for color and monochrome video users (using ANSI control codes). What does that mean? Literally, TheDraw makes designing and figuring out a video screen display ,using ANSI escape characters, childs play.

To obtain local full screen and keyboard control of a remotely connected terminal or computer in telecommunications a control character oriented keyboard and screen control system is necessary. This tutorial describes how the ANSI escape sequences are used in MSDOS. An ANSI escape sequence is a series of characters, beginning with the Escape character, used to define functions to MSDOS. You can use these escape sequences to affect cursor movement, erase information from the screen display, set the graphics mode, and reassign keys.

There are several ways to execute ANSI escape sequences:
  1. You can enter them at the MSDOS command line using the PROMPT command.
  2. You can create them in a file and then issue them with the TYPE command.
  3. You can include escape sequences in your QBASIC/GWBASIC or assembly language programs.

Before issuing and ANSI escape sequences, you must load the special device driver ANSI.SYS. To do this, create or edit your CONFIG.SYS file in the root directory of the MSDOS Boot drive (C:\) and add the following command to it.

DEVICE=C:\DOS\ANSI.SYS     or
DEVICE=C:\WINDOWS\COMMAND\ANSI.SYS

When you restart MSDOS, the ANSI.SYS driver extended screen and keyboard control driver will be installed

ANSI ESCAPE SEQUENCE SYNTAX:
  • Each ANSI escape sequence has the same basic syntax:
  • ESC[parameters-command (spaces added for readability)

ESC is the 1 byte code for escape. The escape code is $e at the command line using PROMPT. In QBASIC or GWBASIC the code for escape is CHR$(27). In ASCII, ESC is 27 decimal or 1B hexadecimal.

ANSI CURSOR CONTROL FUNCTIONS:

ESC[#;#H - Cursor Position (CUP)

The first # specifies the line number, the second # specifies the column. The default for both is 1

ESC[#A - Cursor Up (CUU)

Moves the cursor UP # number of lines

ESC[#B - Cursor Down (CUD)

Moves the cursor DOWN # number of lines

ESC[#C - Cursor Forward (CUF)

Moves the cursor RIGHT # number of columns

ESC[#D - Cursor Backward (CUB)

Moves the cursor LEFT # number of columns

ESC[#;#f - Horizontal & Vertical Position

(same as ESC[#;#H)

ESC[s - Save Cursor Position (SCP)

The current cursor position is saved. The cursor position can be restored using (RCP) ESC[u.

ESC[u - Restore cursor position (RCP)

Restores the cursor position saved with the (SCP) sequence ESC[s.

ESC[2J - Erase Display (ED)

Clears the screen and moves to the home position

ESC[K - Erase Line (EL)

Clears the current line from the cursor position

ESC[#;...;#m - Set Graphics Rendition (SGR)

(optional text may be included in the ...)

The Escape parameters for (SGR) are:

  • 0 All attributes OFF
  • 1 Bold ON
  • 4 Underscore ON (monochrome only)
  • 5 Blink ON
  • 7 Reverse Video ON
  • 8 Concealed ON
  • 30 Black Foreground
  • 31 Red
  • 32 Green
  • 33 Yellow
  • 34 Blue
  • 35 Magenta
  • 36 Cyan
  • 37 White
  • 40 Black Background
  • 41 Red
  • 42 Green
  • 43 Yellow
  • 44 Blue
  • 45 Magenta
  • 46 Cyan
  • 47 White
  • ESC[=#h Set Mode (SM)

    Escape parameters for Set Mode (SM)

    040x25 black& white
    140x25 color
    280x25 black & white
    380x25 color
    4320x200 color
    5320x200 black & white
    6640x200 black & white
    7wrap at end of line

    ESC[=#I Reset Mode (RM)

    Uses same parameters as Set Mode (SM)

    ESC[=#;#...#p or ESC[string--Keyboard Reasignment

    The first # is the ASCII code for the key being changed, the second # or "string" is the replacement, with one exception - if the first # is a NULL then the first and second # represent the key being reassigned.

    EXAMPLES:
    PROMPT $e[7m$n$p$e[mcreates a standard prompt $n$g except
    in reverse video.
    PROMPT $p$g$e[33;44m- on a color video, this will set the prompt to display
    the directory $p$g and display it in red against blue.
    ESC[65;81p- reassigns A to become Q
    ESC[0;68;"DIR";13p- reassigns the F10 key to DIR + [CR]
    ESC[2J- clear the screen & home cursor
    ESC[7m- all subsequent characters in reverse video
    ESC[7;5m- all subsequent characters reverse and blinking
    ESC[13;40f- move cursor to center of screen

    ASCII Code-- Extended Key

    16 - 25ALT Q,W,E,R,T,Y,U,I,O,P
    30 - 38ALT A,S,D,F,G,H,J,K,L
    44 - 40ALT Z,X,C,V,B,N,M
    59 - 68[F1] through [F10]
    84 - 93SHIFT[F1] through SHIFT[F10]
    94 - 103[F1] through [F10]
    104 - 113ALT[F1] through ALT[F10]

    Using the above ASCII Code to Extended Key conversion table and the ANSI.SYS redefine key syntax:

    prompt $e[0;##;"command"p {without trailing ENTER}

    or

    prompt $e[0;##;"command";13p {with trailing ENTER}

    • where ## = the ASCII code of the key to redefine and
    • command = the command to assign to the Extended Key

    Create a batch file to redefine the following DOS commands to keyboard keys.

    ALT-DDIR [ENTER]
    ALT-BDIR B: [ENTER]
    ALT-WDIR /W [ENTER]
    ALT-SDIR | SORT | MORE
    ALT-TTYPE
    ALT-CCOPY
    ALT-PPRINT
    ALT-RRENAME
    F4]DISKCOPY A: B:
    [F5]FORMAT B:
    [F10]CLS

    REMEMBER TO END THE BATCH FILE WITH "PROMPT $P$G" TO RETURN THE PROMPT TO NORMAL.

    [TOP of Page]

    This page is maintained by: Michael P. Harris

    [Viking Home Page] viking.delmar.edu
    Last modified Mar 10
    [E-mail] mpharris@surf.delmar.edu
    Copyright © 1997