{KEYEXAM.PAS / EXAMPLE FOR KEYBOARD INTERRUPT ROUTINES}
{WRITING BY THE KING IN 10/19/95                      }
Uses Dos,Crt;
Const
	RIGHTARROW =  75;        {Right Coursr Key}
    LEFTARROW  =  77;		 {Left Coursr Key}
    UPARROW    =  72;        {Up Coursr Key}
    DOWNARROW  =  80;        {Down Coursr Key}

    {Ranges !}
    MINRANGEX = 1;
    MINRANGEY = 2;
    MAXRANGEX = 79;
    MAXRANGEY = 24;
Var
	OldInt9 : PRocedure; {The Old Interrupt 9}
    Keys : Array[1..128] Of Boolean;  {The Keys status}

{--------------------------------------------}
{New Interrupt 9 for handle with the keyboard}
{--------------------------------------------}

Procedure NewInt9;interrupt;
Begin
    Keys[Port[$60] Mod 128] := (Port[$60] < 128) ;
    {Checking if Port[$60] < 128 , If He Is , Keys[Port[$60] Mod 128]
    Is True Else False}
	Asm
    	PushF                       {Pushing Flags}
	End;
    OldInt9;                        {Calling the old interrupt}
    Mem[$0040:$001A] := Mem[$0040:$001C];
	{Puting The Tail And The Head , for clear the buffer}
End;
{-------------------------------------------}
{         Init The new interrupt            }
{-------------------------------------------}

Procedure Init;
Begin
	GetIntVec($9,@OldInt9);
	SetIntVec($9,@NewInt9);
End;

{--------------------------------------}
{      Restore The Old interrupt       }
{--------------------------------------}
Procedure Restore;
Begin
	SetIntVec($9,@OldInt9);
End;

{-------------------------------------------}
{      Moving A Man On The Screen           }
{-------------------------------------------}

Procedure MoveMan;
Var
	X,Y:Byte;          {The X,Y Of the man}
Begin
	Clrscr;                  {clearing the screen}
    TextColor(15);           {textcolor = White}
    TextBackGround(1);       {textbackground = Blue}
    GotoXy(1,1);
    Writeln(' THE KING KEYBOARD INTERRUPT ROUTINES , PRESS "ESC" TO EXIT , USE THE COURSRS   ');
    TextBackGround(0);       {textbackground = black}
	X:=40;
    Y:=17;
    Repeat
    	GotoXy(X,Y);                {Paint the man}
    	Write(Chr(1));

        {Keyboard Checking}
        If Keys[LEFTARROW] Then
        	Inc(X);

        If Keys[RIGHTARROW] Then
        	Dec(X);

        If Keys[UPARROW] Then
        	Dec(Y);

        If Keys[DOWNARROW] Then
        	Inc(Y);

		{Checking if the man cross the ranges}
        If X < MINRANGEX Then
			Inc(X);

        If Y < MINRANGEY Then
			Inc(Y);

        If X > MAXRANGEX Then
			Dec(X);

        If Y > MAXRANGEY Then
			Dec(Y);

        Delay(50);
        GotoXy(WhereX-1,WhereY);
        Write(' ');

    Until(Keys[1]);         {Until ESC Key}
    clrscr;
End;
Begin
	Init;              {INIT KEYBOARD}
    MoveMan;           {MOVING THE MAN}
    Restore;           {RESTORE OLD KEYBOARD}
End.
