Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multiple Key Presses at once  (Read 1936 times)
0 Members and 1 Guest are viewing this topic.
Offline lordanki

Junior Newbie





« Posted 2006-10-11 20:59:44 »

Last year in my regular computer science class I made a pong clone with two player capability. The problem is that you had to let up on the key so that the other person could move as well. How do I go about making it so where both keys can be pressed simutaneously and the bars move up and down. Thanks. - Jordan
Offline woogley
« Reply #1 - Posted 2006-10-12 03:14:36 »

track the keys in an array. this way you can check their state in your game loop, which is better than checking it inside the event method. an example can be found at http://woogley.net/misc/MultiKey/
Offline CommanderKeith
« Reply #2 - Posted 2006-10-12 04:08:37 »

Actually I've had this problem too.

I think the trouble is that the keyboard's buffer can't handle that many keys and so it won't actually tell the operating system when a new key is pressed.  I don't think there's any way around it.  I have noticed that some keyboards are worse than others however and some keys seem to fill the buffer quicker than others.  I'm pretty sure this isn't a java-only thing but happens in all games & programs.

It's got a funnuy side effect however- the best strategy in your pong game might be to jam the keys so your opponent can't move!

Keith

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dishmoth
« Reply #3 - Posted 2006-10-12 10:11:24 »

I think the trouble is that the keyboard's buffer can't handle that many keys and so it won't actually tell the operating system when a new key is pressed.  I don't think there's any way around it.  I have noticed that some keyboards are worse than others however and some keys seem to fill the buffer quicker than others.  I'm pretty sure this isn't a java-only thing but happens in all games & programs.

Last time I wrote a two-player game (ten years ago!) I ran up against this problem.  My keyboard seemed to be the only one on the planet on which the game was playable.  (That was with up, down, left, right and fire keys for each player, and players would often be holding down three keys at once -- two directions plus fire.)

As far as I can tell, the severity of the problem varies from keyboard to keyboard (the keyboard on my current laptop is particularly bad for it).  Some key combinations are worse than others, but I've noticed that there's less of a problem for the 'special' keys such as Shift, Alt, Ctrl and the Cursor keys.  (If I remember correctly Doom used to use those keys -- maybe not a coincidence?)  That said, you may run into other problems if you use those keys in a game because some operating systems (or users) assign special extra functionality to them (for example, if you hold down the right shift key for long enough on Windows, unexpected things happen).

The solution?  Provide alternative playing keys, or user-definable keys.  And avoid writing two-player games!  Wink

BUT...  The original example was talking about a two-player Pong game, which presumably wouldn't expect the players to be pressing more than one key each at a time.  So in that case I'm guessing that the problem is more likely to be the way that the code is processing the key events (as woogley said), rather than the events that the keyboard is sending.

Simon

Offline Kova

Senior Devvie





« Reply #4 - Posted 2006-10-12 15:21:05 »

yeah, this is a common problem... solution is to set up actions on keys that dosen't conflict with each other. Don't know how keyboard works, but I know that for some keys pressing more of them will not register the last or last couple pressed while if you press more keys wich are on different sides of keyboards or they are special keys then everything is fine. So I'm assuming some keys are sharing same connection to computer.
Offline woogley
« Reply #5 - Posted 2006-10-12 16:07:04 »

I feel so.. ignored Tongue

by tracking the keys with an array, you can track several key presses at once (since you're not doing it inside the event method). most keyboards will shoot about 4 or 5 maximum key presses at the same time - more than enough for a pong game - especially since you won't be pressing that many keys at the same time (2 max: player 1 down, player 1 up, vice versa, etc)
Offline dishmoth
« Reply #6 - Posted 2006-10-12 16:33:38 »

I feel so.. ignored Tongue

Not ignored...  Never that... Wink

Your original post answered the question so thoroughly that there wasn't anything to add.  So we all just started grumbling about keyboard hardware limitations instead.

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #7 - Posted 2006-10-12 17:17:06 »

There are 2 possible hardware issues. The first one is that keyboards only support up to x keys at the same time (with old and crappy ones that was 2 or 3) and the other is the keymatrix (how the keys are wired up). On cherry keyboards for example up+left+space doesnt work at the same time.

There are more expensive gamer keyboards which allow you to press any 10 keys at the same time.

弾幕 ☆ @mahonnaiseblog
Offline Kova

Senior Devvie





« Reply #8 - Posted 2006-10-12 21:48:18 »

I feel so.. ignored Tongue
...

We (I think) were talking about hardware limitation, not the one of keyboard press event that can contain single keycode at a time.
So there isn't a really a solution to this Sad ... strange what kind of stuff are considered expansive for mass production and what is still troubleing us for many years while computer development improved exponentionaly.
Offline Jeff

JGO Coder




Got any cats?


« Reply #9 - Posted 2006-10-12 22:20:07 »

Yes.

PC Keyboards are scanned in a matrix.  What this means is that they "ghost".  Certain key combinations end up creating "phantom" strokes to the scanner or hiding one or another of the keys.

Hardware problem. Nothing you can do about it short of chosing sepcific key combinations that dont conflict.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!