can multiple keys be pressed on the keyboard?

Hello.

Is there a way to scan for multiple keys being pressed at the same time? I would like to be able to move UP + RIGHT, not only in the last direction pressed. Also, it is evident there is a small "gap" after the initial press of the key and the continuation, as there is when we are typing.

thank you all.

float x=width/2;
float y=height/2;
float xvel=0;
float yvel=0;
float frict = 0.97;
float vel = 0.6;

void setup() {
  size(800, 800);
  //fullScreen();
  background(0);
  frameRate(60);

  x=width/2;
  y=height/2;
}

void draw() {

  background(0);//cls
  x=x+xvel; 
  y=y+yvel;
  yvel=yvel*frict;
  xvel=xvel*frict;

  rect(x, y, 50, 50);
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
      yvel=yvel-vel;
    } else if (keyCode == DOWN) {
      yvel=yvel+vel;
    } 
    if (keyCode == LEFT) {
      xvel=xvel-vel;
    } else if (keyCode == RIGHT) {
      xvel=xvel+vel;
    }
  }
}


Answers

Sign In or Register to comment.