keyboard icon indicating copy to clipboard operation
keyboard copied to clipboard

Ubuntu Linux: Listening to on_release events getting OSError: [Errno 19] No such device

Open CreamyCookie opened this issue 4 years ago • 8 comments

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..

CreamyCookie avatar Oct 16 '20 11:10 CreamyCookie

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 avatar Mar 25 '21 04:03 marcel-valdez

@marcel-valdez I think that's what I'm getting as well. Thanks for posting the full traceback.

CreamyCookie avatar Mar 25 '21 10:03 CreamyCookie

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:

  1. Hook any keyboard event with a callback
  2. Disconnect your keyboard
  3. keyboard thread will throw exception.
  4. Reconnect your keyboard
  5. Events are no longer listened to.

marcel-valdez avatar Mar 26 '21 16:03 marcel-valdez

Figured out a fix for the issue. Will post pull request.

marcel-valdez avatar Mar 26 '21 17:03 marcel-valdez

@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.)

CreamyCookie avatar Mar 26 '21 17:03 CreamyCookie

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

tom9672 avatar May 04 '22 04:05 tom9672

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!

adri9valle avatar Oct 17 '23 12:10 adri9valle

https://github.com/boppreh/keyboard/pull/642 should fix the issue

H3LL0U avatar Mar 14 '24 18:03 H3LL0U