Results 1 to 3 of 3
  1. #1
    garnaout is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Do events while button is pressed

    I want to call methods while button is pressed and stop when released (it's a fast forward button) just to be clear I want something as simple as this:

    Java Code:
    pseudocode:
    
    As long as F is pressed
    System.out.println("Forwardinggggggg");
     
    When F is released
    System.out.println("Forwardinggggggg Stopped");
    I know I need a listener and I did something like that but it's not working, what am I missing?

    Java Code:
    ... boolean pressed = false;
     
    else if (name.equals("fastForward") && keyPressed) {  
     
                        pressed = true;
     
                        if(pressed == true)
                        {
                        System.out.println("Forwardinggggggg");
                        }
                    }
     
                    else if (!name.equals("fastForward") && !keyPressed)  { pressed = false;
                    System.out.println("Forwardinggggggg Stoppsss");}
    if I keep pressing F I don’t see the System.out.println(“Forwardinggggggg”); repeating


    thanks in advance

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default Re: Do events while button is pressed

    1. Button (as in java.awt.Button, javax.swing.JButton ...) or keyboard key?
    2. What UI toolkit? AWT/Swing/SWT/other? or is this a console app?

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    garnaout is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Do events while button is pressed

    its jMonkey (animation/game development)

    it's a keyboard key sorry I should have clarified it.

    see code below

    Java Code:
    private void initInputs() {
    
            inputManager.addMapping("fastForward", new KeyTrigger(keyInput.KEY_F));
            inputManager.addMapping("rewind", new KeyTrigger(keyInput.KEY_R));
    
            ActionListener acl = new ActionListener() {
                public void onAction(String name, boolean keyPressed, float tpf) {
                    
                   if (name.equals("fastForward") && keyPressed) {  
    
                        pressed = true;
                        
                        if(pressed == true)
                        {
                         System.out.println("Fast Forward");
                        }                           
                    }
                    
                    else if (name.equals("fastForward") && !keyPressed)  { pressed = false; 
                    System.out.println("Fast Forward STOPS");} 
                    
                    
                    else if (name.equals("rewind") && keyPressed) {
    
                        System.out.println("REWINDS");
                    }
    
                }
            };
            inputManager.setCursorVisible(true);
             
            inputManager.addListener(acl, "fastForward");
            inputManager.addListener(acl, "rewind");
        }

Similar Threads

  1. How to set button to be pressed more than once
    By ajs1351 in forum New To Java
    Replies: 20
    Last Post: 12-04-2011, 06:53 AM
  2. Pausing and starting a loop when a button is pressed.
    By xan.amini in forum New To Java
    Replies: 16
    Last Post: 08-11-2011, 08:15 AM
  3. Checking if a button was pressed in a Window.
    By Valkyrie in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 05:28 AM
  4. Waiting for a button to be pressed
    By SomeGuyOverThere in forum New To Java
    Replies: 6
    Last Post: 08-21-2008, 09:30 PM
  5. Need Help With Button Events
    By adlb1300 in forum New To Java
    Replies: 5
    Last Post: 11-03-2007, 05:36 AM

Posting Permissions

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