Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Overcoming the max-keys-at-a-time limitation of a keyboard.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    I have posted this in another forum. I know 'croos-posting' isn't well-accepted here, but I didn't get help there for a long while and would very much like to solve this problem. Thank you 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:

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

         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 much appreciated.
    Thanks a lot


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

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

    Quote Originally Posted by AvivC View Post
    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?
    No, by software there are no solutions (and you have had a clear answer in http://www.javaprogrammingforums.com...eystrokes.html).
    The manipulation of input/action maps does not improve/solve anything in that sense, at most only complicates the code.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

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

    You cannot overcome the hardware limitations of your keyboard. I'd suggest looking for a different combination of key strokes that aren't so susceptible to ghosting. There is a neat app on Microsoft Applied Science to identify ghosted keypress combinations. Manufacturers wire keyboards in such a way as to prevent ghosting on common keypress combinations which is why SHIFT+A+W+D+SPACE will work but H+J+K won't.

  4. The Following 2 Users Say Thank You to ChristopherLowe For This Useful Post:

    andbin (January 7th, 2014), GregBrannon (January 7th, 2014)

Similar Threads

  1. Please help with Arrow Keys
    By rtucker1023 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2013, 04:58 PM
  2. [SOLVED] quick keys
    By Gerardgrundy in forum Java IDEs
    Replies: 7
    Last Post: November 5th, 2012, 03:59 PM
  3. Overwrite what Keys do.
    By Melawe in forum Java Theory & Questions
    Replies: 4
    Last Post: May 17th, 2012, 11:23 PM
  4. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 08:50 PM
  5. combining keys
    By subhvi in forum Java SE APIs
    Replies: 10
    Last Post: August 29th, 2009, 04:35 PM

Tags for this Thread