moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.79k stars 248 forks source link

on_press gives different key attributes values when backend is uinput #319

Closed bjr-lopos closed 3 years ago

bjr-lopos commented 3 years ago

Hi all,

the "key" object given by the on_press event on a xorg backend is different compared to an uinput backend. The xorg backend was used as development environment on Ubuntu 20.04.1 LTS. The uinput backend on this environment which we are focusing on:

We ran this minimal python script on both environments.

def on_press(key):
    pprint(vars(key))

listener = Listener( on_press=on_press)
listener.start()

while True:
  time.sleep(1)

For the keys (enter, 'a' and numpad 2) on a xorg backend (as non-root on Ubuntu) we get:

{'__objclass__': <enum 'Key'>, '_name_': 'enter', '_value_': <65293>}
{'_symbol': None, 'char': 'a', 'combining': None, 'is_dead': False, 'vk': 97}
{'_symbol': None, 'char': '2', 'combining': None, 'is_dead': False, 'vk': None}

For the same keys on a uinput backend (as root on the pi) env we get:

{'_kernel_name': None, '_x_name': None, 'char': 'Key.enter', 'combining': None, 'is_dead': False, 'vk': 28}
{'_kernel_name': None, '_x_name': None, 'char': "'a'", 'combining': None, 'is_dead': False, 'vk': 16}
{'_kernel_name': None, '_x_name': None, 'char': None, 'combining': None, 'is_dead': False, 'vk': 80}

showkey shows similiar output on both systems. In a bash terminal the keyboard behaves normal. We would like to handle the keys in Python in an uniform way. Are we missing a mapping in the OS or in python?

Thanks,

Bart

moses-palmer commented 3 years ago

Thank you for your report.

The only attribute of a key that can be relied upon to be portable between different backends is char. When looking at your example output, it is clear that that is not the case; the values for the uinput backend are incorrect. I will look into this.

moses-palmer commented 3 years ago

I have prepared a small for for the uinput backend in fixup-uinput-keycodes. It seems to work properly now, but I would very much appreciate if you could test it also.

moses-palmer commented 3 years ago

I close this issue due to lack of feedback with the assumption that the branch mentioned above, which has now been merged into the main branch, solves it.

If it does not, please reopen this issue.