Board index » delphi » Help on keyboard events!

Help on keyboard events!

I was wondering if there was a way (well, actually, I know there's a way I just
don't know what it is...) to make a program run while it's looking for a key
pressed by the user.  Readkey doesn't work 'cause it pauses the program until
you press a key... I've been trying to work with events like GetKeyEvent and
stuff like that but I just don't understand how these work...  Please bare with
me 'cause I don't have very large knowldge of Pascal programming.

Thank you.

-------------
Philippe Laflamme

Live for today, go tomorrow. -Roger Waters
-------------

 

Re:Help on keyboard events!


Quote
In article <4uaked$...@Vir.com>, pfla...@vir.com (Philippe Laflamme) wrote:
>I was wondering if there was a way (well, actually, I know there's a way I
just
>don't know what it is...) to make a program run while it's looking for a key
>pressed by the user.  Readkey doesn't work 'cause it pauses the program until
>you press a key... I've been trying to work with events like GetKeyEvent and
>stuff like that but I just don't understand how these work...  Please bare
with
>me 'cause I don't have very large knowldge of Pascal programming.

G'day,

You could try something like this:

program endless;
uses
  crt;
const
  Esc=#27;
var
  ch : char;
  done : boolean;
begin {of main prog code}
done:=false;
repeat
  {where this procedure repeats as fast as possible, and
   does you're screen updating, error checking, etc}
  repeat TheInternalProcess until keypressed; {i.e. continually}
  {capture the user input}
  ch:=readkey;
  {process the user input}
  case ch of
    c : MoveLeft; {Move a spright}
    d : MoveRight; {Move a spright}
    ... {Any other commands in response to user's input}
    Esc : done:=true;
until done;
end. {of main prog code}

Hope this helps. I'm sure there are other ways, but this is the easiest for
me (e.g. Turbovision's GetIdle), and I use this construct in nearly all my
progs.

cheers, Alex.

Re:Help on keyboard events!


On 7 Aug 1996, Philippe Laflamme wrote:

Quote
> I was wondering if there was a way (well, actually, I know there's a way I just
> don't know what it is...) to make a program run while it's looking for a key
> pressed by the user.  Readkey doesn't work 'cause it pauses the program until
> you press a key... I've been trying to work with events like GetKeyEvent and
> stuff like that but I just don't understand how these work...  Please bare with
> me 'cause I don't have very large knowldge of Pascal programming.

When you work with Turbo Vision (from where you probably took the
GetKeyEvent routine), your program will never pause waiting for a
keystroke but will continue to run while keystrokes are passed to the
application's event handler. If you do traditional-style programming, you
may want to check KeyPressed which returns True if and only if a keypress
is available via ReadKey.

          -  Hans
--------------------------------------------------------------------------------
Hans Schleichert, Dipl.-Phys.
Institut fuer Medizinische Psychologie und Verhaltensneurobiologie
(Institute of Medical Psychology and Behavioural Neurobiology)
Eberhard-Karls-University
Gartenstrasse 29
D-72074 Tuebingen, Germany

Re:Help on keyboard events!


Quote
In article <4uaked$...@Vir.com> pfla...@vir.com (Philippe Laflamme) writes:
>I was wondering if there was a way (well, actually, I know there's a way I
>just
>don't know what it is...) to make a program run while it's looking for a key
>pressed by the user.  Readkey doesn't work 'cause it pauses the program until
>you press a key... I've been trying to work with events like GetKeyEvent and
>stuff like that but I just don't understand how these work...  Please bare
>with
>me 'cause I don't have very large knowldge of Pascal programming.
>Thank you.

How about this:

program hiya;

{$X+}

uses
  crt;

const
  mesg : string[11] = 'Hi there!!!';

var
  b : byte;

procedure writemessage;

var
  t : char;
  wx, wy : byte;

begin
  {rotate the message}
  t := mesg[1];
  move (mesg[2],mesg[1],10);
  mesg[11] := t;
  wx := wherex; wy := wherey;
  gotoxy (50,1);
  write  (mesg);
  gotoxy (wx,xy);
end;

var
  ch : char;

begin
  b := 0;
  writeln ('Please press 1, 2, or 3. . . .');
  repeat
    writemessage;
    if keypressed then
      begin
        ch := readkey;    
        if ch=#0 then
          readkey;  {pull extra key from buffer}
        if ch in ['1'..'3'] then
          b := ord(ch)-48;
      end;
  until b>0;
  writeln ('You selected ',b,'.');
end.  

See, this should be pretty simple.  It's waiting for input, but it checks
keypressed() to see if there's any input it needs to handle.  Otherwise, it
just keeps on doing what you want to be doing (in this case, just scrolling
a small message).

This isn't tested, but I think it should to what you're looking for.

Quote
>-------------
>Philippe Laflamme
>Live for today, go tomorrow. -Roger Waters
>-------------

--
Scott Earnest          | We now return you to our regularly scheduled |
siny...@{*word*104}space.org | chaos and mayhem. . . .                      |

Re:Help on keyboard events!


Quote
> I was wondering if there was a way (well, actually, I know there's a way I just
> don't know what it is...) to make a program run while it's looking for a key
> pressed by the user.  Readkey doesn't work 'cause it pauses the program until
> you press a key... I've been trying to work with events like GetKeyEvent and
> stuff like that but I just don't understand how these work...  Please bare with
> me 'cause I don't have very large knowldge of Pascal programming.

In you loop, instead of

d:=readkey;

put

if keypressed then d:=readkey;

and if a key hasn't been pressed yet, it will skip over..

Re:Help on keyboard events!


Borland conveniently has a keypressed function...

ie..

Repeat until keypressed;

will sit and do nothing until a key is pressed...

Hope this helps...

-Travis

** Travis J. Galoppo
** CS Student - Niagara University
** Programmer - NASA *EOS* (OTD/LIS missions)  (http://wwwghcc.msfc.nasa.gov)
** (http://www.niagara.edu/~s943022)

Re:Help on keyboard events!


Quote
Philippe Laflamme wrote:

> I was wondering if there was a way (well, actually, I know there's a way I just
> don't know what it is...) to make a program run while it's looking for a key
> pressed by the user.  Readkey doesn't work 'cause it pauses the program until

> Thank you.

> -------------
> Philippe Laflamme

USES Crt;

BEGIN
 IF KeyPressed THEN DoSomeThing.
  ELSE Continue with something else
END;

Ben.

Other Threads