Python Library: pynput

control mouse and keyboard with Python

06 Sep 2022

Library resources
PyPI https://pypi.org/project/pynput/
Github https://github.com/moses-palmer/pynput
Documentation https://pynput.readthedocs.io/en/latest/

pip3 install pynput

Snippets

keyboard automation

from pynput.keyboard import Key, Controller

keyb = Controller()

with keyb.pressed(Key.cmd):
    keyb.press('f')
    keyb.release('f')

see which key is being pressed

from pynput.keyboard import Key, Listener

def show(key):

    print('\nYou Entered {0}'.format( key))

    if key == Key.delete:
        # Stop listener
        return False

# Collect all event until released
with Listener(on_press = show) as listener:
    listener.join()

links

social