# Problem with multiple inputs

**URL:** <https://discourse.processing.org/t/problem-with-multiple-inputs/25066>\
**Category:** Coding Questions\
**Created:** [October 31, 2020, 12:37am UTC](https://discourse.processing.org/t/problem-with-multiple-inputs/25066 "2020-10-31T00:37:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lepetoc](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@lepetoc](https://discourse.processing.org/u/lepetoc)\
**Post date:** [October 31, 2020, 12:37am UTC](https://discourse.processing.org/t/problem-with-multiple-inputs/25066/1 "2020-10-31T00:37:29Z")

</div>

Hello,  
i’m actually trying to code a litlle, i’m at the beginning and i’m trying to do the character movement, i searched some codes to handle multiple inputs, but i have a problem that i cannot understand :

```auto
void keyPressed()
{
  setMove(keyCode, true);
}

void keyReleased()
{
  setMove(keyCode, false);
}

boolean setMove(int k, boolean b)
{
  switch(k) {
    case UP: 
      return isUp = b;
    case DOWN: 
      return isDown = b;
    case LEFT: 
      return isLeft = b;
    case RIGHT: 
      return isRight = b; 
    default:            
      return b;
    }
}

void input() 
{
  if(isUp) {
    test = -1;
    vertical = test;
  }
  else {
    test = 0;
    vertical = test;
  }
  
  if(isDown) {
    vertical = 1;
  }
  else {
    vertical = 0;
  }
  
  if(isLeft) {
    horizontal = -1;
  }
  else {
    horizontal = 0;
  }
  
  if(isRight) {
    horizontal = 1;
  }
  else {
    horizontal = 0;
  }
}

```

I hope the code isn’t too long, the problem is that it doesn’t move up and left, but it perfectly move down and right.  
I tried to remove the down and right condition and it worked perfectly for up and left.  
I don’t understand why the vertical variable doesn’t change when there are all conditions together.

Sorry if you don’t understand my problem, my english is not very good.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2020, 2:55am UTC](https://discourse.processing.org/t/problem-with-multiple-inputs/25066/2 "2020-10-31T02:55:02Z")

</div>

In switch case you need a break; after each section

---

<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:** [October 31, 2020, 9:52am UTC](https://discourse.processing.org/t/problem-with-multiple-inputs/25066/3 "2020-10-31T09:52:02Z")

</div>

> [@lepetoc](#):
>
> I don’t understand why the _vertical_ variable doesn’t change when there are all conditions together.

You need to be aware that _isUp_ needs to antagonize w/ _isDown_.

Same for _isLeft_ needs to exclude _isRight_ and vice-versa.

You can do that more succinctly by subtracting _isDown_ w/ _isUp_:  
`vertical = int(isDown) - int(isUp);`

Same logic for _isRight_ & _isLeft_:  
`horizontal = int(isRight) - int(isLeft);`

Here’s an online sketch using that subtraction approach for movement inside method Player::**move()**:

https://www.openprocessing.org/sketch/718355/embed/

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2020, 4:06pm UTC](https://discourse.processing.org/t/problem-with-multiple-inputs/25066/4 "2020-10-31T16:06:18Z")

</div>

Oh, my comment was wrong

Gosh.
