Can I use two keys, held one after another, to make an object appear?

So put it like this. Is it possible to have it were, if I held 'w' then 's', it would perform my command?

Answers

  • edited July 2015 Answer ✓
    boolean w_pressed = false;
    
    void setup(){
      size(200,200);
    }
    
    void draw(){
      background(0);
    }
    
    void keyPressed(){
      if(key=='w') w_pressed = true;
      if(key=='s' && w_pressed) println("CMD");
    }
    
    void keyReleased(){
      if(key=='w') w_pressed = false;
    }
    
  • @TfGuy44 didn't you mean keyReleased instead of mouseReleased ? :)

  • w_pressed must be set false after cmd and after any key (except w)

  • alright thanks guys!

  • so say if I wanted to draw an ellipse, how would I make it stay there until i released 's' button? It's just flashing the ellipse.

  • _vk_vk
    edited July 2015 Answer ✓

    Functions like mousePressed keyPressed or the released ones are callback functions, meaning they are called once when the respective event happens. Draw is called in a infinite loop (by default at 60fps) and updates the 'canvas' once each frame. So if you draw inside a callback function the draw will be fast erased by next background call in draw (assuming you have one).

    You can do either, use a flag changed in callback function to turn on/off the drawing of the ellipse:

    //global
    boolean doDraw = false
    
    //in draw
    if(doDraw)
    ellipse(x, y, w, h)
    
    //in callback
    if(key ... the usual)
    doDraw = !doDraw // this will toggle the boolean
    

    or, if you are adding lots of stuff to be draw, use a collection and add a new instance at callback

    //in draw
    for(CoolDraw cd:coolDraws)
    cd.display()
    
    // in callback
    coolDraws.add(new CoolDraw())
    //or remove some... whatever...
    

    or yet use the var keyPressed direct in draw. Usually this is not recommended, as it tends to be not precise for detecting key presses reporting several times for one press (remember 60 times each sec). But maybe in that case it may be a good choice...

    //in draw
    if(keyPressed && key = '?'){
    ellipse(x, y, w, h)
    }
    

    This can be combined with handling in callback functions.

  • You would need to keep track of if the ellipse should be drawn somehow. If the right combinations of buttons is pressed and the ellipse is not already showing, it should start showing. If the right combinations of buttons is pressed and the ellipse is already showing, it should stop showing.

    You will probably want to use a new boolean to track this.

    boolean ellipse_showing = false;
    

    When does this value change? When do you use it to determine if something else should happen?

  • Alright it makes sense now. Thanks for the quick help, I'm new on here and I'm loving it.

  • Really I'm just playing around and I was just wondering. When I pasted your first code in, I changed the"CMD" to an ellipse, and it only flashed the ellipse every time I pressed the combo of keys. So I was just wondering if I could have it were, it showed the ellipse as long as I held down the combination of keys. Thanks to _vk it reminded me that I could of just threw everything in a void draw. thanks again to everybody helping me out!

Sign In or Register to comment.