[comp.sys.amiga] Repeating multiple keys

peter@dalcsug.UUCP (Peter Philip) (11/30/87)

Can anyone tell me where I could find an example of detecting which keys
are being held down?  I need this for a game I would like to write, so it has
to be quite fast and able to detect more than one key (like 'x' for turn
right and '/' for thrust) simultaneously.  The only reference for doing
 this that I could find was in the RKM under keyboard.device - which also 
says not to use the keyboard.device directly!!


- Peter Philip
- Dalhousie University
- Halifax, NS

bryce@hoser.berkeley.edu (Bryce Nesbitt) (12/03/87)

In article <209@dalcsug.UUCP> peter@dalcsug.UUCP (Peter Philip) writes:
>Can anyone tell me where I could find an example of detecting which keys
>are being held down?  I need this for a game I would like to write, so it has
>to be quite fast and able to detect more than one key (like 'x' for turn
>right and '/' for thrust) simultaneously.  The only reference for doing
> this that I could find was in the RKM under keyboard.device - which also 
>says not to use the keyboard.device directly!!

For a game you want primarily *POSITIONAL* information.  ie: thrust
is to the left of fire.  For this application Intution RAWKEYS are
perfect.  For *everyone else* RAWKEYS need to be converted by the
	      ^^^^^^^^^^^^^^^
DeadKeyConvert() function.  (Example comming soon to comp.sources.amiga
and not-as-soon to a FISH disk near you)

 
You need to tell Intution to send your program events of class RAWKEY.
These will contain a code in the lower 7 bits, the high bit will tell
you if the key was pressed or released.  Keep track of this and you
will know at any one moment what keys are down.

The console.device chapter will have info on the codes.  The Intuition
manual will tell you how to get RAWKEY messages.



	>>REMEMBER: YOU ONLY USE RAW CODES FOR POSITIONAL CONTROL<<
		>>TEXT EDITORS, TERM PROGRAMS, AND ANYTHING<<
			>>ELSE USES RawKeyConvert() !!<<

