unit laklv;
{$IFDEF FPC}{$CALLING OLDFPCCALL}{$ASMMODE INTEL}{$ENDIF}
interface
type
_xReadkey_Doplnek = function(b:byte):word;
_xKeyPressed_Doplnek = function(b:byte):boolean;

const
xReadkey_Doplnek : _xReadkey_Doplnek = nil;
xKeyPressed_Doplnek : _xKeyPressed_Doplnek = nil;

{kody nekterych klaves }
  Tab       = #09;Esc       = #27;Enter     = #13;Space     = #32;
  BackSpace = #08;Del       = #83;Ins       = #82;Doprava   = #77;
  Doleva    = #75;Nahoru    = #72;Dolu      = #80;PageUp    = #73;
  PageDown  = #81;F1        = #59;F2        = #60;F3        = #61;
  F4        = #62;F5        = #63;F6        = #64;F7        = #65;
  F8        = #66;F9        = #67;F10       = #68;Home      = #71;
  Endkey    = #79;Sdoleva   = #52;Sdoprava  = #54;Snahoru   = #56;
  Sdolu     = #50;

{kody klaves, jak je vraci funkce xReadkey (diky Mircosofte) }
xF1=315;  xShiftF1=340;  xCtrlF1=350;    xAltX =301;
xAltF1=360;
xF2=316;  xShiftF2=341;  xCtrlF2=351;    xAltF2=361;
xF3=317;  xShiftF3=342;  xCtrlF3=352;    xAltF3=362;
xF4=318;  xShiftF4=343;  xCtrlF4=353;    xAltF4=363;
xF5=319;  xShiftF5=344;  xCtrlF5=354;    xAltF5=364;
xF6=320;  xShiftF6=345;  xCtrlF6=355;    xAltF6=365;
xF7=321;  xShiftF7=346;  xCtrlF7=356;    xAltF7=366;
xF8=322;  xShiftF8=347;  xCtrlF8=357;    xAltF8=367;
xF9=323;  xShiftF9=348;  xCtrlF9=358;    xAltF9=368;
xF10=324; xShiftF10=349; xCtrlF10=359;   xAltF10=369;
xF11=484; xShiftF11=251; xCtrlF11=393; xAltF11=395;
xF12=352; xShiftF12=80;  xCtrlF12=370;   xAltF12=396;
xLSipka=331; {xShiftLSipka=331;} xCtrlLSipka=371;  xAltLsipka=411;
xPSipka=333; {xShiftPSipka=333;} xCtrlPSipka=372;  xAltPSipka=413;
xHSipka=328; {xShiftHSipka=328;} xCtrlHSipka=397;  xAltHSipka=408;
xDSipka=336; {xShiftDSipka=336;} xCtrlDSipka=401;  xAltDSipka=416;
xIns=338;xDel=339;
xHome=327;xEndk=335;xPgUp=329;xPgDn=337;xCtrlPgUp=388;
xCtrlG=7;xEnter=13;xESC=27;xBackSpace=8;xTAB=9;xCtrlPgDn=374;


xLastKey:word=0;            {posledni zmacknuta klavesa co zachytilo xReadKey}
xKeyLock:boolean=false;     {v nekterych pripadech je potreba tuto vymozenost}
                            {docasne zakazat}

ang_pismena = ['a'..'z','A'..'Z'];
cislo = ['0'..'9'];
pridavne_znaky = ['.','_','!','-','~','%','&','#','\',':'];

pro_nazvy_souboru = ang_pismena + cislo + pridavne_znaky;

Procedure BufferOff;
Procedure PockejNaKlavesu;
Procedure PockejNaEnter;
function xReadKey:word;
function xKeyPressed:boolean;
function lKeyPressed:boolean;  {nahrada KeyPressed - nejak mi to funguje lepe}
function Je_CapsLock:boolean;
function Je_NumLock:boolean;
function Je_CTRL:boolean;
Function Je_Shift:boolean;

implementation

Procedure BufferOff;
begin
while xkeypressed do xReadkey;
end;

Procedure PockejNaKlavesu;
begin
BufferOff;
repeat until xkeypressed;
BufferOff;
end;

Procedure PockejNaEnter;
var o:word;
begin
BufferOff;
repeat
o:=Xreadkey;
until o=xEnter;
BufferOff;
end;

function lKeyPressed:boolean;assembler;
asm
mov ah,11h
int 16h
mov al,1
jnz @stisknuto
mov al,0
@stisknuto:
end;

function xKeyPressed:boolean;
{$IFNDEF FPC}
var p:pointer;
{$ENDIF}
begin
xLastkey:=0;

{$IFDEF FPC}
if xKeyPressed_Doplnek=nil
{$ELSE}
move(xKeyPressed_Doplnek,p,4);
if p=nil
{$ENDIF}
   then
   xKeyPressed:=LKeyPressed else xKeyPressed:=xKeyPressed_Doplnek(0);
end;

function xReadKey:word;
var w:word;
{$IFNDEF FPC}
p:pointer;
{$ENDIF}

begin
{$IFDEF FPC}
if xReadKey_Doplnek=nil
{$ELSE}
move(xReadKey_Doplnek,p,4);
if p=nil
{$ENDIF}
   then begin
   Asm
   mov AX,$0700 {cislo sluzby pro cteni klaves}
   int $21      {precteni klavesy - normalni Readkey}
   or AL,AL      {neco udelame s AL, ve kterem se vratil ten znak (tak, abychom ho nezmenili), tim se nastavi ZF}
   jnz @vratznak {takze kdyz to AL neni 0, skocime dal >> }
   int $21        {jestli jsme jeste tady, tak v AL byla 0, takze precteme dalsi znak (v AX porad jeste je $07)}
   mov AH,1       {do AH ulozime 1, aby bylo poznat, ze to byla specialni klavesa}
   jmp @konec     {a mame hotovo.}
   @vratznak:
   xor AH,AH     { >> vynulujeme AH, precteny znak zustane v AL a je vymalovano.}
   @konec:
   mov w,ax
   end;
   xLastKey:=w;
   xReadKey:=w;
   end
   else begin
   xLastKey:=xReadkey_Doplnek(0);
   xReadKey:=xLastKey;
   end;
End;{xreadkey}

function Je_CapsLock:boolean;
var w:word;
begin
{$IFDEF FPC}
asm
mov ax,fs:[$400+$17]
and ax,64
mov w,ax
end;
{$ELSE}
w:=MemW[Seg0040:$17] and 64;
{$ENDIF}
Je_CapsLock:=w<>0;
end;

function Je_NumLock:boolean;
var w:word;
begin
{$IFDEF FPC}
asm
mov ax,fs:[$400+$17]
and ax,8192
mov w,ax
end;
{$ELSE}
w:=MemW[Seg0040:$17] and 8192;
{$ENDIF}
Je_NumLock:=w<>0;
end;

function Je_CTRL:boolean;
var w:word;
begin
{$IFDEF FPC}
asm
mov ax,fs:[$400+$17]
and ax,4
mov w,ax
end;
{$ELSE}
w:=MemW[Seg0040:$17] and 4;
{$ENDIF}
Je_CTRL:=w<>0;
end;

Function Je_Shift:boolean;
var w:word;
begin
{$IFDEF FPC}
asm
mov ax,fs:[$400+$17]
and ax,3
mov w,ax
end;
{$ELSE}
w:=MemW[Seg0040:$17] and 3;
{$ENDIF}
Je_Shift:=w<>0;
end;


end.
