{@@@@@@@@@@@ copyright (C) 1984 by Neil J. Rubenking @@@@@@@@@@@@@@@@@@@@@@@@
The purchaser of these procedures and functions may include them in COMPILED
programs freely, but may not sell or give away the source text.

    This is a fancy driver for KEYBOARD.LIB.  It returns a character
    or descriptive phrase for ANY key pressed.  Note, though, that the
    <Alt> + <keypad number> combinations will be rendered as their
    equivalent <Ctrl> + <letter>s. (Alt-1 = Ctrl-A, etc.)


}


{$I regpack.typ}
{$I keyboard.lib}
type
  KeyType = string[12];
var
  KeyValue            : integer;
  ASCIIcode, ScanCode : byte;
  Result              : KeyType;

{@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@}
function Read_Keyboard: KeyType;
var
  TempRead : KeyType;
  {============================================================}
function SpecialKey(Code:byte):KeyType;
const
  Row0 : KeyType = '1234567890-=';
  Row1 : KeyType = 'QWERTYUIOP';
  Row2 : KeyType = 'ASDFGHJKL';
  Row3 : KeyType = 'ZXCVBNM';
var
  temp : KeyType;
begin
case code of
       14: temp := 'BackSpace';
       15: temp := 'Back Tab';
   16..25: temp := 'Alt-' + Row1[code-15];
   30..38: temp := 'Alt-' + Row2[code-29];
   44..50: temp := 'Alt-' + Row3[code-43];
 120..131: temp := 'Alt-' + Row0[code-119];
   59..67: temp := 'F' + chr(code - 10);
       68: temp := 'F10';
   84..92: temp := 'Shift F' + chr(code-35);
       93: temp := 'Shift F10';
  94..102: temp := 'Ctrl-F' + chr(code-45);
      103: temp := 'Ctrl-F10';
 104..112: temp := 'Alt-F' + chr(code-55);
      113: temp := 'Alt-F10';
       71: temp := 'Home';
       72: temp := 'Up';
       73: temp := 'PgUp';
       75: temp := 'Left';
       77: temp := 'Right';
       79: temp := 'End';
       80: temp := 'Down';
       81: temp := 'PgDn';
       82: temp := 'Ins';
       83: temp := 'Del';
      114: temp := 'Ctrl-PrtSc';
      115: temp := 'Ctrl-Left';
      116: temp := 'Ctrl-Right';
      117: temp := 'Ctrl-End';
      118: temp := 'Ctrl-PgDn';
      119: temp := 'Ctrl-Home';
      132: temp := 'Ctrl-PgUp';
 else
   temp := 'Ctrl-Break';
 end;  {case}
SpecialKey := temp;
end;
  {============================================================}
function SpecChr(code:byte):KeyType;
begin
  case code of
     0..26 : SpecChr := 'Ctrl-' + chr(code + 64);
        27 : SpecChr := 'Esc';
   28..255 : SpecChr := chr(code);
  end;
end;
  {============================================================}
begin
    KeyValue  := KeyBoard('W');
    ScanCode  := KeyValue shr 8;
    ASCIICode := (KeyValue shl 8) shr 8;
                    {The special keys that have no ASCII character generate }
                    {a zero in place of the code.  However, there are three }
                    {non-printable characters that DO have an ASCII code.   }
                    {Their scan codes are 14, 15, and 28.  We provide for   }
                    {them below.                                            }

    if (not (ScanCode in [14,15,28])) and (ASCIICode <> 0)  then
      TempRead := SpecChr(ASCIICode)
    else
      begin
        if ASCIICode <> 0 then
          begin
            case ScanCode of
              14: TempRead := 'BackSpace';
              15: TempRead := 'Tab';
              28: TempRead := 'Return';
            end;  {case}
          end  {second if}
      else
    TempRead := SpecialKey(ScanCode);
      end;  {the upper else}
   Read_Keyboard := TempRead;
end;

{@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@}
begin
  WriteLn('Press various keys and key combinations.');
  WriteLn('Press <ctrl><break> to stop.');
  repeat
    Result := Read_Keyboard;
    WriteLn(Result);
  until Result = 'Ctrl-Break';
end.