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

The keyboard devices are now getting automatically recognized and don't raise OSError when disconected #642

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions keyboard/_nixcommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import atexit
from time import time as now
from time import sleep
from threading import Thread
from glob import glob
try:
Expand Down Expand Up @@ -101,20 +102,46 @@ def __init__(self, devices, output=None):
self.event_queue = Queue()
self.devices = devices
self.output = output or self.devices[0]
def start_reading(device):
while True:
self.event_queue.put(device.read_event())
for device in self.devices:
thread = Thread(target=start_reading, args=[device])
thread.daemon = True
thread.start()

for device in self.devices:
self.create_device_thread(device)
thread = Thread(target=self.check_for_new_devices)
thread.daemon = True
thread.start()

def check_for_new_devices(self):
while True:
sleep(2)
devices_paths = [device.path for device in self.devices]
H3LL0U marked this conversation as resolved.
Show resolved Hide resolved

new_devices = [device.path for device in list_devices_from_proc("kbd")]

for new_device in new_devices:
if not(new_device in devices_paths):

new_device_to_append = EventDevice(new_device)
self.devices.append(new_device_to_append)
self.create_device_thread(new_device_to_append)
def read_event(self):
return self.event_queue.get(block=True)

def write_event(self, type, code, value):
self.output.write_event(type, code, value)

def start_reading(self,device):
while True:
try:
self.event_queue.put(device.read_event())
except OSError:
if device in self.devices:
self.devices.remove(device)

break

def create_device_thread(self, device):
thread = Thread(target=self.start_reading, args=[device])
thread.daemon = True
thread.start()
import re
from collections import namedtuple
DeviceDescription = namedtuple('DeviceDescription', 'event_file is_mouse is_keyboard')
Expand Down
9 changes: 8 additions & 1 deletion keyboard/_nixkeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,18 @@ def init():
pressed_modifiers = set()

def listen(callback):
global device
build_device()
build_tables()

while True:
time, type, code, value, device_id = device.read_event()
try:
time, type, code, value, device_id = device.read_event()
except OSError:

device = None
init()
continue
if type != EV_KEY:
continue

Expand Down