Skip to main content

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.1 - Simplified usage on Linux

  • Propagate import errors raised on Linux to help troubleshoot missing Xlib module.

  • Declare python3-xlib as dependency on Linux for Python 3.

v1.0.6 - Universal wheel

  • Make sure to build a universal wheel for all python versions.

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.

Metadata

Release files for pynput 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for pynput 1.1
File Interpreter ABI Platform
pynput-1.1-py3.5.egg Legacy Egg format - - Details
pynput-1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details
pynput-1.1-py2.7.egg Legacy Egg format - - Details

Total release size: 321.0 kB

Release files / pynput-1.1-py3.5.egg

Download URL pynput-1.1-py3.5.egg
Size 131.3 kB
Tags Egg
SHA-256 checksum
How to use checksums
8eed32c62c254eaefc9c1a4ec697c32957ed6abfa16d54a10c73d6c5733d1620
BLAKE2b-256 checksum
How to use checksums
08d77ffb5f6e78afdc1611aafdfe5515ee90988297d30292ea06829dad87dc83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pynput-1.1-py2.py3-none-any.whl

Download URL pynput-1.1-py2.py3-none-any.whl
Size 59.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3a296fdc07f9fb063adb514558df649e7b4a6c69a5636483a08f95e7a4b429d1
BLAKE2b-256 checksum
How to use checksums
46cb35e92b107f76c736e5e5d93c04d15cc706afa62950eb5138dcf3d1899620
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pynput-1.1-py2.7.egg

Download URL pynput-1.1-py2.7.egg
Size 130.2 kB
Tags Egg
SHA-256 checksum
How to use checksums
06400e169df8e740866c9892bfa8e545299ae5335e14d1dda929dba456f2a31f
BLAKE2b-256 checksum
How to use checksums
c7dabe49978438f0fa82ed41d6c764e62b3dc67501e8db0406f08a4a3c9ceff1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

1.8.2

2 release files

1.8.1

2 release files

1.8.0

1 release file

1.7.8

1 release file

1.7.7

1 release file

1.7.6

3 release files

1.7.5

3 release files

1.7.4

3 release files

1.7.3

3 release files

1.7.2

3 release files

1.7.1

3 release files

1.7.0

3 release files

1.6.8

3 release files

1.6.7

3 release files

1.6.6

3 release files

1.6.5

3 release files

1.6.4

3 release files

1.6.3

3 release files

1.6.2

3 release files

1.6.1

3 release files

1.6.0

3 release files

1.5.2

3 release files

1.5.1

3 release files

1.5.0

3 release files

1.4.5

3 release files

1.4.4

3 release files

1.4.3

3 release files

1.4.2

3 release files

1.4

1 release file

1.3.9

2 release files

1.3.8.1

1 release file

1.3.7

2 release files

1.3.6

2 release files

1.3.5

3 release files

1.3.4

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3

2 release files

1.2

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

3 release files

1.1.1

2 release files

This release

1.1 This release

3 release files

1.0.6

4 release files

1.0.5

5 release files

1.0.4

4 release files

1.0.3

4 release files

1.0.2

2 release files

1.0.1

2 release files

1.0

1 release file

0.6

1 release file

0.5.1

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

2 release files

0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page