Subsections

- 1 Types, Variables, Constants
- 2 Procedures and Functions
  - 1 AssignCrt
  - 2 CursorBig
  - 3 ClrEol
  - 4 ClrScr
  - 5 CursorOff
  - 6 CursorOn
  - 7 Delay
  - 8 DelLine
  - 9 GotoXY
  - 10 HighVideo
  - 11 InsLine
  - 12 KeyPressed
  - 13 LowVideo
  - 14 NormVideo
  - 15 NoSound
  - 16 ReadKey
  - 17 Sound
  - 18 TextBackground
  - 19 TextColor
  - 20 TextMode
  - 21 WhereX
  - 22 WhereY
  - 23 Window

#
1 The CRT unit.

crtex This chapter describes the `CRT` unit for Free Pascal, both under DOS LINUX and WINDOWS. The unit was first written for DOS by Florian klämpfl. The unit was ported to LINUX by Mark May, and enhanced by Michaël Van Canneyt and Peter Vreman. It works on the LINUX console, and in xterm and rxvt windows under X-Windows. The functionality for both is the same, except that under LINUX the use of an early implementation (versions 0.9.1 and earlier of the compiler) the crt unit automatically cleared the screen at program startup. This chapter is divided in two sections.

- The first section lists the pre-defined constants, types and variables.
- The second section describes the functions which appear in the interface part of the CRT unit.

# 1 Types, Variables, Constants

Color definitions :

      Black = 0;
      Blue = 1;
      Green = 2;
      Cyan = 3;
      Red = 4;
      Magenta = 5;
      Brown = 6;
      LightGray = 7;
      DarkGray = 8;
      LightBlue = 9;
      LightGreen = 10;
      LightCyan = 11;
      LightRed = 12;
      LightMagenta = 13;
      Yellow = 14;
      White = 15;
      Blink = 128;

Miscellaneous constants

      TextAttr: Byte = $07;
      TextChar: Char = ' ';
      CheckBreak: Boolean = True;
      CheckEOF: Boolean = False;
      CheckSnow: Boolean = False;
      DirectVideo: Boolean = False;
      LastMode: Word = 3;
      WindMin: Word = $0;
      WindMax: Word = $184f;
      ScreenWidth = 80;
      ScreenHeight = 25;

Some variables for compatibility with Turbo Pascal. However, they're not used by Free Pascal.

    var
      checkbreak : boolean;
      checkeof : boolean;
      checksnow : boolean;

The following constants define screen modes on a DOS system:

    Const
      bw40 = 0;
      co40 = 1;
      bw80 = 2;
      co80 = 3;
      mono = 7;

The `TextAttr` variable controls the attributes with which characters are written to screen.

    var TextAttr : byte;

The `DirectVideo` variable controls the writing to the screen. If it is `True`, the the cursor is set via direct port access. If `False`, then the BIOS is used. This is defined under DOS only.

    var DirectVideo : Boolean;

The `Lastmode` variable tells you which mode was last selected for the screen. It is defined on DOS only.

    var lastmode : Word;

# 2 Procedures and Functions

##
1 AssignCrt

Declaration
  `Procedure AssignCrt (Var F: Text);`
Description
  AssignCrt Assigns a file F to the console. Everything written to the file F goes to the console instead. If the console contains a window, everything is written to the window instead.

Errors
  None.
See also
  Window

