keyboard
keyboard copied to clipboard
Ubuntu Linux: Listening to on_release events getting OSError: [Errno 19] No such device
Thanks for this library!
I'm using it to listen to every on_release
event:
keyboard.on_release(callback=self.on_key_up)
But sadly after a while I always get this exception:
OSError: [Errno 19] No such device
After that, the callback isn't called anymore.
Edit: Actually, sometimes it happens right away..
I am getting this error after 1-2 hour or so of listening on a keyboard event.
Exception in thread Thread-18: Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/usr/lib/python3.8/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/home/marcel/.local/lib/python3.8/site-packages/keyboard/_nixcommon.py", line 106, in start_reading self.event_queue.put(device.read_event()) File "/home/marcel/.local/lib/python3.8/site-packages/keyboard/_nixcommon.py", line 83, in read_event data = self.input_file.read(struct.calcsize(event_bin_format)) OSError: [Errno 19] No such device
@marcel-valdez I think that's what I'm getting as well. Thanks for posting the full traceback.
I figured out when/why this happens. If your keyboard disconnects even for a fraction of a second the event polling code thinks the device no longer exists and when it tries to fetch the event, it throws exception and fails. My keyboard has a faulty cable which at times loses the connection for an unnoticeable amount of time, but the code manages to attempt to poll my (disconnected) keyboard for that moment and throws exception.
To test, do the following:
- Hook any keyboard event with a callback
- Disconnect your keyboard
- keyboard thread will throw exception.
- Reconnect your keyboard
- Events are no longer listened to.
Figured out a fix for the issue. Will post pull request.
@marcel-valdez Awesome that you figured it out! Yeah, I think my keyboard might also have an issue. (Or maybe it's something related as the disconnect seems to happen more often when the screen is locked.)
Figured out a fix for the issue. Will post pull request. @marcel-valdez Does that mean the keyboard loss will not make exceptions of no such device? I still have this issue. using keyboard 0.13.5. some one know which version fix this issue?
update: thanks so much for your code. but after adding the 'if device.input_file.readable():', it still shows 'no such device' after disconnecting the keyboard, and not listen after i reconnect keyboard. I tried use add_hotkey and is_pressed, born not work after reconnect
hi! im having the same problem, i forked the repo and download the code in order to debug it but if the connection is lost the readable() method is still returning True, so the error hangs the script...
is there any workaround or way to raise the error to the main thread and stopped all in order to catch the exception and relaunch or something, now even if a wrap the code into a try/except the exception is printed but not raised and the program is hanged
thx!
https://github.com/boppreh/keyboard/pull/642 should fix the issue