From dc60a6206303f91f48845894578dc460e043f47f Mon Sep 17 00:00:00 2001 From: Pietro Brenna Date: Tue, 1 Oct 2019 14:56:30 +0200 Subject: [PATCH] Trovo device dinamicamente --- libbeepz_test/test.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/libbeepz_test/test.py b/libbeepz_test/test.py index ff1b803..6da8898 100644 --- a/libbeepz_test/test.py +++ b/libbeepz_test/test.py @@ -4,10 +4,16 @@ Created on 1 ott 2019 @author: Emanuele Trabattoni ''' -from evdev import InputDevice, ecodes, categorize +from evdev import InputDevice, ecodes, categorize, list_devices 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.grab() @@ -24,22 +30,28 @@ char_map = { 458791: 0, 458792: "Invio" } + + def get_numero(dev): stringa = "" last = None while True: - r,w,x = select([dev], [], []) + r, w, x = select([dev], [], []) 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: - last = char_map[event.value] - if last != "Invio": - stringa += str(last) - else: - yield stringa - stringa = "" + try: + last = char_map[event.value] + if last != "Invio": + stringa += str(last) + else: + yield stringa + stringa = "" + except KeyError: + print("Carattere sconosciuto") else: last = None + for x in get_numero(dev): - print(x[0:12]) \ No newline at end of file + print("ricevo", x[0:12])