moses-palmer / pynput

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

Incorrect keycode intepreted after control? #409

Closed Inc0n closed 2 years ago

Inc0n commented 3 years ago

Description

Keycode after a control modifier is not understood as the original keycode. For example pressing control-v, the first control is understood, but the next key is '\x16' instead of the 'v' Output:

$ python3 ckhd.py
Key.ctrl_r Key.ctrl {'v', <Key.ctrl: <59>>} set()
'\x16' '\x16' {'v', <Key.ctrl: <59>>} {<Key.ctrl: <59>>}

Platform and pynput version Mac M1 big sur 11.5.1 pynput 1.7.3

To Reproduce

With the following code:

import pynput.keyboard as Keyboard

ctrlvkbd = Keyboard.HotKey.parse("<ctrl>+v")
ctrlr = Keyboard.HotKey(ctrlvkbd, lambda: print("ctrl r triggers"))

def on_press(key):
    print(key, listener.canonical(key), ctrlr._keys, ctrlr._state)
    ctrlr.press(listener.canonical(key))

def on_release(key):
    ctrlr.release(listener.canonical(key))

with Keyboard.Listener(
    on_press=on_press,
    on_release=on_release
) as listener:
    listener.join()

Run the code, and press control+v. and the message "ctrl r triggers" is never seen

moses-palmer commented 3 years ago

Thank you for report.

This issue has been reported previously, but I finally have a working macOS environment, so I managed to attempt a solution. If you find the time, please test the branch fixup-macos-ctrl-modifier and provide feedback on this issue.

moses-palmer commented 2 years ago

I have verified that the snippet provided in the comment above works with the test branch, and it has been merged into master, so I will close this issue.