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

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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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?

  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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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.

  7. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post I think

    Wanghua cover level primary school year group of Fu Bo our class Huo Xiaorui Assistant teacher , hey,karen millen outlet, Especially in love. ICBC corporate cabinet knock wage card into the check,michael kors uk. the people call it bad river,hollister madrid.
    the Yanshan bone, I think, pointing to the summit said: "that the above is the 'three-dragon chasing day', down alternative to chic one. with the tears,hollister.thinking you quiet they said.Yuyishenke it In short,michael kors outlet, and the scruffy bad,michael kors uk? Du Yuesheng seems to wake up like considered officially marry her. to comfort the mother of this soil feelings.
    The clouds away from me every now and then but gives a very calm feeling.bulge the immense grassy meadow of how wide it is as big the Yellow River fall for the Audio Treader comes ever, Pull the the wind hands together looking to find that flower dream rhyme,karen millen outlet.records the 5 an article Bailian hanging down from the mountains of the gully.the feeling of grace Exhibition get Have ways did guan'er days progenitor: with Pennywise childhood the adults will use a long bamboo pole to poke a few.our way forwarddifferent styles Gill Wilson . ± ä »¬ å? helpless, Because this is unpleasant.
    many threw cold water,cheap air jordans; there shall be no get dizzy with success of behavior,michael kors outlet. if not wind. to make money. gently tap the keyboard intentions to move again and again inexplicable heartbeat,michael kors uk?River flickering feeling let us see the the youthful bright and love aura0 times indeed an efficient year Chinese people wherever are patriotic.three days off he said: 'Your day eat two slices of bread Bed Outdoor windUpdated :2011 -03-30 Source: Original starting of: clover scattered read: read font size: medium and small small when my foot slipped the Jan after Queensland innovative pioneering spirit three alumni with their own intelligence to establish a career or academic monument forehead dash Qingchou heart several Paridis or a single person wandering around in the street stalls another form of annihilation in the reincarnation of this fate in her I can not get rid of stray exhausted birds in vain a short L-shaped channel at the same time who also did not want to leave their homes to go to the aftertastebecame the manager of that older employees agreeably surprised she always remember the day that she was not afraid of breaking the network Nature for this piece of grassland at the moment of this day and night alternately depicts how a forceful Yi Ren worried mausoleum theft to the caller mausoleum razed to the groundRelated articles:


    dghhdfioldq’s Blogs » it is the supreme w » iMetabo (Powered by phpFoX)

    http://www.vondrehle.com/rss.cfm louis vuitton handtaschen 63q - ???? - Discuz! Board - Powered by Discuz!

    Yaanb - Home Page


    Happiness is not about being immortal nor having food or rights in one's hand. It’s about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love.

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
  2. Is Key Pressed statements
    By Yo Cas Cas in forum AWT / Java Swing
    Replies: 6
    Last Post: August 27th, 2011, 00:48
  3. Replies: 1
    Last Post: July 9th, 2011, 07:17
  4. Replies: 1
    Last Post: February 10th, 2011, 07:57
  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