Multiple Key Presses at once

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

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/

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

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

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.

I feel so… ignored :stuck_out_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)

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.

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.

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 :frowning: … 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.

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.