Handling return values of ReadKey

783 Views Asked by At
Program Example3;
uses Crt;

{ Program to demonstrate the ReadKey function. }

var
  ch : char;
begin
  writeln('Press Left/Right, Esc=Quit');
  repeat
    ch:=ReadKey;
      case ch of
      #0 : begin
             ch:=ReadKey; {Read ScanCode}
             case ch of
             #32: Writeln ('Space');
             #75 : WriteLn('Left');
             #77 : WriteLn('Right');
             end;
           end;
      #27 : WriteLn('ESC');
      end;
  until ch=#27 {Esc}
end.                         

This is Lazarus IDE Pascal. I want to extend functionality of an example copied from documentation so that the program recognizes space, not only left/right/esc keys.

I found a program that writes out the codes as you press the keys. It says 32 for space. I added the #32 case in switch statement above. Why do I still see no output when pressing space?

1

There are 1 best solutions below

0
On
case ch of
#0 : begin
       ch:=ReadKey; {Read ScanCode}
       case ch of
       #75 : WriteLn('Left');
       #77 : WriteLn('Right');
       end;
     end;
#27 : WriteLn('ESC');
#32 : WriteLn('Space'); {<- space case should go HERE}
end;

Space is not an extended key, so is not preceded by #0. We don't put #32 case into #0 case but next to it.