# GLFW Input Debouncing?

**URL:** <https://discourse.glfw.org/t/glfw-input-debouncing/776>\
**Category:** support\
**Created:** [October 5, 2016, 6:50pm UTC](https://discourse.glfw.org/t/glfw-input-debouncing/776 "2016-10-05T18:50:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![huntnb](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/huntnb/32/67_2.png) [@huntnb](https://discourse.glfw.org/u/huntnb)\
**Post date:** [October 5, 2016, 6:50pm UTC](https://discourse.glfw.org/t/glfw-input-debouncing/776/1 "2016-10-05T18:50:04Z")

</div>

Hello,

I’m using GLFW’s key callback to control a camera in a simple (OpenGL) 3D world. I’ve registered a callback function via `glfwSetKeyCallback` that maintains a global array of booleans: an entry is set when a `GLFW_PRESS` event occurs and is cleared when the `GLFW_RELEASE` event occurs.

```
void glfw_key_callback(GLFWwindow *window, int key, int scan, int action, int mod)
{
	...
	if (action == GLFW_PRESS) {
		keyMap[key] = 1;
	} else if (action == GLFW_RELEASE) {
		keyMap[key] = 0;
	}
}

```

This keymap is then queried in the main loop by the camera handling code to decide how to move the camera:

```
if (keyMap[GLFW_KEY_UP]) {
	// Then move the camera forward...
}

```

This all works as expected, modulo one slightly annoying bug: after a key is released, **there is a spurious `GLFW_PRESS`/`GLFW_RELEASE` event pair, causing a minor stutter in camera’s motion**. I’ve read the Input guide and the API docs, but don’t see anything that would explain this behavior.

I’ve included a link to a small program that demonstrates this problem at the bottom of this post. When running it, I would expect to see a single PRESS/RELEASE pair when I push and release the UP key, but instead see _two_ different PRESS/RELEASE combos:

```
0
PRESS <--- me pushing GLFW_KEY_UP
1
1
...
1
RELEASE <--- me releasing GLFW_KEY_UP
0
0
PRESS <--- spurious press
1
1
1
RELEASE <--- spurious release

```

Is this expected behavior for GLFW? Is there a best practice to eliminate this sort of input “bouncing”? In case it’s relevant, I’m running Ubuntu 16.04, with its default Unity shell.

**Link to demo program** : [https://gist.github.com/anonymous/fa27ad8ce5406445c3797544d6f1b7ce](https://gist.github.com/anonymous/fa27ad8ce5406445c3797544d6f1b7ce)

Thanks in advance!  
-Nick

---

<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:** [October 5, 2016, 8:57pm UTC](https://discourse.glfw.org/t/glfw-input-debouncing/776/2 "2016-10-05T20:57:23Z")

</div>

> [@huntnb](#):
>
> I’ve registered a callback function via glfwSetKeyCallback that maintains a global array of booleans: an entry is set when a GLFW\_PRESS event occurs and is cleared when the GLFW\_RELEASE event occurs.

GLFW already does this for you, per window. See [glfwGetKey](http://www.glfw.org/docs/latest/group__input.html#gadd341da06bc8d418b4dc3a3518af9ad2).[quote=“huntnb, post:1, topic:776”]  
This all works as expected, modulo one slightly annoying bug: after a key is released, there is a spurious GLFW\_PRESS/GLFW\_RELEASE event pair, causing a minor stutter in camera’s motion.  
[/quote]

This sounds like bug #747.

> <https://github.com/glfw/glfw/issues/747>
>
> I just implemented camera movements and noticed that the camera moved forward fo…r a bit after releasing W. After some investigation I noticed that glfwSetKeyCallback triggered another shorter sequence of GLFW\_PRESS, GLFW\_REPEAT and GLFW\_RELEASE after I've release the key. The longer I hold the key the longer the second sequence is.
> 
> To make sure it wasn't something in my code I downloaded the getting started example and added a simple \`printf("%i ", action);\`
> 
> The output looked like this:
> 
> \`1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 1 2 2 2 2 0\`
> 
> Where the first 0 is when I actually released the key and the rest of the sequence are phantom key events.
> 
> I'm running GLFW 3.1.2 using the offical package on ArchLinux x64

---

<div class="post-metadata">

**Author:** ![huntnb](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/huntnb/32/67_2.png) [@huntnb](https://discourse.glfw.org/u/huntnb)\
**Post date:** [October 5, 2016, 9:07pm UTC](https://discourse.glfw.org/t/glfw-input-debouncing/776/3 "2016-10-05T21:07:36Z")

</div>

That does indeed look like the same bug. Thanks, elmindreda! Any future followups will go to linked issue.

-Nick
