Moving two Objects at the same time with different keys

I´m working on a game in wich i have two balls. Both Balls should be movable at the same time, the first one with w,a,s,d and the second one with the arrow keys. My problem is that i can always only move one object. Are there some fundamental steps i missed out? please help me!

              // Ball 1 movement
void draw(){
  if(keyPressed){

    if (key == 'w' ){

      ballY1 = ballY1 - 3;
     
    }

    if (key == 's' ){

    ballY1 =ballY1 + 3;     
       
    }

    if (key == 'a'){

      ballX1 = ballX1 - 3;     
       
    }

    if (key == 'd' ){

      ballX1 = ballX1 + 3;
      
    }

               // Ball 2 movement

    if(keyPressed){

    if (keyCode == UP  ){

      ballY2 = ballY2 -3;
      
      
    }
    if (keyCode == DOWN  ){

      ballY2 = ballY2 + 3;     
       
    }

   if (keyCode == LEFT){

      ballX2 = ballX2 - 3;      
       
    }

    if (keyCode == RIGHT){

       ballX2 = ballX2 + 3;         
      
    }
}

This is not my whole code!!! I left out all the other code like drawing the ellipses or the variables because it might be distracting and i only need help with this specific thing.

Thanks for your fast reply but is there any simpler solution because i dont really understand the code :relieved:im sorry i´m learning processing in school right now

You can ask where you don’t get it. :wink:

The main parts of my sketch is the keyPressed() & keyReleased() callbacks: :nerd_face:


And some container to store which keyCode values are currently active: :keyboard:
final byte[] keysDown = new byte[KEYS_RANGE];