1

so i have successfully been able to track both mouse and keyboard events using the following code

devices = map(InputDevice, ('/dev/input/event1', '/dev/input/event0'))
devices = {dev.fd: dev for dev in devices}

for dev in devices.values(): print(dev)

while True:
    r, w, x = select(devices, [], [])
    for fd in r:
            for event in devices[fd].read():
                    print(event)

and the data i get from this code when pressing a key or moving the mouse is the following:

event at 1550702472.373994, code 00, type 00, val 00
event at 1550702472.389984, code 00, type 02, val -5
event at 1550702472.389984, code 01, type 02, val -2
event at 1550702472.389984, code 00, type 00, val 00
event at 1550702472.405988, code 00, type 02, val -3
event at 1550702472.405988, code 00, type 00, val 00
event at 1550702472.421987, code 00, type 02, val -2

my question is how can i convert this data or analyze the numbers to get a good output like an actual key. something like analyzing the 1550702472.xxx number to get a certain key or maybe the numbers following.

UPDATE: when i say print(categorize(event)) i get this:

key event at 1550703468.964836, 38 (KEY_L), down
synchronization event at 1550703468.964836, SYN_REPORT
event at 1550703469.052829, code 04, type 04, val 458767
key event at 1550703469.052829, 38 (KEY_L), up
synchronization event at 1550703469.052829, SYN_REPORT
relative axis event at 1550703472.093778, REL_Y
synchronization event at 1550703472.093778, SYN_REPORT
relative axis event at 1550703472.125780, REL_X
synchronization event at 1550703472.125780, SYN_REPORT
relative axis event at 1550703472.141764, REL_X

still the same question though, what array can i use to say if KEY_L do this.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • The running number there is a timestamp. The actual keycodes are the latter numbers. – ferrix Feb 20 '19 at 23:00
  • Numeric comparisons are as a rule faster than string comparisons, so in real-world code you should use the constants (not the numbers, but the module's names for them like `evdev.evcodes.REL_X`) rather than asking the library to look up strings for you. – Charles Duffy Feb 20 '19 at 23:06
  • ...as for the values, the types tell you how to evaluate them; if your type constant matches `evdev.evcodes.REL_X`, then you know that a val of `-2` means that the mouse moved 2 units left on the X axis. If it's `evdev.evcodes.REL_Y`, you know a value of `0` means the mouse isn't moving on the vertical axis. If you have an `ABS_X` or `ABS_Y`, you know you're being given an absolute location, like on a touchscreen or tablet; etc. – Charles Duffy Feb 20 '19 at 23:09
  • BTW, it's surprising to see two evdev questions in a day from different new users -- they're fairly rare, but we also had one over at https://stackoverflow.com/questions/54794566/looking-for-a-string-in-device-capabilities-evdev-python/54794847#54794847. Out of curiosity, is this referenced in a classroom assignment? – Charles Duffy Feb 20 '19 at 23:12
  • so when i click the key L and it says KEY_L what should i use to get that string out of the data so i can compare it even if its bad structure – reese houseknecht Feb 20 '19 at 23:15
  • charles it is the same person and i know that is against stack rules but i am trying my hardest to write this code and i truly dont know what to search up – reese houseknecht Feb 20 '19 at 23:17

0 Answers0