Skip to content
Advertisement

Reading and debouncing multiple controller keys with evdev

I have an 8bitdo NES controller that I have hooked up to a Raspberry Pi and am using it to interact with some various demos I’m working on.

I’m trying to figure out a good way to grab multiple keypresses with evdev while debouncing so that a single keypress doesn’t trigger twice. I could set a flag and check the next loop, but thought this might be taken care of with the library.

I’m using the active_keys function to get a list of all keypresses as using the for event in device.read_loop(): call appears to be blocking and only grabs a single key per loop.

This is where I’m currently at:

JavaScript

As I mentioned, I could set a flag per key to handle a debounce which would get quickly redundant and thought there was a more elegant way to handle this. I didn’t notice it in the API documentation though.

Advertisement

Answer

I can see in the evdev source that there is a debounce class, but it doesn’t appear to be accessible from the Python interface.

A flag per key with a pause and a re-read is a simple and widely used method you appear to be familiar with. You can do it somewhat elegantly with Python’s set intersection:

JavaScript

A more sophisticated method could be to use an integrator.

Alternatively, you could also try using events or async events if you really want it to be fast. These may be automatically debounced, but I’m not sure:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement