# Why does glfwGetMouseButton() miss button presses

**URL:** <https://discourse.glfw.org/t/why-does-glfwgetmousebutton-miss-button-presses/1402>\
**Category:** support\
**Created:** [August 12, 2019, 10:29am UTC](https://discourse.glfw.org/t/why-does-glfwgetmousebutton-miss-button-presses/1402 "2019-08-12T10:29:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackywhite](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/jackywhite/32/567_2.png) [@jackywhite](https://discourse.glfw.org/u/jackywhite)\
**Post date:** [August 12, 2019, 10:29am UTC](https://discourse.glfw.org/t/why-does-glfwgetmousebutton-miss-button-presses/1402/1 "2019-08-12T10:29:29Z")

</div>

I poll events with GLFW 60 times per second. Which as I understand means that `glfwGetMouseButton()` should detect any mouse button press with the duration over ca 0.016 seconds. If I press and release a button quicker than that it will get the `GLFW_RELEASE` state. I don’t think it is physically possible to press and release a mouse button quicker than that. So how come most of my mouse clicks are missed when I use this function?

Of course using callback for this purpose works. But could someone explain to me why it works if I do this:

```
bool clickedLeft, clickedRight;
void MyClickCallback(GLFWwindow* window, int button, int action, int mods) {
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == 1) {
        clickedLeft = (clickedLeft) ? false : true;
    }

    else if (button == GLFW_MOUSE_BUTTON_RIGHT && action == 1) {
        clickedRight = (clickedRight) ? false : true;
    }
}

void UpdateFrame() {
    if (clickedLeft)
        cout << "Clicked Left" << endl;
    if (clickedRight)
        cout << "Clicked Right" << endl;
}

```

In this example the boolean `clickedLeft` may be updated multiple times per frame if I click and release a button faster than the duration of a frame. But on frame update we get the final state of the `clickedLeft`. So how come glfw detects the button press correctly with this approach when it does not with `glfwGetMouseButton()`?

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [August 12, 2019, 5:53pm UTC](https://discourse.glfw.org/t/why-does-glfwgetmousebutton-miss-button-presses/1402/2 "2019-08-12T17:53:04Z")

</div>

As you note `glfwGetMouseButton()` can miss events whereas a callback won’t.

Your code looks like it will toggle the state of your bool variables every time a button is pressed. I would instead use something like:

```auto
bool clickedLeft = false;
bool clickedRight = false;
void MyClickCallback(GLFWwindow* window, int button, int action, int mods) {
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == 1) {
        clickedLeft = true;
    }

    else if (button == GLFW_MOUSE_BUTTON_RIGHT && action == 1) {
        clickedRight = true;
    }
}

void UpdateFrame() {
    if (clickedLeft)
        cout << "Clicked Left" << endl;
    if (clickedRight)
        cout << "Clicked Right" << endl;

    // reset every frame
    clickedLeft = false;
    clickedRight = false;
}

```
