Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
CreamyCookie opened this issue Oct 16, 2020 · 8 comments

Comments

@CreamyCookie
Copy link

CreamyCookie commented Oct 16, 2020

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

@marcel-valdez
Copy link

marcel-valdez commented Mar 25, 2021

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

@CreamyCookie
Copy link
Author

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

@marcel-valdez
Copy link

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
Copy link

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

@CreamyCookie
Copy link
Author

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

marcel-valdez added a commit to marcel-valdez/keyboard that referenced this issue Mar 26, 2021
…cted momentarily.

I encountered the issue in a *nix machine (Ubuntu 16.04).
The fix makes it so that we don't ready the input file when a keyboard is
disconnected by checking the 'readable()' method on the file pointer, in Ubuntu
when the keyboard is re-connected the 'readable()' method returns True and
everything works as usual.
QuentiumYT pushed a commit to Quentium-Forks/keyboard that referenced this issue Mar 4, 2022
…cted momentarily.

I encountered the issue in a *nix machine (Ubuntu 16.04).
The fix makes it so that we don't ready the input file when a keyboard is
disconnected by checking the 'readable()' method on the file pointer, in Ubuntu
when the keyboard is re-connected the 'readable()' method returns True and
everything works as usual.
@tom9672
Copy link

tom9672 commented May 4, 2022

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

@adri9valle
Copy link

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!

QuentiumYT pushed a commit to Quentium-Forks/keyboard that referenced this issue Jan 29, 2024
…cted momentarily.

I encountered the issue in a *nix machine (Ubuntu 16.04).
The fix makes it so that we don't ready the input file when a keyboard is
disconnected by checking the 'readable()' method on the file pointer, in Ubuntu
when the keyboard is re-connected the 'readable()' method returns True and
everything works as usual.
H3LL0U added a commit to H3LL0U/keyboard that referenced this issue Mar 14, 2024
@H3LL0U
Copy link

H3LL0U commented Mar 14, 2024

#642 should fix the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants