Skip to main content

Monitor and control user input devices

Project description

pynput

This library allows you to control and monitor input devices.

Currently, mouse and keyboard input and monitoring are supported.

Controlling the mouse

Use pynput.mouse.Controller like this:

from pynput.mouse import Button, Controller

mouse = Controller()

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

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

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

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

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

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

Monitoring the mouse

Use pynput.mouse.Listener like this:

from pynput.mouse import Listener

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 listener:
    listener.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.

On Windows, virtual events sent by other processes may not be received. This library takes precautions, however, to dispatch any virtual events generated to all currently running listeners of the current process.

Controlling the keyboard

Use pynput.keyboard.Controller like this:

from pynput.keyboard import Key, Controller

keyboard = Controller()

# Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space)

# Type a lower case A; this will work even if no key on the
# physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a')

# Type two upper case As
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
    keyboard.press('a')
    keyboard.release('a')

# Type 'Hello World' using the shortcut type method
keyboard.type('Hello World')

Monitoring the keyboard

Use pynput.keyboard.Listener like this:

from pynput.keyboard import Key, Listener

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

def on_release(key):
    print('{0} release'.format(
        key))
    if key == Key.esc:
        # Stop listener
        return False

# Collect events until released
with Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

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

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

Starting a keyboard listener may be subject to some restrictions on your platform.

On Mac OSX, one of the following must be true:

  • The process must run as root.

  • Your application must be white listed under Enable access for assistive devices. Note that this might require that you package your application, since otherwise the entire Python installation must be white listed.

On Windows, virtual events sent by other processes may not be received. This library takes precautions, however, to dispatch any virtual events generated to all currently running listeners of the current process.

Release Notes

v1.0.5 - Fixes for dragging on OSX

  • Corrected dragging on OSX.

  • Added scroll speed constant for OSX to correct slow scroll speed.

v1.0.4 - Fixes for clicking and scrolling on Windows

  • Corrected name of mouse input field when sending click and scroll events.

v1.0.3 - Fixes for Python 3 on Windows

  • Corrected use of ctypes on Windows.

v1.0.2 - Fixes for thread identifiers

  • Use thread identifiers to identify threads, not Thread instances.

v1.0.1 - Fixes for Python 3

  • Corrected bugs which prevented the library from being used on Python 3.

v1.0 - Stable Release

  • Changed license to LGPL.

  • Corrected minor bugs and inconsistencies.

  • Corrected and extended documentation.

v0.6 - Keyboard Monitor

  • Added support for monitoring the keyboard.

  • Corrected wheel packaging.

  • Corrected deadlock when stopping a listener in some cases on X.

  • Corrected key code constants on Mac OSX.

  • Do not intercept events on Mac OSX.

v0.5.1 - Do not die on dead keys

  • Corrected handling of dead keys.

  • Corrected documentation.

v0.5 - Keyboard Modifiers

  • Added support for modifiers.

v0.4 - Keyboard Controller

  • Added keyboard controller.

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 Distributions

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

pynput-1.0.5-py3.4.egg (130.3 kB view details)

Uploaded Egg

pynput-1.0.5-py3-none-any.whl (202.9 kB view details)

Uploaded Python 3

pynput-1.0.5-py2.py3-none-any.whl (202.9 kB view details)

Uploaded Python 2Python 3

pynput-1.0.5-py2.7.egg (129.8 kB view details)

Uploaded Egg

pynput-1.0.5-py2-none-any.whl (202.9 kB view details)

Uploaded Python 2

File details

Details for the file pynput-1.0.5-py3.4.egg.

File metadata

  • Download URL: pynput-1.0.5-py3.4.egg
  • Upload date:
  • Size: 130.3 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pynput-1.0.5-py3.4.egg
Algorithm Hash digest
SHA256 ba709daf38324383a28e8e2aacf29c8ff373a777f440f0392e10c70ed149ef8c
MD5 14aa1f4c5747ac1623ec2f50f8e3bf09
BLAKE2b-256 4b02b0f1f5b0f7eae621f726f02910f7aaa8f6a698f507ee1765820f09812396

See more details on using hashes here.

File details

Details for the file pynput-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for pynput-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ebf2860dc8c8dabcc0b6fc83e50e3a2b1a5358ddb291b95fa884a06cb41cf1ab
MD5 294735eca4af868b419940c4b59bddab
BLAKE2b-256 6891e3e2860f6383d25d851487308fe10429fe67c27862f362879711955a07d9

See more details on using hashes here.

File details

Details for the file pynput-1.0.5-py2.py3-none-any.whl.

File metadata

  • Download URL: pynput-1.0.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 202.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for pynput-1.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4b4756756d0e6f246b094ac311b060413810523e5dfa6095980a172299b2809a
MD5 ec3fd8e1f8da16d7a3c5ed63bfa5f035
BLAKE2b-256 0709e68a89040c3494579d18a60415f82f8d499ff04749eeb0f85ec86472878a

See more details on using hashes here.

File details

Details for the file pynput-1.0.5-py2.7.egg.

File metadata

  • Download URL: pynput-1.0.5-py2.7.egg
  • Upload date:
  • Size: 129.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pynput-1.0.5-py2.7.egg
Algorithm Hash digest
SHA256 3ed0c9ddf85cbd03a860c7d666ae524d2ae24b3f4f6bec1a1a64ca30e02e4fd5
MD5 06c3638679ed2dd72d60983fd292b92c
BLAKE2b-256 af70280ab2dde60ad13988394c7a07aab1ac07f5a5aaa17e029a4b03fafdc836

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pynput-1.0.5-py2-none-any.whl
Algorithm Hash digest
SHA256 7b3d5c7da64bd0d08785272c6549cdf34a1c412bc5d02311b0dd589ab578d6e7
MD5 e0e88782e171103225cff861598944fe
BLAKE2b-256 085b3cf1627fd1c23e8a6be1660ae3e92d501c7a946fe430e981581ce5c7be89

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