unit rezklav;
uses go32;
{$MODE FPC}
{$Q-}
{$R-}
{$S-}
{$D-}
{$DEFINE OPATRNOST}   {Zda se, ze je mozne ji vypnout...}

interface
procedure ZapniObsluhuKlavesnice;
procedure VypniObsluhuKlavesnice;
kl_kod:word;

implementation

const kbdint = $9;

var oldint9_handler:tseginfo;
    newint9_handler:tseginfo;

    clickproc:pointer;
    backupDS:Word; external name '___v2prt0_ds_alias';

procedure int9_handler; assembler;  { VSIMNETE SI, ZE TU NENI A NESMI BYT UVEDEN MODIFIKATOR INTERRUPT }
asm
cli
{$IFDEF OPATRNOST}
push ds
push es
push fs
push gs
pusha
{$ENDIF}
   mov ax,cs:[backupDS]
   mov ds,ax
   mov es,ax
   mov ax,dosmemselector
   mov fs,ax
 call clickproc
{$IFDEF OPATRNOST}
popa
pop gs
pop fs
pop es
pop ds
{$ENDIF}
 jmp cs:[oldint9_handler]
sti
end;
procedure int9_dummy; begin end;

procedure clicker;
begin
sound(500);delay(10);nosound;
end;
procedure clicker_dummy; begin end;

procedure ZapniObsluhuKlavesnice;
begin
clickproc:=@clicker;
lock_data(clickproc, sizeof(clickproc));
lock_data(kl_kod, sizeof(kl_kod));
lock_data(dosmemselector, sizeof(dosmemselector));

lock_code(@clicker,longint(@clicker_dummy) - longint(@clicker));
lock_code(@int9_handler,longint(@int9_dummy)-longint(@int9_handler));
newint9_handler.offset:=@int9_handler;
newint9_handler.segment:=get_cs;
get_pm_interrupt(kbdint, oldint9_handler);
set_pm_interrupt(kbdint, newint9_handler);
end;

procedure VypniObsluhuKlavesnice;
begin
set_pm_interrupt(kbdint, oldint9_handler);
unlock_data(dosmemselector, sizeof(dosmemselector));
unlock_data(kl_kod, sizeof(kl_kod));
unlock_data(clickproc, sizeof(clickproc));

unlock_code(@clicker,longint(@clicker_dummy)-longint(@clicker));
unlock_code(@int9_handler,longint(@int9_dummy)-longint(@int9_handler));
end;

end;

