Recent

Author Topic: [Please Help] Readkey and keypressed not responding  (Read 5231 times)

ShiverMeSideways

  • Newbie
  • Posts: 1
[Please Help] Readkey and keypressed not responding
« on: April 04, 2014, 08:32:52 am »
Hello!

I've recently gotten back into Pascal and I'm trying to remember how everything worked.

I'm trying to make some test programs in order to eventually make a game and I first started with a small text-mode tile engine. However, that is not the problem. The problem is with the Readkey and Keypressed functions.

The code below simply does not respond, sitting instead in an infinite loop. I used Write to display what key I did press and it does not display anything on the screen over the tiles at any point, nor does the program Delay for 1000 miliseconds. This leads me to believe that the Keypressed function may be the culprit.

I am using Free Pascal in Windows XP SP2.

And now the code:
Code: [Select]
Program TestProgram;
uses crt, mouse;
var mousebuttons: byte;
var button, button2: word;
var quit: boolean;
var i, j, k, l, m, n: integer;
var viewportx, viewporty: integer;
var ch: char;
var tile: array[1..160, 1..80] of record
  x, y, tiletype, building: integer;
end;
const mbnames: array[1..3] of String=('left button', 'right button', 'middle button');
const tile_grass: integer = 1;
const tile_water: integer = 2;
procedure generatemap;
begin
  randomize;
  for i:=1 to 160 do
  begin
    for j:=1 to 80 do
    begin
      tile[i, j].x:=i;
      tile[i, j].y:=j;
      tile[i, j].tiletype:=random(3);
      if tile[i, j].tiletype<1 then tile[i, j].tiletype:=1;
      tile[i, j].building:=0;
    end;
  end;
end;
procedure drawtiles;
begin
  for i:=1 to 160 do
  begin
    for j:=1 to 80 do
    begin
      if ((i>=viewportx) and (i<=viewportx+79) and (j>=viewporty) and (j<=viewporty+20)) then
      begin
        gotoxy(1+i-viewportx, 1+j-viewporty);
        if (tile[i, j].tiletype = tile_grass) then textcolor(2);
        if (tile[i, j].tiletype = tile_water) then textcolor(3);
        write('Û');
      end;
    end;
  end;
end;
begin
  mousebuttons:=detectmouse;
  if mousebuttons = 0 then
    writeln('No mouse detected. Please connect a mouse.')
  else
   begin
     initmouse;
     showmouse;
   end;
  clrscr;
  cursoroff;
  clrscr;
  generatemap;
  viewportx := 1;
  viewporty := 1;
  quit := false;
  while (quit = false) do
  begin
    drawtiles;
    if (keypressed) then
    begin
      ch := readkey;
      if ((ch = #81) or (ch = #113)) then quit := true;
      gotoxy(1,1);
      write(ch);
      delay(1000);
    end
    else
      ch := ' ';
  end;
  cursoron;
end.

Could someone please tell me what I'm doing wrong? Thank you very much in advance!

Bart

  • Hero Member
  • *****
  • Posts: 3541
    • Bart en Mariska's Webstek
Re: [Please Help] Readkey and keypressed not responding
« Reply #1 on: April 04, 2014, 11:31:03 am »
It is the mouse unit that interferes with crt unit.
If you leave out the InitMouse and ShowMouse then keypressed will behave as expected.

Bart

Tomas Hajny

  • Moderator
  • New Member
  • *****
  • Posts: 36
Re: [Please Help] Readkey and keypressed not responding
« Reply #2 on: April 04, 2014, 12:57:14 pm »
BTW, note that you can use unit Keyboard which should work well with unit Mouse and which provides functions with a similar functionality as Readkey and Keypressed (namely GetKeyEvent and PollKeyEvent).

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7508
Re: [Please Help] Readkey and keypressed not responding
« Reply #3 on: April 04, 2014, 02:18:28 pm »
He also uses gotoxy, so would need unit video too, and that requires rewriting how you output.

Tomas Hajny

  • Moderator
  • New Member
  • *****
  • Posts: 36
Re: [Please Help] Readkey and keypressed not responding
« Reply #4 on: April 04, 2014, 03:20:38 pm »
Well, yes, at best. Depending on the chosen platforms and other functionality needed, it might be enough to continue using Crt for video while just changing the keyboard handling related part. I tested with the provided sample under Win32 and after adding InitKeyboard at the beginning and using PollKeyEvent and GetKeyEvent like this (in the main loop):

    KE := PollKeyEvent;
    if (KE <> 0) then
    begin
      KE := GetKeyEvent;
      if ((char (KER.Keycode) = #81) or (char (KER.Keycode) = #113)) then quit := true;

(obviously following the previous declaration of KE and KER:

var
 KE: TKeyEvent;
 KER: TKeyRecord absolute KE;

)

Admittedly, the same wouldn't work under Unix platforms due to known incompatibility between units Video and Crt (and someone who had promised to have a look at rewriting unit Crt but never finished it :-( - oops, that sounds like me :-((( ).