• Please review our updated Terms and Rules here

Readkey function for TP3

CP/M User

Veteran Member
Joined
May 2, 2003
Messages
2,986
Location
Back of Burke (Guday!), Australia
I ported this nice little BASIC game (nothing special) to TP3 a year or two ago, unfortunately I was persistant in using readkey which works great on an IBM, cause the function I'm using is calling BIOS function 16! :-o

So now I'm having trouble finding an TP3 solution to this cause my code which the offending code looks like this:

Code:
if keypressed then
 case readkey of
  #120, #88 : checkvalue(myvalue,ghostvalue);
  #109, #77 : begin
                     myvalue:=myvalue+1;
                     if myvalue=10 then myvalue:=0;
                   end;
 end;

My readkey function looks like this:

Code:
CONST PendingKey : BYTE = 0;
 
TYPE Registers = Record
                           CASE Integer of
                                1 : (AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags : Integer);
                                2 : (AL,AH,BL,BH,CL,CH,DL,DH : Byte);
                           END;
 
FUNCTION ReadKey : char;
VAR regs : registers;
BEGIN
 If Pendingkey<>0 then BEGIN
  Readkey:=chr(Pendingkey);
  Pendingkey:=0;
 END;
ELSE BEGIN
 regs.ah:=0;
 intr($16,regs);
 if regs.AL=0 then Pendingkey:=Regs.AH;
 Readkey:=CHR(regs.AL);
 end;
end;

I guess the trouble is I've been trying to use a machine equivalent (being a CPC), though it doesn't quite work the same as an IBM Interrupt 16 - even though it states it checks if a key has been pressed without waiting, I've good back to basics in writing a program which returns the keypress values - which maybe part of the problem. I guess with my code simply checks for a keypress it goes into checking which key has been pressed, the readkey function simply returns a value which I've feed into the Case statement. In the past things I've translated things like 'mychar:=readkey;' into 'read(kbd,mychar);' no probs, though throwing a readkey on it's own isn't usually done in TP3, and was just wondering if anyone here has made a generic TP3 readkey function without the machine BIOS Interrupts used in my readkey example above??
 
CP/M User wrote:

Code:
CONST PendingKey : BYTE = 0;
 
TYPE Registers = Record
                           CASE Integer of
                                1 : (AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags : Integer);
                                2 : (AL,AH,BL,BH,CL,CH,DL,DH : Byte);
                           END;
 
FUNCTION ReadKey : char;
VAR regs : registers;
BEGIN
 If Pendingkey<>0 then BEGIN
  Readkey:=chr(Pendingkey);
  Pendingkey:=0;
 END;
ELSE BEGIN
 regs.ah:=0;
 intr($16,regs);
 if regs.AL=0 then Pendingkey:=Regs.AH;
 Readkey:=CHR(regs.AL);
 end;
end;

Just looking at that code, surely it's not as easy as changing it to something like this?

I'll try! :-o UPDATE: Er no? read(kbd,ch); isn't allowed cause ch has to be of type CHAR. How ironic, my little game is using CHARacter keys - so I should be able to replace those in my CASE statement with CHARacters (perhaps this is the issue?).

Code:
CONST PendingKey : BYTE = 0;
 
FUNCTION ReadKey : char;
 
var ch : byte;
 
BEGIN
If Pendingkey<>0 then 
BEGIN
  Readkey:=chr(Pendingkey);
  Pendingkey:=0;
END;
 ELSE BEGIN
  read(kbd,ch);
  Readkey:=CHR(ch);
 end;
end;
 
A Solution

A Solution

Oh well another problem posted, another one solved by me! :-o

The good news is I've kept the solution very genetic, so theorically this game will work with anyone using TP3, bad news is I have to get it onto this machine (as opposed to my old pentium with typing it in - I'll reconnect the disk drive!).

I've never seen so many BEGIN..END; statements though! :-o Well it's been a while! ;-)

Code:
IF keypressed then BEGIN
 read(KBD,getchar);
 IF getchar IN[#120, #88, #109, #77] then BEGIN
 CASE getchar OF
  #120, #88 : checkvalue(myvalue,ghostvalue);
  #109, #77 : begin
                     myvalue:=myvalue+1;
                     if myvalue=10 then myvalue:=0;
                   end;
  end; {case statement}
 end; {getchar in}
end; {keypressed}

I should warn people that the Pascal code is incomplete, eventually I'll be posting the whole game on my website. I've merely posted the code here to demonstrate how TP3 might handle a non-interruptive, non-echoed input, I guess I have the Gulper game (written in TP3) to thank. Unlike the Gulper game my interpreted game handles and controls in the main code (both games share 'IF keypressed then' statement in the same main code!) before Gulper calls a check_keypress; procedure.

The principal here is if the user has hit a key, the program goes storing that key into getchar, and then checks if the key pressed matches one of the valid keys - the program works without that checking routine, though the program runs slightly slower due to the case statement constantly being used - guess it's a bit like the following BASIC:

Code:
10 a$=inkey$: if a$<>"q" and a$<>"Q" then 10
20 if a$="q" or a$="Q" then 30
30 print"bye":end
 
Back
Top