Calling simultaneous functions in draw()

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!

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;}
 }

Hello,

This should help:

Reference / Processing.org < See Logical Operators in the Control section

:)

1 Like

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?

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;}
  }
}
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;
}
2 Likes

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?

1 Like

How does it stop? With an error message?

Hello @bcabc,

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

And adding this to draw() :

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:

:)

2 Likes

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:

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;
}
1 Like