Results 1 to 3 of 3
  1. #1
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Overcoming the max-three-keys-at-a-time limitation of a keyboard.

    I admit to have posted this in another forum. But I didn't get help there for a while and would really like to solve this issue. Thanks for understanding.

    I'm working on a Java 2D game which requires a max of six keys be held down at the same time.
    The game is for two players on the same keyboard, playing simultaneously.

    However, all three computers I ran the program on only allow a max of three keys held at a time. They all have trouble with reacting to more than three keys being held.
    It seems that pressing a new key after three are already held, either cancels some other key-holding or is ignored.

    I've been told that this is a hardware issue. Most keyboards can't handle more than three keys held at a time. But a lot of games do require this, and they do not require special gaming-keyboards to run on my computer without problems.

    So there has to be a solution that will make the game playable on any standard keyboard.

    If there is, could you please explain to me how to code it in my program?
    (I use Key Bindings).

    The game's controls:

    Player 1

    - Rotate sprite and set angle of movement: LEFT arrow
    - Rotate sprite and set angle of movement: RIGHT arrow
    - Move forward: UP arrow
    - Shoot missile: ENTER key

    Player 2

    - Rotate sprite and set angle of movement: 'A' key
    - Rotate sprite and set angle of movement: 'D' key
    - Move forward: 'W' key
    - Shoot missile: 'T' key

    Relevant code:

    The Key Bindings part:

    Java Code:
        // An action for every key-press.
        // Each action sets a flag indicating the key is pressed.
    
    		leftAction = new AbstractAction(){
    			public void actionPerformed(ActionEvent e){		
    				keysPressed1[0] = true;
    			}
    		};
    		
    		rightAction = new AbstractAction(){
    			public void actionPerformed(ActionEvent e){
    				keysPressed1[1] = true;
    			}
    		};
    
        // And so on...
        // ....
    
        // An action for every key-release.
        // Each action sets a flag indicating the key was released.
        // This is only necessary for some of the keys.
    
    		leftReleased = new AbstractAction(){
    			public void actionPerformed(ActionEvent e){
    				keysPressed1[0] = false;
    			}
    		};
    		
    		rightReleased = new AbstractAction(){
    			public void actionPerformed(ActionEvent e){
    				keysPressed1[1] = false;
    			}
    		};
    
        // And so on...
        // ....
    
        // Binding the keys to the actions.
    
    		inputMap.put(KeyStroke.getKeyStroke("UP"),"upAction");
    		inputMap.put(KeyStroke.getKeyStroke("LEFT"),"leftAction");
        // etc...
    
    		actionMap.put("upAction",upAction);
    		actionMap.put("leftAction",leftAction);
        // etc...
    In the Board class. It has most of the game's code.
    This part checks the flags and reacts to key presses and releases.

    Java Code:
         keysPressed1 = tank1.getKeys(); // get flags-array of tank1.
    		    	keysPressed2 = tank2.getKeys(); // get flags-array of tank2.
    		    
    		    	if(keysPressed1[0]==true) // if LEFT is pressed.
    		    		tank1.setAngle(tank1.getAngle()-3);
    		    	
    		    	if(keysPressed1[1]==true) // if RIGHT is pressed.
    		    		tank1.setAngle(tank1.getAngle()+3);
    		    	
    		    	if(keysPressed1[2]==true){ // if UP is pressed.
    		    		tank1.setDX(2 * Math.cos(Math.toRadians(tank1.getAngle())));
    		    		tank1.setDY(2 * Math.sin(Math.toRadians(tank1.getAngle())));
    		    	}
    		    	
    		    	if(keysPressed1[2]==false){ // if UP is released.
    		    		tank1.setDX(0);
    		    		tank1.setDY(0);
    		    	}
    		    	
              // And the same for the second player's keys...
    This is mostly how reacting to key-presses and key-releases works in my program. When a key is pressed or released, a flag is set. The Board class reades the flags every game-loop cycle and reacts accordingly.

    As I said, the program doesn't react correctly to more than 3 keys held at a time, probably because of the keyboard. Is there a way to code a solution?

    Help will be very appreciated.
    Thanks a lot
    Last edited by AvivC; 01-07-2014 at 01:11 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: Overcoming the max-three-keys-at-a-time limitation of a keyboard.

    That's not something Java specific - I've had the two player key blocking problem for as long as I can remember [in commercial games, not my own programs]. I don't think this is anything you can really solve since its the hardware that is actually the limiting factor.

    The way to really SOLVE it is to not use the keyboard for both players; allow a different input device, say a game controller, for a second player. That is possible in Java but not using the standard API; you'd need something like JInput or LWJGL to access a gaming device.

    Note that it matters which keys on the keyboard you use; I've had most success letting one player use the keys around WSAD and let the other player use the keys on the keypad.
    Last edited by gimbal2; 01-07-2014 at 01:21 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Default Re: Overcoming the max-three-keys-at-a-time limitation of a keyboard.

    It will cause A little bit of mistiming but what if you had a timer running, lets say every 5 milliseconds and what happens is when you press a key it changes a boolean to true. When the timer fires it sees what keys have been pressed and reacts to it. then resets to false.

Similar Threads

  1. pressing two keys at the same time
    By inbar7176 in forum Eclipse
    Replies: 7
    Last Post: 12-12-2013, 01:24 PM
  2. Replies: 0
    Last Post: 05-12-2013, 10:51 AM
  3. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  4. Limitation of audio class
    By khuong@austin.rr.com in forum New To Java
    Replies: 0
    Last Post: 03-25-2011, 12:27 AM
  5. Why syntax limitation with try/catch?
    By XmisterIS in forum New To Java
    Replies: 5
    Last Post: 09-22-2010, 03:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •