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 4 of 4

Thread: KeyListener - how to make program react to the key that was pressed twice?

  1. #1
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default KeyListener - how to make program react to the key that was pressed twice?

    So, shortly...

    My mini game:
    When I press a 'Space bar' key - the first ball (A) stops moving, and the second ball (B) starts moving.
    The problem: How to make the second ball (B) stop moving with a same (Space bar) key?
    What i need: 1st time 'Space bar' pressed - first ball stops. 2nd time 'Space bar' pressed - second ball stops.

    How to do that?

    I manage to make them both stop if i set that balls stop moving after different keys pressed. But I don't need that.


    public void run() {
    		while(running){
    			if(BallA.ballMoves)
    				BallA.move();
    			if(!BallA.ballMoves){
    				BallB.move();
    }
     
    @Override
    	public void keyPressed(KeyEvent e) {
     
    		if(e.getKeyCode() == KeyEvent.VK_SPACE){
    			BallA.ballMoves = false;
    		}

    Sorry, if I'm writing in a wrong sub-forum...

  2. The Following User Says Thank You to scorpas For This Useful Post:

    tsharetlme (November 30th, 2012)


  3. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeyListener - how to make program react to the key that was pressed twice?

    You need a way to keep track of the state of the key presses. Assign a different value to a variable for each state: No presses, one press, two presses, ????
    How many states are there? Is there a 'three pressed' state or does the state restart at No presses?
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (November 28th, 2012)

  5. #3
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: KeyListener - how to make program react to the key that was pressed twice?

    Quote Originally Posted by Norm View Post
    You need a way to keep track of the state of the key presses. Assign a different value to a variable for each state: No presses, one press, two presses, ????
    How many states are there? Is there a 'three pressed' state or does the state restart at No presses?
    no presses - first ball moves.
    one press - first ball stops, second ball starts to move.
    two presses - second ball stops (both are not moving).

    Then some small action happens in the game and the thing with a 'space bar' key should restart.

    I'm making a small basketball shooting game.
    1st thing what I need is to set shot's accuracy. So there is a cross on which one ball is moving up and down, the other ball moves - left and right. I'm trying to stop them both with 'space bar' key.
    Untitled-1.png

    When both balls will be stopped, shot's accuracy will be calculated and the shot will happen. After the first shot, I will need to set an accuracy for the second shot and so on and on.

    I hope you understand what I'm trying to say.
    Sorry for my bad english....

  6. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeyListener - how to make program react to the key that was pressed twice?

    How are you thinking of keeping track of the state of the key presses? Can you post the logic the program needs to use to keep track of the key press state?
    It will require a variable with values for each different state and some if statements to determine the current state and statements to change the state from the current state to the next state.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How Should I React to Softpedia Using My Programs?
    By KevinWorkman in forum The Cafe
    Replies: 9
    Last Post: August 29th, 2012, 10:17 AM
  2. Is Key Pressed statements
    By Yo Cas Cas in forum AWT / Java Swing
    Replies: 6
    Last Post: August 27th, 2011, 12:48 AM
  3. Replies: 1
    Last Post: July 9th, 2011, 07:17 AM
  4. Replies: 1
    Last Post: February 10th, 2011, 08:57 AM
  5. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM