# Keyboard buttons

**URL:** <https://discourse.processing.org/t/keyboard-buttons/717>\
**Category:** Coding Questions\
**Created:** [June 5, 2018, 3:20am UTC](https://discourse.processing.org/t/keyboard-buttons/717 "2018-06-05T03:20:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nethaniel](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@nethaniel](https://discourse.processing.org/u/nethaniel)\
**Post date:** [June 5, 2018, 3:20am UTC](https://discourse.processing.org/t/keyboard-buttons/717/1 "2018-06-05T03:20:46Z")

</div>

i am trying to make a program that has a two shapes that move around with keyboard buttons i am somewhat new to coding and do not know how to make the keyboard recognize two different keys at the same time this is what i currently have:

```auto
float R = 500;
float U = 500;
float r;
float u;
void setup(){
 
  size(1000,1000);
  
}
void draw(){
  
  
  R=constrain(R,0,width-30);

   U=constrain(U,0,height-30);
 move();
 
  rect(R,U,30,30);
  ellipse(r,u,30,30);
  
}
void move(){
  if(keyPressed){
    if(keyCode == LEFT+RIGHT){
      R=R+5;
      U=U+5;
    }
   if (keyCode == LEFT) {
    R=R-5;
  }
  if(keyCode == RIGHT) {
    R=R+5;
  }
  if(keyCode == UP) {
    U=U-5;
  }
  if(keyCode == DOWN) {
    U=U+5;
  }
    if (key == 'w') {
    u=u-5;
  }
  if(key == 'a') {
    r=r-5;
  }
  if(key == 's') {
    u=u+5;
  }
  if(key == 'd') {
    r=r+5;
  }
  }
}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [June 5, 2018, 4:26am UTC](https://discourse.processing.org/t/keyboard-buttons/717/2 "2018-06-05T04:26:51Z")

</div>

I recently wrote some code that deals with tracking which keys are down in a neat way:

> [@Mario game help! Move background screen when image is moved?](https://discourse.processing.org/t/mario-game-help-move-background-screen-when-image-is-moved/659/4):
>
> No! You can certainly have a background image that is larger than your sketch! I took a bit of time to code you a working example: float background\_x\_offset; float mario\_x\_position; float mario\_y\_position; PImage bg; void setup(){ size(200,200); spoofBackgroundImage(); background\_x\_offset = 0; mario\_x\_position = width/2; } void draw(){ background(0); if( kd[0] ) mario\_x\_position--; if( kd[1] ) mario\_x\_position++; if( mario\_x\_position \> 3\*width/4 && background\_x\_offset \< 600 ){ …

Here’s the code that counts:

```auto
void keyPressed(){
  k(true);
}

void keyReleased(){
  k(false);
}
 
int[] ks = {LEFT, RIGHT};
boolean[] kd = {false,false};
 
void k(boolean b){
 for( int i = 0; i < ks.length; i++){
   if( ks[i] == keyCode ) kd[i] = b;
 }
}

```

Notice that this defines two arrays. `ks` is the keys that you wish to track. `kd` is a boolean array that records if the key in `ks` at the same index is held down.

You can use this in your conditional checks:

```
if( kd[0] && kd[1] ){ // If LEFT arrow and RIGHT arrow are held down at the same time.

```

Notice that this is using the logical AND operator, `&&`, not the addition operator `+`!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 6, 2018, 11:45pm UTC](https://discourse.processing.org/t/keyboard-buttons/717/3 "2018-06-06T23:45:10Z")

</div>

If you are doing more than a few keys, another general option is to use implement @TfGuy44’s boolean approach using a [HashMap](https://processing.org/reference/HashMap.html).

```auto
import java.util.Map;
HashMap<Character,Boolean> flags = new HashMap<Character,Boolean>();

```

Any key that gets pressed, put it in the map with a `true`… any key that gets released, take it out with a `false.`

```auto
void keyPressed(){
  flags.put(key, Boolean.TRUE);
}
void keyReleased(){
  flags.put(key, Boolean.FALSE);
}

```

This can be used for simultaneous keypresses – or for a large collection of toggles:

```auto
void keyReleased(){
  if(flags.get(key)!=null){
    flags.put(key, !(flags.get(key)));
  } else {
    flags.put(key, Boolean.TRUE);
  }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 6, 2018, 11:49pm UTC](https://discourse.processing.org/t/keyboard-buttons/717/4 "2018-06-06T23:49:32Z")

</div>

[http://Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G)

---

<div class="post-metadata">

**Author:** ![lmccart](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lmccart/32/12_2.png) [@lmccart](https://discourse.processing.org/u/lmccart)\
**Post date:** [June 7, 2018, 7:34pm UTC](https://discourse.processing.org/t/keyboard-buttons/717/5 "2018-06-07T19:34:43Z")

</div>

Thanks @GoToLoop. Please post some text description when you post links.