|\ /|  . Ack! (NAK, SOH, EOT)
{o O} . bryce@hoser.berkeley.EDU -or- ucbvax!hoser!bryce
 (") 
  U	WARNING: hoser's spool directory eats a *lot* of mail. :-(

peter@sugar.UUCP (Peter da Silva) (12/03/87)

In article <209@dalcsug.UUCP>, peter@dalcsug.UUCP (Peter Philip) writes:
> Can anyone tell me where I could find an example of detecting which keys
> are being held down? ...  The only reference for doing
>  this that I could find was in the RKM under keyboard.device - which also 
> says not to use the keyboard.device directly!!

Open a window with IDCMP flags set to include RAWKEY.

When you get a key message...

	while(msg = GetMsg(win->UserPort))
		switch(msg->Class) {
			case RAWKEY:
				key_down[msg->Code & 0x7F] =
					(msg->Code & 0x80)?0:1;
				break;
		}

I'm doing this... quite fast. You do this rother than using the keyboard.device
directly because you want to be intuition-friendly. Don't you?
-- 
-- Peter da Silva  `-_-'  ...!hoptoad!academ!uhnix1!sugar!peter
-- Disclaimer: These U aren't mere opinions... these are *values*.

andy@cbmvax.UUCP (12/04/87)

In article <22036@ucbvax.BERKELEY.EDU> bryce@hoser.berkeley.edu (Bryce Nesbitt) writes:
>In article <209@dalcsug.UUCP> peter@dalcsug.UUCP (Peter Philip) writes:
>perfect.  For *everyone else* RAWKEYS need to be converted by the
>	      ^^^^^^^^^^^^^^^
>DeadKeyConvert() function.  (Example comming soon to comp.sources.amiga
>and not-as-soon to a FISH disk near you)

Or look in the V1.2 Enhancer manual if you can't wait.
-- 
andy finkel		{ihnp4|seismo|allegra}!cbmvax!andy 
Commodore-Amiga, Inc.

"Any sufficiently advanced technology is indistinguishable from
 a rigged demo."

Any expressed opinions are mine; but feel free to share.
I disclaim all responsibilities, all shapes, all sizes, all colors.

dpvc@ur-tut.UUCP (Davide P. Cervone) (12/12/87)

In article <1213@sugar.UUCP> peter@sugar.UUCP (Peter da Silva) writes:
>In article <209@dalcsug.UUCP>, peter@dalcsug.UUCP (Peter Philip) writes:
>> Can anyone tell me where I could find an example of detecting which keys
>> are being held down? ... 
>
>Open a window with IDCMP flags set to include RAWKEY.
>
>When you get a key message...
>
>	while(msg = GetMsg(win->UserPort))
>		switch(msg->Class) {
>			case RAWKEY:
>				key_down[msg->Code & 0x7F] =
>					(msg->Code & 0x80)?0:1;
>				break;
>		}
>
>-- Peter da Silva  `-_-'  ...!hoptoad!academ!uhnix1!sugar!peter
>-- Disclaimer: These U aren't mere opinions... these are *values*.

One caution about using this method in a multi-window, multi-tasking 
environment:  you may miss some key presses or key-up events.  For instance,
suppose I'm holding down the RETURN key (or any other key), and then click
in another window, then let go of the key, then click in the original window
again.  The original window will not see the RETURN-key-up event, and will 
think I'm still holding it down.  This becomes more serious if you are using
this method to track things like SHIFT keys (don't do this, use the Qualifier
field instead).

This may not be important in your application, but maybe it is.  Just though
I'd point out where the problem could come in.

Perhaps what we need is an input handler that keeps track of the keyboard
status (I'm pretty big on input handlers, you might have noticed).  This would
avoid the problem stated above, because if you put the handler before 
intuition, you will get ALL the key events no mater which window they are
destined for.  A really slick implementation would set up a message port
so that as many tasks as what to know could ask it for the current keyboard
state.  It could return some sort of array where each bit represented a key.
The details are left to the interested reader...

Davide P. Cervone
dpvc@tut.cc.rochester.edu
dpvc@ur-tut.UUCP
DPVC@UORDBV.BITNET

michael@stb.UUCP (Michael) (12/12/87)

In article <22036@ucbvax.BERKELEY.EDU> bryce@hoser.berkeley.edu (Bryce Nesbitt) writes:
>In article <209@dalcsug.UUCP> peter@dalcsug.UUCP (Peter Philip) writes:
>>Can anyone tell me where I could find an example of detecting which keys
>>are being held down?  I need this for a game I would like to write, so it has
> 
>You need to tell Intution to send your program events of class RAWKEY.
>you if the key was pressed or released.  Keep track of this and you
>will know at any one moment what keys are down.

WRONG!. RAWKEY only tells you what was pressed while you were active. If
another window was active, is intuition going to tell you about every
key stroke? Of course not--if it did, any program that used this would
trigger on everything. You need another way to get the present "Keys pressed"
state.
				Michael
-- 
: Michael Gersten		ihnp4!hermix!ucla-an!remsit!stb!michael
:				sdcrdcf!trwrb!scgvaxd!stb!michael
: "Copy Protection? Just say 'Off site backup'. "

bryce@hoser.berkeley.edu (Bryce Nesbitt) (12/13/87)

In article <10005@stb.UUCP> michael@stb.UUCP (Michael) writes:
>In article <22036@ucbvax.BERKELEY.EDU> bryce@hoser.berkeley.edu (Bryce Nesbitt) writes:
>>You need to tell Intution to send your program events of class RAWKEY.
>>you if the key was pressed or released.  Keep track of this and you
>>will know at any one moment what keys are down.
>
<WRONG!. RAWKEY only tells you what was pressed while you were active. If
<another window was active, is intuition going to tell you about every
<key stroke? Of course not--if it did, any program that used this would
<trigger on everything. You need another way to get the present "Keys pressed"
<state.

For the application presented (a game) the RAWKEY method is prefectly
adequate.  What person expects to hold down the "fire" key, click out
of the window (still holding the key) then click back in and have it
still firing?  Nobody.  The game designer will monitor ACTIVEWINDOW 
anyway, it won't cause any problem whatsoever.

With the RAWKEY method the game works smoothly and completely within
Intution.  One *could* use the keyboard.device directly (do a KBD_READMATRIX)
but there is no need for it.  The RAWKEY method is cleaner.

|\ /|  . Ack! (NAK, SOH, EOT)
{o O} . bryce@hoser.berkeley.EDU -or- ucbvax!hoser!bryce
 (") 
  U	WARNING: hoser's spool directory eats a *lot* of mail. :-(

dillon@CORY.BERKELEY.EDU (Matt Dillon) (12/14/87)

:In article <22036@ucbvax.BERKELEY.EDU> bryce@hoser.berkeley.edu (Bryce Nesbitt) writes:
:>In article <209@dalcsug.UUCP> peter@dalcsug.UUCP (Peter Philip) writes:
:>>Can anyone tell me where I could find an example of detecting which keys
:>>are being held down?  I need this for a game I would like to write, so it has
:> 
:>You need to tell Intution to send your program events of class RAWKEY.
:>you if the key was pressed or released.  Keep track of this and you
:>will know at any one moment what keys are down.
:
:WRONG!. RAWKEY only tells you what was pressed while you were active. If
:another window was active, is intuition going to tell you about every
:key stroke? Of course not--if it did, any program that used this would
:trigger on everything. You need another way to get the present "Keys pressed"
:state.
:				Michael

	Don't be dense Michael.  The context was "in an application", not
"I want to see every key pressed no matter which window is active".  Not
only that, but this context was directly refered to by Bryce when he said
"RAWKEY", which is an Intuition Device.  Not only that, but the second half
of your reply CONTRADICTS the first half.

	Try to understand an article before replying with a flame.

				-Matt

peter@sugar.UUCP (Peter da Silva) (12/16/87)

[ peter@dalcsug.UUCP (Peter Philip) wants to know how to tell what keys
  are pressed in a game he's writing]
[ bryce@hoser.berkeley.edu (Bryce Nesbitt) says to use RAWKEY and track it]

michael@stb.UUCP (Michael) writes:
> WRONG!. RAWKEY only tells you what was pressed while you were active. If
> another window was active, is intuition going to tell you about every
> key stroke? Of course not--if it did, any program that used this would
> trigger on everything. You need another way to get the present "Keys pressed"
> state.

The copy protection bandit strikes again.

In practical terms RAWKEY is fine for telling what keys are held down in a
game. If you need more than that, your best bet is an input handler. I don't
think Mr. Phillip needs go that far.
-- 
-- Peter da Silva  `-_-'  ...!hoptoad!academ!uhnix1!sugar!peter
-- Disclaimer: These U aren't mere opinions... these are *values*.