r/processing • • May 14 '21

Hi guys need HELP!!!!!

Hi can anyone tell me how to have two separate key pressed events in your code in processing.

Currently I have 2 seperate keypressed events.

I want the 2 keyPressed events to occur 1 at a time.

But at the moment they occur at the same time when a key is pressed.

0 Upvotes

10 comments sorted by

4

u/mflouze Seeker of Knowledge May 14 '21

Hi ! It would be easier to help you if you showed us your code.

0

u/ComfortableMachine4 May 14 '21

int speed = 0;

void setup() {
size(200,200);
}

void draw() {
line(12,56,78,163);
line(62,76,178,183);
line(72,86,98,153);
line(23,96,28,143);

if(keyPressed) {
circle(45,45,125);
speed = 3;
}

if(speed == 3 && keyPressed) {
rect(56,75,156,197);
}
}

3

u/[deleted] May 14 '21

The computer runs your lines of code from top to bottom. When the key is pressed, you set the speed to 3, then immediately you check if it's 3, which of course it will be! Can you think of another way to do that?

0

u/ComfortableMachine4 May 14 '21

Sorry I don't understand what you are trying to say.

-7

u/ComfortableMachine4 May 14 '21

IS THERE A WAY THE 2 KEYPRESSED EVENTS CAN OCCUR SEPERATELY

2

u/kstacey May 14 '21

Where is keypressed declared an even set in this?

1

u/x-seronis-x May 14 '21

its a processing state variable like frameCount that is defined in the processing environment. The OP was just misleading by referring to the event which they did NOT use. They're merely examining the state variable

1

u/mflouze Seeker of Knowledge May 14 '21

Your code might not work for 2 reasons.

- First, the keyPressed() function is never called. This function allows you to define events linked to specific keys on the keyboard. In your code, you didn't define separate keys events. Instead, you use the keyPressed event which respond to any key.

- The second issue is related to the first one. My guess is the speed variable was set to 3 while Processing was still listening to the generic keyPressed event. Therefore, the two if conditions were true at the same time. (If someone can confirm or deny this explanation, I'm curious!)

I recommend you used the Processing native keyPressed() function (see the doc for syntax).In this function, you must define which key must be detected (eg ENTER, a number, a letter, etc). It goes with an if condition to detect a specific key and operate in consequence (again, see the doc).
So you have a key to draw the circle and set the speed, and another key to draw the rectangle (that will work only if the speed is set to 3).

I did a quick edit of your code :

    int speed = 0;
    
    void setup() {
      size(200,200);
    }
    
    void draw() {
      line(12,56,78,163);
      line(62,76,178,183);
      line(72,86,98,153);
      line(23,96,28,143);
    }
    
    void keyPressed() {
      if(key == ENTER) {
        circle(45,45,125);
        speed = 3;
      }
      
      if(key == '0' && speed == 3) {
        rect(56,75,156,197);
      }
    }

I hope these explanations are clear.

EDIT: If someone has a better solution, or a more elegant one, feel free to add (or remove) something!

3

u/i-live-life May 14 '21

In your code you are looking at the keyPressed event in one if statement and then again seperately in other. Since the key has been pressed, both of these events become true and both sets of code are executed.

The reference documentation has an example that uses keyCode so that you can do different things depending on the actual key pressed, see here: https://processing.org/reference/keyCode.html

1

u/x-seronis-x May 14 '21 edited May 14 '21

First fix your terminology. You're not using the keyPressed event at all. You're using the state variable.

https://processing.org/reference/keyPressed_.html

The keyPressed() event (if you start using it) will be called exactly once per key being pressed. Its not called 'at exact same time'. But you can set whatever state you want during those events and react to them at the same time in your draw() event. This allows you to handle different actions based on different keys.

You can always choose to keep NOT using the keyPressed event and only examine the keyPressed state variable like you are currently doing. And if you want to do multiple things then just DO MULTIPLE THINGS. You can put as many lines of code as you want in whatever conditional block checks the state