moses-palmer / pynput

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

keyboard.Controller does not work (macos ) #414

Closed KhDenys closed 3 years ago

KhDenys commented 3 years ago

Hi, I can't tap any keys by pynput, here the code that I've used to simulate key combination:

import time

from pynput.keyboard import Key, KeyCode, Listener, Controller

keyboard = Controller()

def on_press(key):
    print('{0} pressed'.format(key))

def on_release(key):
    print('{0} release'.format(key))
    keyboard.tap(KeyCode.from_char('w'))
    time.sleep(0.025)
    keyboard.type('x')
    time.sleep(0.025)
    keyboard.type('r')
    time.sleep(0.025)
    if key == Key.esc:
        return False

with Listener(
    on_press=on_press,
    on_release=on_release,
) as listener:
    listener.join()
moses-palmer commented 3 years ago

(I took the liberty to edit your comment for readability: https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks)

When I run your sample, it enters an infinite loop of printing "w", "x" and "r". Every tap and type will cause on_release to be called.

If this is not what is happening for you, please see the documentation for requirements when running under macOS.

KhDenys commented 3 years ago

@moses-palmer Idk how, but it seems like some dependence wasn't installed. So after I had installed another similar libs, pynput.keyboard.Controller started working.