Board index » delphi » Help with keyboard

Help with keyboard

Is there any way to check in pascal whether a key is
currently pressed down, or if a key had just been released?

The reason that I am asking this is that I'm programming a
game, and I want the player to keep moving as long as he is
holding down the movement key, and stop upon releasing it.

Thanks in advance! :)

* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful

 

Re:Help with keyboard


"Danra" <[email protected]> schreef in bericht
news:[email protected]
Quote
> Is there any way to check in pascal whether a key is
> currently pressed down, or if a key had just been released?

> The reason that I am asking this is that I'm programming a
> game, and I want the player to keep moving as long as he is
> holding down the movement key, and stop upon releasing it.

> Thanks in advance! :)

> * Sent from AltaVista http://www.altavista.com Where you can also

find related Web Pages, Images, Audios, Videos, News, and Shopping.
Smart is Beautiful

Danra,
look up the help files for:
keypressed and readkey
they should do the job.
CU,
Marcel
--
Please remove the *.NoSpam* entry from my eMail address when replying.
It's meant to keep Spam and Advertising out, not friends.
Please test my new web pages
http://users.pandora.be/marcel.van.der.mierde/
still under heavy construction

Re:Help with keyboard


Danra wrote in <[email protected]>...

Quote
>Is there any way to check in pascal whether a key is
>currently pressed down, or if a key had just been released?

Only by hooking to the hardware interrupt vector of the keyboard.  The
keyboard sends information as key down and key up messages for every
key.

--
Raimo Suonio, Helsinki, Finland
http://www.dlc.fi/%7Eexp-1/suonio/
Oikeinkirjoitusohjeita news- ja web-kirjoittajille:
http://www.dlc.fi/%7Eexp-1/oikeinkirjoitus.html

Re:Help with keyboard


Hi,

Marcel  Vander Mierde wrote:

Quote
>> Is there any way to check in pascal whether a key is
>> currently pressed down, or if a key had just been released?

<snip>

Quote
> look up the help files for:
> keypressed and readkey
> they should do the job.

Wrong answer, these functions will not provide the data. Raimo gave
the correct answer already: Only by monitoring the keyboard interrupt
and reading the information from the keyboard controller can you find
out when a key is pressed and when it is released.

Even then it is not guranteed that you can detect reliably which keys
are down when more than one key is pressed. I believe that detailed
descriptions of everything have been posted in this group in the past
and should easily be found via DejaNews <http://www.deja.com/home_ps.shtml>.

The usual link/source collections should also have information.

 - Sebastian

Re:Help with keyboard


Thank you all, especially for the last two replies (I
already knew about readkey and keypressed... :) ).

Umm... Just what link/source collections are you talking
about, Marcel, just in general or anything specific? I'd be
happy if you could list a few for me...

* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful

Re:Help with keyboard


Quote
> Thank you all, especially for the last two replies (I
> already knew about readkey and keypressed... :) ).

> Umm... Just what link/source collections are you talking
> about, Marcel, just in general or anything specific? I'd be
> happy if you could list a few for me...

You can access port[96] and see what you get.

(When the program is running, press some key, for example alphabets and
arrow keys.
To quit this program, press escape.
AFTER quitting the program test your CTRL, ALT and Shift keys a few times)

{not tested. run at your own risk}
var v,lastv:integer;
begin
  lastv:=-1;
repeat
  v:=port[96];
  if (v<>0)  and (v<>lastv) then write(v,' ');
  lastv:=v;
until v=1;
end.

You will get some numbers, some smaller than 128 and some larger.
(if you press a key, the number is smaller than 128. If you release a key,
it is above 128)

The numbers are totally unrelated to ASCII:
1 means <ESC>, 2 means <1>, 3 means <2> .....

If you want files, Search
inter57a.zip
on the web and download the files inter57?.zip

Re:Help with keyboard


Hi,

Quote
Danra wrote:
> Umm... Just what link/source collections are you talking
> about

Ah, I was just too lazy to name a few examples. As always, Pedt's
Mini-FAQ is a good place to start:

| * Jeroen Prins Pascal Programmers Pages
|     http://www.cs.vu.nl/~jprins/tp.html

