# Calling simultaneous functions in draw()

**URL:** <https://discourse.processing.org/t/calling-simultaneous-functions-in-draw/39497>\
**Category:** Coding Questions\
**Created:** [October 29, 2022, 11:07pm UTC](https://discourse.processing.org/t/calling-simultaneous-functions-in-draw/39497 "2022-10-29T23:07:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcabc](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@bcabc](https://discourse.processing.org/u/bcabc)\
**Post date:** [October 29, 2022, 11:07pm UTC](https://discourse.processing.org/t/calling-simultaneous-functions-in-draw/39497/1 "2022-10-29T23:07:21Z")

</div>

Hi there -  
I’m struggling with a problem that I was hoping someone here could help with.  
I wrote some code - provided below - where the “a” key on the keyboard makes some rectangles and the “b” key makes some circles.  
I’d ilke to have a functionality by which “a” and “b” could be held down at the same time and rectangles and circles would be drawn.  
Does anyone have any suggestions on how to do this?  
Thanks!

```auto
int in = 0;

void setup() { 
  size(400, 400);
  background(100, 200, 0);

}

void draw() { 
  
  if (in == 1)
  {aMove();}
  else if (in == 2)
  {bMove();}
  else {background(0,0, 0);}
  
}

 void aMove(){
   fill(100, 200, 0);
   rect(random(100)+100, random(100)+100, random(100), random(100));
 }
 
 void bMove(){
      fill(0, 200, 200);
      circle(random(100), random(100), random(100));
 }
  
 void keyPressed(){
    if (key == 'a') {in = 1;} 
    else if (key == 'b') {in = 2;}
    else {in = 3;}
 }

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 29, 2022, 11:19pm UTC](https://discourse.processing.org/t/calling-simultaneous-functions-in-draw/39497/2 "2022-10-29T23:19:13Z")

</div>

Hello,

This should help:

> **[User Input in Processing](https://happycoding.io/tutorials/processing/input#handling-multiple-keys)**
>
> Learn how to detect mouse and keyboard input to make interactive programs in Processing.

> **[Logical Operators / Examples](https://processing.org/examples/logicaloperators.html)**
>
> The logical operators for AND (&&) and OR (||) are used to combine simple relational statements into more complex expressions. The NOT (!) operator is used to negate a boolean statement.

[Reference / Processing.org](https://processing.org/reference) \< See _Logical Operators_ in the _Control_ section

`:)`

---

<div class="post-metadata">

**Author:** ![bcabc](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@bcabc](https://discourse.processing.org/u/bcabc)\
**Post date:** [October 30, 2022, 2:21am UTC](https://discourse.processing.org/t/calling-simultaneous-functions-in-draw/39497/3 "2022-10-30T02:21:37Z")

</div>

Hi @glv - thanks for the suggestion.

I followed the first example and that works at allowing the keystroke to turn things on and off - but still not simultaneously (code below)… If I were to add && statements for every possible combination of every key I’m planning to use, it would be an enormous amount of code… Is there something I’m missing?

```auto
boolean[] yawn = new boolean[27];

void setup(){
  size(800, 800);
  for (int x = 0; x<27; x++)
  {yawn[x] = false;}
  
}

void draw(){
  if (yawn[0] == true)
  {aMove();}
  else if (yawn[1] == true)
  {bMove();}
 // else {background(0,0, 0);}
  
}

 void aMove(){
   fill(100, 200, 0);
   rect(random(100)+100, random(100)+100, random(100), random(100));
 }
 
 
 void bMove(){
      fill(0, 200, 200);
      circle(random(100), random(100), random(100));

 }

void keyPressed(){
for (int numLet = 0; numLet<27; numLet++)
  {
   if (key==numLet+97) {yawn[numLet]=true;}
  }
}

void keyReleased(){
for (int numLet = 0; numLet<27; numLet++)
  {
   if (key==numLet+97) {yawn[numLet]=false;}
  }
}

```

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [October 30, 2022, 2:33am UTC](https://discourse.processing.org/t/calling-simultaneous-functions-in-draw/39497/4 "2022-10-30T02:33:35Z")

</div>

```auto
boolean[] keysDown;

void setup() {
  keysDown = new boolean[256];
}

void draw() {
  background(0);
  if( keysDown['a'] ) circle( 50, 50, 10 );
  if( keysDown['b'] ) rect( 10, 10, 10, 10 );
}

void keyPressed() {
  keysDown[key] = true;
}

void keyReleased() {
  keysDown[key] = false;
}

```

---

<div class="post-metadata">

**Author:** ![bcabc](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@bcabc](https://discourse.processing.org/u/bcabc)\
**Post date:** [October 30, 2022, 5:51am UTC](https://discourse.processing.org/t/calling-simultaneous-functions-in-draw/39497/5 "2022-10-30T05:51:39Z")

</div>

Thanks, @scudly - this is a very elegant solution. I’m noticing that it stops working after four or so keys have been depressed, though - do you know if there is a reason for that?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2022, 6:29am UTC](https://discourse.processing.org/t/calling-simultaneous-functions-in-draw/39497/6 "2022-10-30T06:29:02Z")

</div>

How does it stop? With an error message?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 30, 2022, 10:59am UTC](https://discourse.processing.org/t/calling-simultaneous-functions-in-draw/39497/7 "2022-10-30T10:59:47Z")

</div>

Hello @bcabc,

Using this example:  
[https://discourse.processing.org/t/customizeable-keyboard-controls-for-games/22003/2](https://discourse.processing.org/t/customizeable-keyboard-controls-for-games/22003/2)

And adding this to `draw()` :

```auto
void draw() 
  {
  // Continue doing your thing
  background(0);
  String s = "";
  for(int i = 0; i<256; i++)
    {
    if (keys[i] == true) 
      {
      //print(char(i));   
      s += str(char(i));
      }
    }
    textSize(24);
    text(s, 20, 50);
    println(); 
    }
  }

