# GLFW\_KEY\_UP and GLFW\_KEY\_RIGHT not working

**URL:** <https://discourse.glfw.org/t/glfw-key-up-and-glfw-key-right-not-working/1209>\
**Category:** support\
**Created:** [November 5, 2018, 1:18pm UTC](https://discourse.glfw.org/t/glfw-key-up-and-glfw-key-right-not-working/1209 "2018-11-05T13:18:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![welton](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@welton](https://discourse.glfw.org/u/welton)\
**Post date:** [November 5, 2018, 1:18pm UTC](https://discourse.glfw.org/t/glfw-key-up-and-glfw-key-right-not-working/1209/1 "2018-11-05T13:18:51Z")

</div>

Hello, I recently implemented this code:

`void GLFWCALL handleKeypress (int key, int press) {
   switch (key) {
      case GLFW_KEY_ESC:
         exit(1);
      case GLFW_KEY_RIGHT:
         rotate_x+=5;
      case GLFW_KEY_LEFT:
         rotate_x-=5;
      case GLFW_KEY_UP:
         rotate_y+=5;
      case GLFW_KEY_DOWN:
         rotate_y-=5;
    }
}`

where `rotate_x` and `rotate_y` are both global variables as shown here:

```auto
float rotate_x=0,rotate_y=0;
```

The routine above is used to change these two lines of code `glRotatef (rotate_x, 1.0, 0.0, 0.0);
   glRotatef (rotate_y, 0.0, 1.0, 0.0);`

When I press the left or the down keys, everything works as expected, but pressing the right or the up keys does absolutely nothing.

However if I comment out:

`//case GLFW_KEY_LEFT:
         //rotate_x-=5;` and

`case GLFW_KEY_DOWN:
         rotate_y-=5;
` and runs the program again, both start to working!. Pressing the up key produces the expected result, however pressing the right key does not, it seems to be rotating my object over two axis simultaneously.

So, what I am doing wrong? Thanks for the help.

---

<div class="post-metadata">

**Author:** ![elmindreda](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/elmindreda/32/26_2.png) [@elmindreda](https://discourse.glfw.org/u/elmindreda)\
**Post date:** [November 5, 2018, 2:37pm UTC](https://discourse.glfw.org/t/glfw-key-up-and-glfw-key-right-not-working/1209/2 "2018-11-05T14:37:34Z")

</div>

You need to add `break` in C if you don’t want a [switch statement](https://en.cppreference.com/w/c/language/switch) to continue executing every line after the selected `case` label.

---

<div class="post-metadata">

**Author:** ![tombsar](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@tombsar](https://discourse.glfw.org/u/tombsar)\
**Post date:** [November 5, 2018, 2:53pm UTC](https://discourse.glfw.org/t/glfw-key-up-and-glfw-key-right-not-working/1209/3 "2018-11-05T14:53:12Z")

</div>

One of the great footguns of C and C++ that I still occasionally fall victim to.

I tend to use the following style to mitigate the problem:

```auto
switch (key) {
    case GLFW_KEY_ESC: {
        exit(1);
    } break;

    case GLFW_KEY_RIGHT: {
        rotate_x+=5;
    } break;

    case GLFW_KEY_LEFT: {
        rotate_x-=5;
    } break;

    case GLFW_KEY_UP: {
        rotate_y+=5;
    } break;

    case GLFW_KEY_DOWN: {
        rotate_y-=5;
    } break;

    default: {
        // Do nothing
    }
}

```

but for some reason nobody seems to agree with me…

---

<div class="post-metadata">

**Author:** ![elmindreda](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/elmindreda/32/26_2.png) [@elmindreda](https://discourse.glfw.org/u/elmindreda)\
**Post date:** [November 5, 2018, 2:56pm UTC](https://discourse.glfw.org/t/glfw-key-up-and-glfw-key-right-not-working/1209/4 "2018-11-05T14:56:02Z")

</div>

Not sure I’ll use it but it’s very legible. I like it and wouldn’t mind working in code written that way.

---

<div class="post-metadata">

**Author:** ![welton](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@welton](https://discourse.glfw.org/u/welton)\
**Post date:** [November 5, 2018, 7:18pm UTC](https://discourse.glfw.org/t/glfw-key-up-and-glfw-key-right-not-working/1209/5 "2018-11-05T19:18:34Z")

</div>

Well, when I don’t forget, I tend to use it but without the {}