| * Turbo Pascal Programmers Page
|     http://www.programmers.org/languages/pascal/resources/

| * Franz Glaser's TP Links
|    http://www.geocities.com/SiliconValley/2926/tp.html

| * SWAG.
|   See http://www.gdsoft.com/swag/swag.html

| * Coders Knowledge Base
|    http://netalive.org/ckb/

I bet (without checking) that SWAG has some keyboard monitoring source
code.

 - Sebastian

Re:Help with keyboard


Quote
Danra wrote in message <[email protected]>...
>Is there any way to check in pascal whether a key is
>currently pressed down, or if a key had just been released?

>The reason that I am asking this is that I'm programming a
>game, and I want the player to keep moving as long as he is
>holding down the movement key, and stop upon releasing it.

>Thanks in advance! :)

>* Sent from AltaVista http://www.altavista.com Where you can also find

related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is
Beautiful

You need to access the keyboard on interrupt level.
That means you need to write your own keyboard handler.

Download the game library at my web-site.
The file hdt356.tpu has a keyboard handler.
Various other game libraries also include these. Cause dos-keyboard handler
sucks for games.
Chaou
Heinrich

http://www.crosswinds.net/~jhdutoit/
Have a great TP tutorial!

Re:Help with keyboard


In article <[email protected]>, "Heinrich" says...

Quote

>Danra wrote in message <[email protected]>...
>>Is there any way to check in pascal whether a key is
>>currently pressed down, or if a key had just been released?

>>The reason that I am asking this is that I'm programming a
>>game, and I want the player to keep moving as long as he is
>>holding down the movement key, and stop upon releasing it.

>You need to access the keyboard on interrupt level.
>That means you need to write your own keyboard handler.

Not necessarily.  It depends on how flexible he is about what
the "movement" keys are.

If he is willing to use Shift, Control, and Alt, and he doesn't
need to deal with more than two keys pressed at the same time,
he can tell when these keys are pressed or not pressed simply
by reading the status bytes in the BIOS data area.

-------------------begin example---------------------
uses crt;

var
kb_flag  : byte absolute $40:$17;
kb_flag_1: byte absolute $40:$18;
kb_flag_2: byte absolute $40:$96;

Function LeftShiftPressed:Boolean;
Begin LeftShiftPressed:=kb_flag and 2 <> 0; End;

Function RightShiftPressed:Boolean;
Begin RightShiftPressed:=kb_flag and 1 <> 0; End;

Function LeftCtrlPressed:Boolean;
Begin LeftCtrlPressed:=kb_flag_1 and 1 <>0; End;

Function RightCtrlPressed:Boolean;
Begin RightCtrlPressed:=kb_flag_2 and 4 <> 0; End;

Function LeftAltPressed:Boolean;
Begin LeftAltPressed:=kb_flag_1 and 2 <> 0; End;

Function RightAltPressed:Boolean;
Begin RightAltPressed:=kb_flag_2 and 8 <> 0; End;

begin

clrscr;

repeat

if LeftShiftPressed then begin
  gotoxy(1,1); write('LeftShiftPressed')
  end else begin
  gotoxy(1,1); write('                 ');
  end;

if RightShiftPressed then begin
  gotoxy(1,2); write('RightShiftPressed')
  end else begin
  gotoxy(1,2); write('                 ');
  end;

if LeftCtrlPressed then begin
  gotoxy(1,3); write('LeftCtrlPressed')
  end else begin
  gotoxy(1,3); write('               ');
  end;

if RightCtrlPressed then begin
  gotoxy(1,4); write('RightCtrlPressed')
  end else begin
  gotoxy(1,4); write('                ');
  end;

if LeftAltPressed then begin
  gotoxy(1,5); write('LeftAltPressed')
  end else begin
  gotoxy(1,5); write('              ');
  end;

if RightAltPressed then begin
  gotoxy(1,6); write('RightAltPressed')
  end else begin
  gotoxy(1,6); write('               ');
  end;

until keypressed;

end.

---------------------end example------------------------------

Fifty dollars please.

Re:Help with keyboard


Thank you all for your help!
(Doc, your 50 bucks are on their way... :) Do you always
charge so much, btw? )

* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful

Other Threads