Unit MouseKey;

InterFace

Var Key : Char;
    MouseX,MouseY,Lmx,Lmy : Integer;
    Lbutton,Rbutton,BothButtons,ButtonPressed : Boolean;
    Rshift,Lshift,Ctrl,Alt,ScrollLock,NumLock,CapsLock,Insert: Boolean;


Function MousePresent:Boolean;
Procedure MouseStat;
Function Mousein(X1,Y1,X2,Y2: Integer): Boolean;
Procedure SetMouseLimits(X1,Y1,X2,Y2: Integer);
Procedure PutMouse(X1,Y1: Integer);
Function Keypressed: Boolean;
Function ReadKey: Char;
Procedure ClearKBD;
Procedure GetSFlags;
Procedure WaitKey;
Procedure WaitButton;
Procedure WaitKeyOrButton;
Function GetKey:Char;
Function FileExist(Fil:String):Boolean;
Function FileSaved(Fil:String):Boolean;

Implementation

Function MousePresent; Assembler;
Asm
Xor Ax,Ax
Int 33h
Cmp Ax,-1
Je @Skip
Xor Al,Al
@skip:
End;


Procedure MouseStat;
 Var Buttons: Integer;
 Begin
  Lmx := MouseX;
  Lmy := MouseY;
  Asm
   Mov AX, 3
   Int 33h
   Mov MouseX, CX
   Mov MouseY, DX
   Mov Buttons, BX
  End;
  Lbutton := False;
  Rbutton := False;
  BothButtons := False;
  ButtonPressed := False;
  MouseX := MouseX Div 2;
  If Buttons <> 0 then ButtonPressed := True;
  If (Buttons AND 1) = 1  then Lbutton := True;
  If (Buttons AND 2) Div 2= 1 then Rbutton := True;
  If (Buttons AND 3) Div 3= 1 then BothButtons := True;
 End;

Function Mousein(X1,Y1,X2,Y2: Integer):Boolean;
 Begin
  Mousein := False;
  If (MouseX > X1-1) AND (MouseX < X2+1) AND (MouseY > Y1-1) AND (MouseY < Y2+1) then
  Mousein := True;
 End;

Procedure SetMouseLimits(X1,Y1,X2,Y2: Integer);
 Begin
  X1 := X1 * 2;
  X2 := X2 * 2;
  Asm
   MOV AX, 7
   MOV CX, X1
   MOV DX, X2
   INT 33h
   MOV AX,8
   MOV CX, Y1
   MOV DX, Y2
   Int 33h
  End;
 End;

Procedure PutMouse(X1,Y1: Integer);
 Begin
  X1 := X1 * 2;
  Asm
   Mov AX, 4
   Mov CX, X1
   Mov DX, Y1
   Int 33h
   Mov MouseX, Cx
   Mov MouseY, Dx
  End;
  MouseX := Trunc(MouseX /2);
  Lmx := MouseX;
  Lmy := MouseY;
 End;

Function Keypressed: Boolean; Assembler;
 Asm
  Mov Ah, 01h
  Int 16h
  Jnz @Ok
  Mov Ax, 0
  Jmp @Fini
  @Ok:
  Mov Ax, 1
  @Fini:
 End;

Function ReadKey: Char; Assembler;
 Asm
  Mov Ah, 00h
  Int 16h
 End;

Procedure ClearKBD;
Begin
While Keypressed do ReadKey;
End;

Procedure GetSFlags;
Var Num:Byte;
Begin
Asm
Mov Ah, 02h
Int 16h
Mov Num, Al
End;
Rshift := False;
LShift := False;
Ctrl := False;
Alt := False;
ScrollLock := False;
NumLock := False;
CapsLock := False;
Insert := False;
If (Num And 1) / 1 = 1 then Rshift := True;
If (Num And 2) / 2 = 1 then Lshift := True;
If (Num And 4) / 4 = 1 then Ctrl := True;
If (Num And 8) / 8 = 1 then Alt := True;
If (Num And 16) / 16 = 1 then ScrollLock := True;
If (Num And 32) / 32 = 1 then NumLock := True;
If (Num And 64) / 64 = 1 then CapsLock := True;
If (Num And 128) / 128 = 1 then Insert := True;
End;

Procedure WaitKey;
Begin
Repeat Until Keypressed;
Readkey;
End;

Procedure WaitButton;
Begin
Repeat
MouseStat;
Until ButtonPressed;
End;

Procedure WaitKeyOrButton;
Begin
Repeat
MouseStat;
Until (ButtonPressed) Or (Keypressed);
If Keypressed then Readkey;
End;

Function GetKey:Char;
Begin
GetKey := Chr(0);
If Keypressed then GetKey := Readkey;
End;

Function FileExist(Fil:String):Boolean;
Var Fich:File;
Begin
FileExist := False;
{$I-}
Assign(Fich,Fil);
Reset(Fich,1);
If (Ioresult = 0) And (Length(Fil) > 0) then
Begin
FileExist := True;
Close(Fich);
End;
{$I+}
End;

Function FileSaved(Fil:String):Boolean;
Var Fich:File;
Begin
FileSaved := False;
{$I-}
Assign(Fich,Fil);
Rewrite(Fich,1);
If (Ioresult = 0) And (Length(Fil) > 0) then
Begin
FileSaved := True;
Close(Fich);
End;
{I+}
End;

Begin
End.
