From 9ce2e3f873dd657a0e70b654f2e9681a2fcda151 Mon Sep 17 00:00:00 2001 From: Marcel Valdez Date: Fri, 26 Mar 2021 10:10:58 -0700 Subject: [PATCH] Fix issue #397: Event listener fails when keyboard is disconnected 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. --- keyboard/_nixcommon.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/keyboard/_nixcommon.py b/keyboard/_nixcommon.py index a4d0d06f..7110bb37 100644 --- a/keyboard/_nixcommon.py +++ b/keyboard/_nixcommon.py @@ -103,7 +103,8 @@ def __init__(self, devices, output=None): self.output = output or self.devices[0] def start_reading(device): while True: - self.event_queue.put(device.read_event()) + if device.input_file.readable(): + self.event_queue.put(device.read_event()) for device in self.devices: thread = Thread(target=start_reading, args=[device]) thread.setDaemon(True)