This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zamoht
Zamoht wrote ...

2012/7/5

Double pressing a key?

Zamoht Zamoht

2012/7/5

#
I want you guys to help me find a way to make a program do something if a key is pressed and then do something else if the key is pressed twice. I found this code for double clicking "if(mouse.getClickCount()==2", but can you give me some help on double clicking just with a key? Thanks for your time.
erdelf erdelf

2012/7/5

#
this outside of any methods
private int clicked = 0; 
and this in act:
   if(Greenfoot.isKeyDown("g"))
   {
        if(clicked == 0) clicked = 1;
        else if(clicked == 1) clicked = 2;
        else if(clicked == 3) 
        {
           // your stuff here
          clicked = 0;
        }
   }
Zamoht Zamoht

2012/7/5

#
The problem with this code is that "//your stuff here" happens every time you press g no matter how long there is between the presses. Is there a way to make it like a double click, so you have to press the same key twice in a short amount of time? I was thinking that you could put some kind of loop into the act method, but I'm not sure that would work very well.
nccb nccb

2012/7/5

#
I think you either need to look for the key *not* being down in the middle of the two presses to detect a double tap, or use getKey() to detect the two presses.
erdelf erdelf

2012/7/5

#
ok, I will try again. You want to put a loop in the act() method. This would make the game lagging cause other objects couldn't move if you hold the act method process. try this outside of methods:
private double clicked; 
in act
if(Greenfoot.isKeyDown("g"))
{
     if(System.currentTimeMillis() > clicked + 1000) // this gives me one sec to click
     { 
         // your stuff
     } else 
     { 
        clicked = System.currrentTimeMillis();
     }
} 
danpost danpost

2012/7/5

#
private String actString = ""; // the final Strings that determine what actions to perform
private String timerString = ""; // the undetermined String that can be built on
private int keyTimer = 0;
// declare the above in the class, then in the act call the following two methods
performActions();
buildString();
// 
private void performActions()
{
    if ("".equals(actString)) return;
    if ("g".equals(actString)) {...}
    if ("gg".equals(actString)) {...}
    if ("s".equals(actString)) {...}
    // etc.  perform all the action in this method
    // use of 'else' between 'if's is optional
    actString = "";
}

private void buildString()
{
    if (keyTimer > 0) keyTimer--;
    if (keyTimer == 0 && !"".equals(timerString))
    {
        actString = timerString;
        timerString = "";
        return; // give a chance for new actString to be acted on
    }
    String key = Greenfoot.getKey();
    if (key == null) return;
    if (timerString.equals(key))
    {
        actString = timerString + key;
        timerString = "";
        keyTimer = 0;
        return;
    }
    if (!"".equals(timerString)) actString = timerString;
    timerString = key;
    String doubleClickers = "gst"; // list those keys that a double press does something different
    if (doubleClickers.indexOf(key) == -1) keyTimer = 1; else keyTimer = 40;
}
The reason for the final 'keyTimer = 1' bit, is because in the event there was a doubleClicker waiting in timerString and a different key was pressed (therefore, the doubleClicker should be acted on as a singleClicked key), that key would be pushed into actString, waiting to be processed. We do not want to over-write it with the new singleClicker that pushed it on; so, we wait one cycle with it in timerString. I have not tested the code, but I think it should work.
You need to login to post a reply.