处理ReadKey的返回值


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.                         

这是Lazarus IDE Pascal。我想扩展从文档中复制的一个示例的功能,以便程序识别空格,而不仅仅是左/右/esc键。

我发现了一个程序,当你按下键时,它会把代码写出来。上面说32是空间。我在上面的switch语句中添加了#32 case。为什么按空格键时仍然看不到输出?

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不是扩展键,所以前面没有#0。我们不把#32放在#0后面,而是放在#0旁边。

最新更新