Simplest SHIFT + ARROW key combination (outside keyPressed())

I was looking for some codes but many results are for processing one, or using deprecated keyEvent code, or relying on keyPressed() function, or even depending on extra libraries...

I believe keyPressed() function doesn't work for me now so I am using keyPressed boolean.

This solution works partially. My code will stop firing after shift is pressed/released. I just wanted the Shift key to change "speed" of a movement while user is holding an arrow key permanently.

KeyEvent e; //do I really need this workaround?

void setup() {
}

void draw() {

  if (keyPressed) {
    //int location = keyEvent.getKeyLocation();
    //println( location );
    //KeyEvent.KEY_LOCATION_LEFT:
    if (key == CODED) {
      if (keyCode == LEFT) {
        if (e.isShiftDown()) { //can't I get KeyEvent "e" from other than keyPressed( KeyEvent )?
          println(millis(), "FASTER");
        } else {
          println(millis(), "SLOWER");
        }
      }
    }
  }
}

void keyPressed(KeyEvent ev) {
  e = ev;
}

Answers

  • Answer ✓

    I don't understand why you can't use keypressed().

    void keyPressed(){
      if (key==CODED){
        if (keyCode == LEFT) leftIsPressed = true;
        if (keyCode == SHIFT) shiftIsPressed = true;
    }
    
    void keyReleased(){
      if (key==CODED){
        if (keyCode == LEFT) leftIsPressed = false;
        if (keyCode == SHIFT) shiftIsPressed = false;
    }
    
  • Thanks for your reply!

    keyPressed() works, but it will produce a lot of extra lines for the implementation I need to do... I was just wondering if this is the best practice to trigger the action as I wanted. Using your snippet I was able to make the code below:

    boolean leftIsPressed = false;
    boolean shiftIsPressed = false;
    
    void setup(){
      size(200,200);
    }
    
    void draw(){
      if(leftIsPressed && !shiftIsPressed){
        println(millis(),"Ok");
      }
      else if(leftIsPressed && shiftIsPressed){
        println(millis(),"shift Ok");
      }
    }
    
    void keyPressed(){
      if (key==CODED){
        if (keyCode == LEFT) leftIsPressed = true;
        if (keyCode == SHIFT) shiftIsPressed = true;
      }
    }
    
    void keyReleased(){
      if (key==CODED){
        if (keyCode == LEFT) leftIsPressed = false;
        if (keyCode == SHIFT) shiftIsPressed = false;
      }
    }
    
  • edited July 2016
    // forum.Processing.org/two/discussion/17429/
    // simplest-shift-arrow-key-combination-outside-keypressed#Item_3
    
    // GoToLoop (2016-Jul-06)
    
    boolean leftIsPressed, shiftIsPressed;
    boolean keyAction;
    
    void setup() {
      size(250, 200);
      frameRate(3);
    }
    
    void draw() {
      frame.setTitle("Frame: #" + frameCount);
      background((color) random(#000000));
    
      if (keyAction) {
        keyAction = false;
    
        if      (leftIsPressed & !shiftIsPressed)  print("LEFT w/o SHIFT\t");
        else if (leftIsPressed & shiftIsPressed)   print("LEFT + SHIFT\t");
      }
    }
    
    void keyPressed() {
      setKeyState(keyCode, keyAction = true);
    }
    
    void keyReleased() {
      setKeyState(keyCode, false);
    }
    
    void setKeyState(final int k, final boolean state) {
      if      (k == LEFT)   leftIsPressed  = state;
      else if (k == SHIFT)  shiftIsPressed = state;
    }
    
  • keyPressed() works, but it will produce a lot of extra lines for the implementation I need to do...

    Yes, it's a bit more code, but not that much more? Besides, you separate the key-stuff/interaction from the other stuff in draw(). Which means your code will probably be easier to read and work with.

    GoToLoops's code is even longer. But also even cooler :)

Sign In or Register to comment.