Trovo device dinamicamente

master
Pietro Brenna 2019-10-01 14:56:30 +02:00
parent 78e0426f7c
commit dc60a62063
1 changed files with 23 additions and 11 deletions

View File

@ -4,10 +4,16 @@ Created on 1 ott 2019
@author: Emanuele Trabattoni @author: Emanuele Trabattoni
''' '''
from evdev import InputDevice, ecodes, categorize from evdev import InputDevice, ecodes, categorize, list_devices
from select import select from select import select
dev = InputDevice('/dev/input/by-id/usb-0581_0106-event-kbd') devices = [InputDevice(path) for path in list_devices()]
d = filter(lambda x: x.info.vendor == 1409
and x.info.product == 262
and ('KEY_1', 2) in x.capabilities(verbose=True)[('EV_KEY', 1)], devices)
lista = list(d)
dev = lista[0]
#dev = InputDevice('/dev/input/by-id/usb-0581_0106-event-kbd')
#dev = InputDevice('/dev/input/by-id/usb-Chicony_USB_Keyboard-event-kbd') #dev = InputDevice('/dev/input/by-id/usb-Chicony_USB_Keyboard-event-kbd')
dev.grab() dev.grab()
@ -24,6 +30,8 @@ char_map = {
458791: 0, 458791: 0,
458792: "Invio" 458792: "Invio"
} }
def get_numero(dev): def get_numero(dev):
stringa = "" stringa = ""
last = None last = None
@ -32,14 +40,18 @@ def get_numero(dev):
for event in dev.read(): for event in dev.read():
if event.code == 4 and event.type == 4: if event.code == 4 and event.type == 4:
if last is None: if last is None:
try:
last = char_map[event.value] last = char_map[event.value]
if last != "Invio": if last != "Invio":
stringa += str(last) stringa += str(last)
else: else:
yield stringa yield stringa
stringa = "" stringa = ""
except KeyError:
print("Carattere sconosciuto")
else: else:
last = None last = None
for x in get_numero(dev): for x in get_numero(dev):
print(x[0:12]) print("ricevo", x[0:12])