boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.74k stars 432 forks source link

An OSError when the device is unplugged on RaspberrypiOS #639

Open H3LL0U opened 4 months ago

H3LL0U commented 4 months ago

When the device is unplugged during the execution of the following code:

import keyboard

if __name__ == "__main__":
    keyboard.wait('esc')

The following error acures which breaks the program:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
Exception in thread Thread-4:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
  File "/usr/local/lib/python3.9/dist-packages/keyboard/_nixcommon.py", line 106, in start_reading
    self.event_queue.put(device.read_event())
  File "/usr/local/lib/python3.9/dist-packages/keyboard/_nixcommon.py", line 83, in read_event
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/dist-packages/keyboard/_nixcommon.py", line 106, in start_reading
    self.event_queue.put(device.read_event())
  File "/usr/local/lib/python3.9/dist-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
    data = self.input_file.read(struct.calcsize(event_bin_format))
OSError: [Errno 19] No such device
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/dist-packages/keyboard/_nixcommon.py", line 106, in start_reading
    self.event_queue.put(device.read_event())
  File "/usr/local/lib/python3.9/dist-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

Note: I'm starting the Script through SSH with sudo python3 Log.py and the Script executes normally when the keyboard is innitially plugged in

H3LL0U commented 4 months ago

https://github.com/boppreh/keyboard/issues/531 Similar Issue

H3LL0U commented 4 months ago

Partial solution found. Now the keyboard doesn't raise an OSError when disconected. However upon disconecting it reinitializes everything which automatically sets a fake keyboard. The system should be implomented which checks if there are any changes in the usb devices 5d518fa4fe4083475e913d580ccf1b8126c4d72e (Will work on it later)