moses-palmer / pynput

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

Consistent way to get the key name #462

Open josephernest opened 2 years ago

josephernest commented 2 years ago

When doing:

from pynput import keyboard
def keyevent(key):
    name = key.name if hasattr(key, 'name') else key.char
    print(name)
with keyboard.Listener(on_press=keyevent, on_release=keyevent) as listener:
    listener.join()

I get:

b          # ok   when pressing the B key

ctrl_l             # ok
alt_l              # ok
None               # not ok!    when doing CTRL + ALT + B         , should be :   b

ctrl_l                 # ok
<weird UTF8 char>      # not ok!  when doing CTRL + B     , should be :      b

How to solve this and get consistent key names?

I use Windows + Python 3.7.