Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (525)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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  
  Simultaneous Keyboard Button Input  (Read 4222 times)
0 Members and 1 Guest are viewing this topic.
Offline DoubleBarrel

Senior Newbie





« Posted 2011-11-19 17:33:04 »

I am using slick2d to implement an asteroids-like game. In the update method I am listening to the up,right and left buttons, and if pressed I am moving the spaceship around. I am also listening to the space button, which when pressed causes bullets to appear. Problem is when any two keys are pressed together for example the up button & the space button everything works fine: the spaceship moves forward and bullets starts firing; BUT the moment when more than two buttons are pressed simultaneously, the last button to be pressed is ignored. Example if the up button, the left button and the space button are pressed: the spaceship moves in vertically and to the left but does not fire any bullets. Is this a common problem when implementing games and do any techniques exist so as to address simultaneous keyboard/gamepad input. Thank you very much. DB.

And Java Goes On
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2011-11-19 17:40:40 »

Behold pseudo-code.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
private ArrayList buttonsPressed = new ArrayList();

onButtonPressed(int keycode)
{
    if (!buttonsPressed.contains(keycode))
    {
        buttonsPressed.add(keycode);
    }
}

onButtonReleased(int keycode)
{
    buttonsPressed.remove(keycode);
}

respondToKeyboardInput()
{
    for (int i = 0; i < buttonsPressed.size(); i++)
    {
        if (buttonsPressed.get(i) == Key.LEFT)
        {
            goLeft();
        }
        else if (buttonsPressed.get(i) == Key.JUMP)
        {
            jump();
        }
        //etc
    }
}

See my work:
OTC Software
Offline DoubleBarrel

Senior Newbie





« Reply #2 - Posted 2011-11-19 17:46:45 »

=) Wonderful!! A thousand thanks.  Grin

And Java Goes On
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DoubleBarrel

Senior Newbie





« Reply #3 - Posted 2011-11-19 22:15:56 »

Hi, Sorry to ask again. Could not correctly detecting multiple keyboard input be a hardware problem rather than a software problem? I implemented my input handling as indicated in the above pseudocode and it works fine but not all the time. What I did was to output the id of the key pressed to standard output in the onButtonPressed() method. What happens is that when two buttons are pressed at the same time their id number is outputted but when a third button is added to the combination - its number will not always output. So if the onButtonPressed() method is supposed to catch all instances when a button is pressed, can it be assumed that not all keyboards register all the keys that are pressed concurrently? - thanks again.

And Java Goes On
Offline lhkbob

JGO Knight


Medals: 32



« Reply #4 - Posted 2011-11-19 22:33:55 »

Certain keyboards do have problems where combinations of 3 keys incorrectly report presses, other keys, or don't respond at all.  Look into an explanation of key jamming and ghosting here to see if maybe this is the case: http://en.wikipedia.org/wiki/Rollover_(key)

As an example on my Mac's keyboard, I can hold Q, W, F and then the R key is completely ignored and won't trigger any press events.

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #5 - Posted 2011-11-20 06:23:32 »

I do it this way, in the update method
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
if (input.isKeyDown(Input.KEY_LEFT)) { // Move player left
         // Moving left..
      } else if (input.isKeyDown(Input.KEY_RIGHT)) { // Move player right
         // Moving the player right..
      } else { // No movement
         // Reset animations
      }
if (input.isKeyDown(Input.KEY_SPACE)) { // Jumping
         // Jumping stuff
}


So that everytime you dont care if anything else is pressed, you can just make a new if-statement for it. Kind of self-explainatory.
You can grab input through the container btw.

Meh.. forum messed up my tabbing :L

Offline DoubleBarrel

Senior Newbie





« Reply #6 - Posted 2011-11-20 10:35:43 »

Thanks everyone for replying  Grin I finally think to have managed to find a solution. Rather than using the letter keys from the keyboard (A,B,C,D,E,F..and SPACE) when simultaneous key presses are required use keys such as ctrl or alt in combination with any other two keys. These mentionned keys are wired differently from the rest of the keys (to keep explanations simple) and seem to be more reliable when multiple keys need to be detected if pressed simultaneously. At least, this works with my keyboard anyway. All three keys (alt,ctrl and any two directional arrows) register when pressed together. Thanks again  Pointing Cool

And Java Goes On
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #7 - Posted 2011-11-21 01:47:22 »

Thanks everyone for replying  Grin I finally think to have managed to find a solution. Rather than using the letter keys from the keyboard (A,B,C,D,E,F..and SPACE) when simultaneous key presses are required use keys such as ctrl or alt in combination with any other two keys. These mentionned keys are wired differently from the rest of the keys (to keep explanations simple) and seem to be more reliable when multiple keys need to be detected if pressed simultaneously. At least, this works with my keyboard anyway. All three keys (alt,ctrl and any two directional arrows) register when pressed together. Thanks again  Pointing Cool
3 keys should register fine, but 4 is pushing it. Like you said, modifier keys are often differently wired, so they should not count towards this limit. I've never had a problem with this in any game though.

Myomyomyo.
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.

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

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

toopeicgaming1999 (13 views)
2014-11-26 15:20:08

SHC (26 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (30 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (53 views)
2014-11-12 21:11:50
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!