{$C FIXED PRELOAD PERMANENT}

library DVKBLIB;

uses
  WinTypes, WinProcs, Win31;

const
  MAXBUF = 30;

var
  TheHook: HHook;
  SysModalOn: Bool;

  bufChar: array [0..MAXBUF-1] of word;
  pins, pret: integer;

{ *********************************************************************** }

function KeyboardProc(Code: Integer;
                           wParam: word; lParam: longint): Longint;
                           export;
begin
  KeyboardProc := 0;
  case Code of

    HC_ACTION: begin
      if SysModalOn then exit;
      if (lparam shr 31) and 1 = 0 then
          begin
             bufChar [pins] := wparam;
             pins := (pins + 1) mod MAXBUF;
          end;
      end;

    HC_SYSMODALON: begin
      SysModalOn := True;
      CallNextHookEx(TheHook, Code, wParam, lParam);
    end;

    HC_SYSMODALOFF: begin
      SysModalOn := False;
      CallNextHookEx(TheHook, Code, wParam, lParam);
    end;

  else
     if code < 0 then
        keyboardProc := CallNextHookEx(TheHook, Code, wParam, lParam);
  end;

end;

{ *********************************************************************** }

function StartRecording: Boolean; export;
begin
  StartRecording := false;

  SysModalOn := False;
  pins := 0;
  pret := 0;

  TheHook := SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hInstance, 0);

  if TheHook <> 0 then
    StartRecording := true;
end;

{ *********************************************************************** }

function StopRecording: boolean; export;
begin
  if UnHookWindowsHookEx(TheHook) = False then begin
    StopRecording := false;            { unable to unhook }
    exit;
  end;

  TheHook := 0;
  StopRecording := true;
end;

{ *********************************************************************** }

function getRecordedChar: word; export;
begin
    if pins = pret then
        getRecordedChar := 0
    else
        begin
            getRecordedChar := bufChar [pret];
            pret := (pret + 1) mod MAXBUF;
        end;
end;

exports
  KeyboardProc        index 1 name 'KEYBOARDPROC' resident,
  StartRecording      index 2 name 'STARTRECORDING' resident,
  StopRecording       index 3 name 'STOPRECORDING' resident,
  getRecordedChar     index 4 name 'GETRECORDEDCHAR' resident;

begin
end.
