Two keys pressed & three keys pressed simultaneously

Hello there,

I'm triggering animations that should change based on different keys pressed. I'm having no issues triggering events based on single key presses. I'm however not able to make something happen if two keys are pressed simultaneously.

I want to do something like this:

  void keyPressed() {
    if(key == 'a' & key == 'b') {
      // do stuff
    }
  }

This is obviously wrong. I've been searching for how to do this on the forum and found some information about how to parse the raw data coming in over serial. Can anyone guide me on how to do this?

Also, I'm testing my program with the built-in keyboard on my Mac but will deploy this using a custom Teensy USB external keyboard with Arcade Buttons attached. Will the code work interchangeably for either keyboard?

Answers

  • edited May 2017 Answer ✓

    You can do **this : **

        boolean[] keys;
    
        void setup()
        {
          size(200, 200);
          background(255);
          keys=new boolean[2];
          keys[0]=false;
          keys[1]=false;
          fill(0);
        }
        void draw() 
        {
          if ( keys[0]) 
          {  
            text("1", 50, 50);
          }
          if ( keys[1]) 
          {
            text("2", 100, 100);
          }
          if(keys[0] && keys[1]){
            text("3", 150,150);
          }
        }
    
        void keyPressed()
        {
          if (key=='a')
            keys[0]=true;
          if (key=='b')
            keys[1]=true;
        }
        void keyReleased()
        {
          if (key=='a')
             keys[0]=false;
          if (key=='s')
             keys[1]=false;
       } 
    

    Or read ** this **

  • Answer ✓

    beware that some keyboards won't register multiple keys beyond a certain number - people have had trouble doing two-player, shared-keyboard games in the past.

  • Thank you!

Sign In or Register to comment.