Monday, May 4, 2020

INKEY.P

[Table of Contents]
INKEY.ATR Disk Image

INKEY.P is an example of reading user's key presses in a non-blocking fashion. This example handles (A)ttack, and (Q)uit. The keyboard code for any other pressed character will be displayed.

INKEY.P uses a PEEKPOKE.I include file to be able to POKE to and PEEK from memory locations.

INKEY.P peeks at Atari's internal hardware value for the last key pressed, (memory location 764). See page 76 of Mapping The Atari Revised Edition for more information on this memory location. Memory location 764 returns 255 if no key has been pressed or a raw keyboard matrix code if a key has been pressed. To decipher the raw keyboard matrix code, see page 50 of the Atari 400/800 Operating System User's Manual (you will need to convert from hex to decimal as well) or you can just experiment to figure out which codes go to which key press.  

This example program uses a CASE statement. Note that there is a feature (restriction) in ISO Pascal (and Kyan Pascal as a result) that you must handle every possible case in a CASE statement or you will get a runtime error for any non-handled cases. Here are a few things you can try to avoid this restriction:

  • Remove the CASE statement and use IF/THEN statements
  • Use an IF/THEN statement before the CASE statement to only allow handles cases to pass

Source Code


(* INKEY.P *)
(* KYAN PASCAL 2.X *)
(* ATARI 8-BIT *)
(* Bill Lange *)

program INKEY(INPUT,OUTPUT);
var
   CONTINUE : BOOLEAN;
   K : INTEGER;

#i PEEKPOKE.I

procedure Attack;
begin
   writeln('Attack');
end;

procedure Quit;
begin
   writeln('Quit');
   Continue := False;
end;

procedure GetCommand;
begin  
   K := PEEK(764);
   if K <> 255 then
   begin
      poke(764,255);
      write('(',k,') ');
      if (k=63) or (k=47) then
      begin
      case K of
        63 : Attack; (* a *)
        47 : Quit; (* q *)
      end;
      end;
   end;
end;

(* Main Program *)
begin

   CONTINUE := TRUE;

   while CONTINUE = TRUE do
   begin
      GetCommand;
      (* Do other stuff *)
   end;   
end.



No comments:

Post a Comment