```

I only get a maximum of 6 keys on my keyboard!  
And less with numbers along the top but 6 with number keypad.

This may provide some insight:

> **[Rollover (keyboard)](https://en.wikipedia.org/wiki/Rollover_(keyboard))**
>
> Rollover is the ability of a computer keyboard to correctly handle several simultaneous keystrokes. A keyboard with n-key rollover can correctly detect input from each key on the keyboard at the same time, regardless of how many other keys are also being pressed. Keyboards that lack full rollover will register an incorrect keystroke when certain combinations of keys are pressed simultaneously. Rollover has applications for stenotype, electronic music keyboards, gaming, and touch-typing generally....

`:)`

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [October 30, 2022, 5:30pm UTC](https://discourse.processing.org/t/calling-simultaneous-functions-in-draw/39497/8 "2022-10-30T17:30:24Z")

</div>

As @glv’s link shows, different keyboards will respond to multiple keys differently. And different combinations of keys might work while others don’t.

For instance, on my HP keyboard, I can press ASD4, but not ASD3. Or I can press ASF3, but not ASF4.

Here’s a more thorough key tester that uses `keyCode` instead of `key` that will let you see which key combinations work:

```auto
boolean[] keysDown;
boolean bHigh = false;

void setup() {
  size( 640, 640 );
  keysDown = new boolean[256];
}

void draw() {
  background(bHigh ? 128 : 0);
  noStroke();
  fill( 255 );
  for( int j=0; j<16; j++ ) {
    for( int i=0; i<16; i++ ) {
      if( keysDown[j*16+i] )
        text( str(j*16+i), i*40, j*40 );
    }
  }
}

void keyPressed() {
  if( keyCode < 256 ) 
    keysDown[keyCode] = true;
  else bHigh = true;
}

void keyReleased() {
  if( keyCode < 256 ) 
    keysDown[keyCode] = false;
  else bHigh = false;
}

```
