Skip to main content

Monitor and control user input devices

Project description

pynput

This library allows you to control and monitor input devices.

Currently, only mouse input is supported.

Controlling the mouse

Use pynput.mouse.Controller like this:

from pynput.mouse import Button, Controller, Listener

d = Controller()

# Read pointer position
print('The current pointer position is {0}'.format(
    d.position))

# Set pointer position
d.position = (10, 20)
print('Now we have moved it to {0}'.format(
    d.position))

# Move pointer relative to current position
d.move(5, -5)

# Press and release
d.press(Button.left)
d.release(Button.left)

# Double click; this is different from pressing and releasing twice on Mac
# OSX
d.click(Button.left, 2)

# Scroll two steps down
d.scroll(0, 2)

Monitoring the mouse

Use pynput.mouse.Listener like this:

def on_move(x, y):
    print('Pointer moved to {0}'.format(
        (x, y)))

def on_click(x, y, button, pressed):
    print('{0} at {1}'.format(
        'Pressed' if pressed else 'Released',
        (x, y)))
    if not pressed:
        # Stop listener
        return False

def on_scroll(dx, dy):
    print('Scrolled {0}'.format(
        (x, y)))

# Collect events until released
with Listener(
        on_move=on_move,
        on_click=on_click,
        on_scroll=on_scroll) as l:
    l.join()

A mouse listener is a threading.Thread, and all callbacks will be invoked from the thread.

Call pynput.mouse.Listener.stop from anywhere, or raise pynput.mouse.Listener.StopException or return False from a callback to stop the listener.

Release Notes

v0.3 - Cleanup

  • Moved pynput.mouse.Controller.Button to top-level.

v0.2 - Initial Release

  • Support for controlling the mouse on Linux, Mac OSX and Windows.

  • Support for monitoring the mouse on Linux, Mac OSX and Windows.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pynput-0.3-py2-none-any.whl (49.4 kB view details)

Uploaded Python 2

File details

Details for the file pynput-0.3-py2-none-any.whl.

File metadata

  • Download URL: pynput-0.3-py2-none-any.whl
  • Upload date:
  • Size: 49.4 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pynput-0.3-py2-none-any.whl
Algorithm Hash digest
SHA256 57a4325c80031680b629512edcd12e509f741e1dcdaaeb4d25b1608863522fd9
MD5 e3ab0e2d0d27b78950c5419274d68c2f
BLAKE2b-256 87ef9330409d2c75b98da8a131ed207acb29c16e27a7f724362c1bd5be3a4910

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page