Example

      Program Example1;
      uses Crt;

      { Program to demonstrate the AssignCrt function. }

      var
        F : Text;
      begin
        AssignCrt(F);
        Rewrite(F); { Don't forget to open for output! }
        WriteLn(F,'This is written to the Assigned File');
        Close(F);
      end.

##
2 CursorBig

Declaration
  `Procedure CursorBig ;`
Description
  Makes the cursor a big rectangle. Not implemented on LINUX.
Errors
  None.
See also
  CursorOn, CursorOff

##
3 ClrEol

Declaration
  `Procedure ClrEol ;`
Description
  ClrEol clears the current line, starting from the cursor position, to the end of the window. The cursor doesn't move
Errors
  None.
See also
  DelLine, InsLine, ClrScr

Example

      Program Example9;
      uses Crt;

      { Program to demonstrate the ClrEol function. }

      begin
        Write('This line will be cleared from the',
              ' cursor postion until the right of the screen');
        GotoXY(27,WhereY);
        ReadKey;
        ClrEol;
        WriteLn;
      end.

##
4 ClrScr

Declaration
  `Procedure ClrScr ;`
Description
  ClrScr clears the current window (using the current colors), and sets the cursor in the top left corner of the current window.
Errors
  None.
See also
  Window

Example

      Program Example8;
      uses Crt;

      { Program to demonstrate the ClrScr function. }

      begin
        Writeln('Press any key to clear the screen');
        ReadKey;
        ClrScr;
        Writeln('Have fun with the cleared screen');
      end.

##
5 CursorOff

Declaration
  `Procedure CursorOff ;`
Description
  Switches the cursor off (i.e. the cursor is no longer visible). Not implemented on LINUX.
Errors
  None.
See also
  CursorOn, CursorBig

##
6 CursorOn

Declaration
  `Procedure CursorOn ;`
Description
  Switches the cursor on. Not implemented on LINUX.
Errors
  None.
See also
  CursorBig, CursorOff

##
7 Delay

Declaration
  `Procedure Delay (DTime: Word);`
Description
  Delay waits a specified number of milliseconds. The number of specified seconds is an approximation, and may be off a lot, if system load is high.
Errors
  None
See also
  Sound, NoSound

Example

      Program Example15;
      uses Crt;

      { Program to demonstrate the Delay function. }
      var
        i : longint;
      begin
        WriteLn('Counting Down');
        for i:=10 downto 1 do
         begin
           WriteLn(i);
           Delay(1000); {Wait one second}
         end;
        WriteLn('BOOM!!!');
      end.

##
8 DelLine

Declaration
  `Procedure DelLine ;`
Description
  DelLine removes the current line. Lines following the current line are scrolled 1 line up, and an empty line is inserted at the bottom of the current window. The cursor doesn't move.
Errors
  None.
See also
  ClrEol, InsLine, ClrScr

Example

      Program Example10;
      uses Crt;

      { Program to demonstrate the InsLine function. }

      begin
        ClrScr;
        WriteLn;
        WriteLn('Line 1');
        WriteLn('Line 2');
        WriteLn('Line 2');
        WriteLn('Line 3');
        WriteLn;
        WriteLn('Oops, Line 2 is listed twice,',
                ' let''s delete the line at the cursor postion');
        GotoXY(1,3);
        ReadKey;
        DelLine;
        GotoXY(1,10);
      end.

##
9 GotoXY

Declaration
  `Procedure GotoXY (X: Byte; Y: Byte);`
Description
  Positions the cursor at `(X,Y)`, `X` in horizontal, `Y` in vertical direction relative to the origin of the current window. The origin is located at `(1,1)`, the upper-left corner of the window.

Errors
  None.
See also
  WhereX, WhereY, Window

Example

      Program Example6;
      uses Crt;

      { Program to demonstrate the GotoXY function. }

      begin
        ClrScr;
        GotoXY(10,10);
        Write('10,10');
        GotoXY(70,20);
        Write('70,20');
        GotoXY(1,22);
      end.

##
10 HighVideo

Declaration
  `Procedure HighVideo ;`
Description
  HighVideo switches the output to highlighted text. (It sets the high intensity bit of the video attribute)

Errors
  None.
See also
  TextColor, TextBackground, LowVideo, NormVideo

Example

      Program Example14;
      uses Crt;

      { Program to demonstrate the LowVideo, HighVideo, NormVideo functions. }

      begin
        LowVideo;
        WriteLn('This is written with LowVideo');
        HighVideo;
        WriteLn('This is written with HighVideo');
        NormVideo;
        WriteLn('This is written with NormVideo');
      end.

##
11 InsLine

Declaration
  `Procedure InsLine ;`
Description
  InsLine inserts an empty line at the current cursor position. Lines following the current line are scrolled 1 line down, causing the last line to disappear from the window. The cursor doesn't move.
Errors
  None.
See also
  ClrEol, DelLine, ClrScr

Example

      Program Example10;
      uses Crt;

      { Program to demonstrate the InsLine function. }

      begin
        ClrScr;
        WriteLn;
        WriteLn('Line 1');
        WriteLn('Line 3');
        WriteLn;
        WriteLn('Oops, forgot Line 2, let''s insert at the cursor postion');
        GotoXY(1,3);
        ReadKey;
        InsLine;
        Write('Line 2');
        GotoXY(1,10);
      end.

##
12 KeyPressed

Declaration
  `Function KeyPressed : Boolean;`
Description
  The Keypressed function scans the keyboard buffer and sees if a key has been pressed. If this is the case, `True` is returned. If not, `False` is returned. The `Shift, Alt, Ctrl` keys are not reported. The key is not removed from the buffer, and can hence still be read after the KeyPressed function has been called.

Errors
  None.
See also
  ReadKey

Example

      Program Example2;
      uses Crt;

      { Program to demonstrate the KeyPressed function. }

      begin
        WriteLn('Waiting until a key is pressed');
        repeat
        until KeyPressed;
       { The key is not Read,
         so it should also be outputted at the commandline}
      end.

##
13 LowVideo

Declaration
  `Procedure LowVideo ;`
Description
  LowVideo switches the output to non-highlighted text. (It clears the high intensity bit of the video attribute)

Errors
  None.
See also
  TextColor, TextBackground, HighVideo, NormVideo

For an example, see HighVideo

##
14 NormVideo

Declaration
  `Procedure NormVideo ;`
Description
  NormVideo switches the output to the defaults, read at startup. (The defaults are read from the cursor position at startup)

Errors
  None.
See also
  TextColor, TextBackground, LowVideo, HighVideo

For an example, see HighVideo

##
15 NoSound

Declaration
  `Procedure NoSound ;`
Description

  Stops the speaker sound. This call is not supported on all operating systems.

Errors
  None.
See also
  Sound

Example

      Program Example16;
      uses Crt;

      { Program to demonstrate the Sound and NoSound function. }

      var
        i : longint;
      begin
        WriteLn('You will hear some tones from your speaker');
        while (i<15000) do
         begin
           inc(i,500);
           Sound(i);
           Delay(100);
         end;
        WriteLn('Quiet now!');
        NoSound; {Stop noise}
      end.

##
16 ReadKey

Declaration
  `Function ReadKey : Char;`
Description

  The ReadKey function reads 1 key from the keyboard buffer, and returns this. If an extended or function key has been pressed, then the zero ASCII code is returned. You can then read the scan code of the key with a second ReadKey call. Remark. Key mappings under Linux can cause the wrong key to be reported by ReadKey, so caution is needed when using ReadKey.

Errors
  None.
See also
  KeyPressed

Example

      Program Example3;
      uses Crt;

      { Program to demonstrate the ReadKey function. }

      var
        ch : char;
      begin
        writeln('Press Left/Right, Esc=Quit');
        repeat
          ch:=ReadKey;
          case ch of
           #0 : begin
                  ch:=ReadKey; {Read ScanCode}
                  case ch of
                   #75 : WriteLn('Left');
                   #77 : WriteLn('Right');
      	    end;
      	  end;
          #27 : WriteLn('ESC');
          end;
        until ch=#27 {Esc}
      end.

##
17 Sound

Declaration
  `Procedure Sound (hz : word);`
Description
  Sounds the speaker at a frequency of `hz`. Under WINDOWS, a system sound is played and the frequency parameter is ignored. On other operating systems, this routine may not be implemented.
Errors
  None.
See also
  NoSound

##
18 TextBackground

Declaration
  `Procedure TextBackground (CL: Byte);`
Description

  TextBackground sets the background color to `CL`. `CL` can be one of the predefined color constants.

Errors
  None.
See also
  TextColor, HighVideo, LowVideo, NormVideo

Example

      Program Example13;
      uses Crt;

      { Program to demonstrate the TextBackground function. }

      begin
        TextColor(White);
        WriteLn('This is written in with the default background color');
        TextBackground(Green);
        WriteLn('This is written in with a Green background');
        TextBackground(Brown);
        WriteLn('This is written in with a Brown background');
        TextBackground(Black);
        WriteLn('Back with a black background');
      end.

##
19 TextColor

Declaration
  `Procedure TextColor (CL: Byte);`
Description

  TextColor sets the foreground color to `CL`. `CL` can be one of the predefined color constants.

Errors
  None.
See also
  TextBackground, HighVideo, LowVideo, NormVideo

Example

      Program Example12;
      uses Crt;

      { Program to demonstrate the TextColor function. }

      begin
        WriteLn('This is written in the default color');
        TextColor(Red);
        WriteLn('This is written in Red');
        TextColor(White);
        WriteLn('This is written in White');
        TextColor(LightBlue);
        WriteLn('This is written in Light Blue');
      end.

##
20 TextMode

Declaration
  `procedure TextMode(Mode: Integer);`
Description
  TextMode sets the textmode of the screen (i.e. the number of lines and columns of the screen). The lower byte is use to set the VGA text mode.

  This procedure is only implemented on DOS.

Errors
  None.
See also
  Window

##
21 WhereX

Declaration
  `Function WhereX : Byte;`
Description

  WhereX returns the current X-coordinate of the cursor, relative to the current window. The origin is `(1,1)`, in the upper-left corner of the window.

Errors
  None.
See also
  GotoXY, WhereY, Window

Example

      Program Example7;
      uses Crt;

      { Program to demonstrate the WhereX and WhereY functions. }

      begin
        Writeln('Cursor postion: X=',WhereX,' Y=',WhereY);
      end.

##
22 WhereY

Declaration
  `Function WhereY : Byte;`
Description

  WhereY returns the current Y-coordinate of the cursor, relative to the current window. The origin is `(1,1)`, in the upper-left corner of the window.

Errors
  None.
See also
  GotoXY, WhereX, Window

Example

      Program Example7;
      uses Crt;

      { Program to demonstrate the WhereX and WhereY functions. }

      begin
        Writeln('Cursor postion: X=',WhereX,' Y=',WhereY);
      end.

##
23 Window

Declaration
  `Procedure Window (X1, Y1, X2, Y2: Byte);`
Description
  Window creates a window on the screen, to which output will be sent. `(X1,Y1)` are the coordinates of the upper left corner of the window, `(X2,Y2)` are the coordinates of the bottom right corner of the window. These coordinates are relative to the entire screen, with the top left corner equal to `(1,1)` Further coordinate operations, except for the next Window call, are relative to the window's top left corner.

Errors
  None.
See also
  GotoXY, WhereX, WhereY, ClrScr

Example

      Program Example5;
      uses Crt;

      { Program to demonstrate the Window function. }

      begin
        ClrScr;
        WriteLn('Creating a window from 30,10 to 50,20');
        Window(30,10,50,20);
        WriteLn('We are now writing in this small window we just created, we '+
                'can''t get outside it when writing long lines like this one');
        Write('Press any key to clear the window');
        ReadKey;
        ClrScr;
        Write('The window is cleared, press any key to restore to fullscreen');
        ReadKey;
      {Full Screen is 80x25}
        Window(1,1,80,25);
        Clrscr;
        Writeln('Back in Full Screen');
      end.