{ Keyboard unit }

unit Keyb;

interface

const
  { Chars and numbers }
  TextChars = ['0'..'9', 'A'..'Z', ' ', '-', '.'];

  { Keys }
  kbUpArrow    = $FF00 or Ord('H'); { Up arrow }
  kbDownArrow  = $FF00 or Ord('P'); { Down arrow }
  kbLeftArrow  = $FF00 or Ord('K'); { Left arrow }
  kbRightArrow = $FF00 or Ord('M'); { Right arrow }

  kbEsc        = $1B; { Esc key }
  kbEnter      = $0D; { Enter key }
  kbBackSpace  = $08; { Back Space key }

  { Read pressed key /Return UpCased char/}
  function GetUpCasedKey: word;
  { Read pressed key /Return NOT UpCased char/}
  function GetKey: word;

  procedure Input(X, Y, TextLen: byte; var text: string);

implementation

uses Crt;

function GetUpCasedKey;
var
  Key: word;

begin
  If Keypressed then
  begin
    Key := Ord(UpCase(ReadKey));
    If Key = 0 then Key := $FF00 or Ord(UpCase(ReadKey));
  end else
   Key := 0;

  GetUpCasedKey := Key;
end;

function GetKey;
var
  Key: word;

begin
  If Keypressed then
  begin
    Key := Ord(ReadKey);
    If Key = 0 then Key := $FF00 or Ord(ReadKey);
  end else
   Key := 0;

  GetKey := Key;
end;


procedure Input;
var
  Key: word;
  Len: byte;
  AChar: char;
  EndOfInput: boolean;

begin
  EndOfInput := false;

  repeat
    { Draw text }
    GotoXY(x, y);
    Write(Text,'Û');

    { Read key }
    repeat
      Key := GetKey;
    until Key <> 0;
    AChar := Chr(Lo(Key));

    Len := Length(Text);
    case Key of
      kbEnter     : begin
                      EndOfInput := true;
                    end;
      kbBackSpace : If Len > 0 then
                    begin
                      Delete(Text, Len, 1);
                      GotoXY(x + Len, y);
                      Write(' ');
                    end;
    end;

    If Len < TextLen then
    begin
      If UpCase(AChar) in TextChars then Text := Text + AChar;
    end;
  until EndOfInput;
end;

end.