GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing pypi.org — pynput pynput 1.8.2Monitor and control user input devices pip install pynput pynputThis library allows you to control and monitor input devices. Currently, mouse and keyboard input and monitoring are supported. See here for the full documentation. Controlling the mouseUse pynput.mouse.Controller like this: from pynput.mouse import Button, Controller
mouse = Controller()
# Read pointer position
print('The current pointer position is {}'.format(
mouse.position))
# Set pointer position
mouse.position = (10, 20)
print('Now we have moved it to {}'.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 macOS
mouse.click(Button.left, 2)
# Scroll two steps down
mouse.scroll(0, 2)
Monitoring the mouseUse pynput.mouse.Listener like this: from pynput import mouse
def on_move(x, y, injected):
print('Pointer moved to {}; it was {}'.format(
(x, y, 'faked' if injected else 'not faked')))
def on_click(x, y, button, pressed, injected):
print('{} at {}; it was {}'.format(
'Pressed' if pressed else 'Released',
(x, y, 'faked' if injected else 'not faked')))
if not pressed:
# Stop listener
return False
def on_scroll(x, y, dx, dy, injected):
print('Scrolled {} at {}; it was {}'.format(
'down' if dy < 0 else 'up',
(x, y, 'faked' if injected else 'not faked')))
# Collect events until released
with mouse.Listener(
on_move=on_move,
on_click=on_click,
on_scroll=on_scroll) as listener:
listener.join()
# ...or, in a non-blocking fashion:
listener = mouse.Listener(
on_move=on_move,
on_click=on_click,
on_scroll=on_scroll)
listener.start()
A mouse listener is a threading.Thread, and all callbacks will be invoked from the thread. Call pynput.mouse.Listener.stop from anywhere, raise StopException or return False from a callback to stop the listener. When using the non-blocking version above, the current thread will continue executing. This might be necessary when integrating with other GUI frameworks that incorporate a main-loop, but when run from a script, this will cause the program to terminate immediately. The mouse listener threadThe listener callbacks are invoked directly from an operating thread on some platforms, notably Windows. This means that long running procedures and blocking operations should not be invoked from the callback, as this risks freezing input for all processes. A possible workaround is to just dispatch incoming messages to a queue, and let a separate thread handle them. Handling mouse listener errorsIf a callback handler raises an exception, the listener will be stopped. Since callbacks run in a dedicated thread, the exceptions will not automatically be reraised. To be notified about callback errors, call Thread.join on the listener instance: from pynput import mouse
class MyException(Exception): pass
def on_click(x, y, button, pressed):
if button == mouse.Button.left:
raise MyException(button)
# Collect events until released
with mouse.Listener(
on_click=on_click) as listener:
try:
listener.join()
except MyException as e:
print('{} was clicked'.format(e.args[0]))
Toggling event listening for the mouse listenerOnce pynput.mouse.Listener.stop has been called, the listener cannot be restarted, since listeners are instances of threading.Thread. If your application requires toggling listening events, you must either add an internal flag to ignore events when not required, or create a new listener when resuming listening. Synchronous event listening for the mouse listenerTo simplify scripting, synchronous event listening is supported through the utility class pynput.mouse.Events. This class supports reading single events in a non-blocking fashion, as well as iterating over all events. To read a single event, use the following code: from pynput import mouse
# The event listener will be running in this block
with mouse.Events() as events:
# Block at most one second
event = events.get(1.0)
if event is None:
print('You did not interact with the mouse within one second')
else:
print('Received event {}'.format(event))
To iterate over mouse events, use the following code: from pynput import mouse
# The event listener will be running in this block
with mouse.Events() as events:
for event in events:
if event.button == mouse.Button.right:
break
else:
print('Received event {}'.format(event))
Please note that the iterator method does not support non-blocking operation, so it will wait for at least one mouse event. The events will be instances of the inner classes found in pynput.mouse.Events. Ensuring consistent coordinates between listener and controller on WindowsRecent versions of _Windows_ support running legacy applications scaled when the system scaling has been increased beyond 100%. This allows old applications to scale, albeit with a blurry look, and avoids tiny, unusable user interfaces. This scaling is unfortunately inconsistently applied to a mouse listener and a controller: the listener will receive physical coordinates, but the controller has to work with scaled coordinates. This can be worked around by telling Windows that your application is DPI aware. This is a process global setting, so _pynput_ cannot do it automatically. Do enable DPI awareness, run the following code: import ctypes PROCESS_PER_MONITOR_DPI_AWARE = 2 ctypes.windll.shcore.SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE) Controlling the keyboardUse 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 keyboardUse pynput.keyboard.Listener like this: from pynput import keyboard
def on_press(key, injected):
try:
print('alphanumeric key {} pressed; it was {}'.format(
key.char, 'faked' if injected else 'not faked'))
except AttributeError:
print('special key {} pressed'.format(
key))
def on_release(key, injected):
print('{} released; it was {}'.format(
key, 'faked' if injected else 'not faked'))
if key == keyboard.Key.esc:
# Stop listener
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
# ...or, in a non-blocking fashion:
listener = keyboard.Listener(
on_press=on_press,
on_release=on_release)
listener.start()
A keyboard listener is a threading.Thread, and all callbacks will be invoked from the thread. Call pynput.keyboard.Listener.stop from anywhere, raise StopException or return False from a callback to stop the listener. The key parameter passed to callbacks is a pynput.keyboard.Key, for special keys, a pynput.keyboard.KeyCode for normal alphanumeric keys, or just None for unknown keys. When using the non-blocking version above, the current thread will continue executing. This might be necessary when integrating with other GUI frameworks that incorporate a main-loop, but when run from a script, this will cause the program to terminate immediately. The keyboard listener threadThe listener callbacks are invoked directly from an operating thread on some platforms, notably Windows. This means that long running procedures and blocking operations should not be invoked from the callback, as this risks freezing input for all processes. A possible workaround is to just dispatch incoming messages to a queue, and let a separate thread handle them. Handling keyboard listener errorsIf a callback handler raises an exception, the listener will be stopped. Since callbacks run in a dedicated thread, the exceptions will not automatically be reraised. To be notified about callback errors, call Thread.join on the listener instance: from pynput import keyboard
class MyException(Exception): pass
def on_press(key):
if key == keyboard.Key.esc:
raise MyException(key)
# Collect events until released
with keyboard.Listener(
on_press=on_press) as listener:
try:
listener.join()
except MyException as e:
print('{} was pressed'.format(e.args[0]))
Toggling event listening for the keyboard listenerOnce pynput.keyboard.Listener.stop has been called, the listener cannot be restarted, since listeners are instances of threading.Thread. If your application requires toggling listening events, you must either add an internal flag to ignore events when not required, or create a new listener when resuming listening. Synchronous event listening for the keyboard listenerTo simplify scripting, synchronous event listening is supported through the utility class pynput.keyboard.Events. This class supports reading single events in a non-blocking fashion, as well as iterating over all events. To read a single event, use the following code: from pynput import keyboard
# The event listener will be running in this block
with keyboard.Events() as events:
# Block at most one second
event = events.get(1.0)
if event is None:
print('You did not press a key within one second')
else:
print('Received event {}'.format(event))
To iterate over keyboard events, use the following code: from pynput import keyboard
# The event listener will be running in this block
with keyboard.Events() as events:
for event in events:
if event.key == keyboard.Key.esc:
break
else:
print('Received event {}'.format(event))
Please note that the iterator method does not support non-blocking operation, so it will wait for at least one keyboard event. The events will be instances of the inner classes found in pynput.keyboard.Events. Global hotkeysA common use case for keyboard monitors is reacting to global hotkeys. Since a listener does not maintain any state, hotkeys involving multiple keys must store this state somewhere. pynput provides the class pynput.keyboard.HotKey for this purpose. It contains two methods to update the state, designed to be easily interoperable with a keyboard listener: pynput.keyboard.HotKey.press and pynput.keyboard.HotKey.release which can be directly passed as listener callbacks. The intended usage is as follows: from pynput import keyboard
def on_activate():
print('Global hotkey activated!')
def for_canonical(f):
return lambda k: f(l.canonical(k))
hotkey = keyboard.HotKey(
keyboard.HotKey.parse('<ctrl>+<alt>+h'),
on_activate)
with keyboard.Listener(
on_press=for_canonical(hotkey.press),
on_release=for_canonical(hotkey.release)) as l:
l.join()
This will create a hotkey, and then use a listener to update its state. Once all the specified keys are pressed simultaneously, on_activate will be invoked. Note that keys are passed through pynput.keyboard.Listener.canonical before being passed to the HotKey instance. This is to remove any modifier state from the key events, and to normalise modifiers with more than one physical button. The method pynput.keyboard.HotKey.parse is a convenience function to transform shortcut strings to key collections. Please see its documentation for more information. To register a number of global hotkeys, use the convenience class pynput.keyboard.GlobalHotKeys: from pynput import keyboard
def on_activate_h():
print('<ctrl>+<alt>+h pressed')
def on_activate_i():
print('<ctrl>+<alt>+i pressed')
with keyboard.GlobalHotKeys({
'<ctrl>+<alt>+h': on_activate_h,
'<ctrl>+<alt>+i': on_activate_i}) as h:
h.join()
Release Notesv1.8.2 (2026-05-12) - Include missing injected parameter on macOS
v1.8.1 (2025-03-17) - Various fixes
v1.8.0 (2025-03-03) - Allow detecting injected events
v1.7.8 (2025-02-28) - Fixes for Python 3.12
v1.7.7 (2024-05-10) - Various fixes
v1.7.6 (2022-01-01) - Various fixes
v1.7.5 (2021-11-19) - Various fixes
v1.7.4 (2021-10-10) - Various fixes
v1.7.3 (2021-02-10) - Various fixes
v1.7.2 (2020-12-21) - Corrected uinput key mapping
v1.7.1 (2020-08-30) - Corrected release notes
v1.7.0 (2020-08-30) - A new backend and many new features and bug fixes
v1.6.8 (2020-02-28) - Various fixes
v1.6.7 (2020-02-17) - Various fixes
v1.6.6 (2020-01-23) - Corrected hot key documentation
v1.6.5 (2020-01-08) - Corrected media key mappings
v1.6.4 (2020-01-03) - Corrected imports yet again
v1.6.3 (2019-12-28) - Corrected imports again
v1.6.2 (2019-12-28) - Corrected imports
v1.6.1 (2019-12-27) - Corrections for Windows
v1.6.0 (2019-12-11) - Global Hotkeys
v1.5.2 (2019-12-06) - Corrected media key names for Xorg
v1.5.1 (2019-12-06) - Corrected media key names for macOS
v1.5.0 (2019-12-04) - Various improvements
v1.4.5 (2019-11-05) - Corrected errors on Python 3.8
v1.4.4 (2019-09-24) - Actually corrected keyboard listener on macOS
v1.4.3 (2019-09-24) - Corrected keyboard listener on macOS again
v1.4.2 (2019-03-22) - Corrected keyboard listener on macOS
v1.4.1 (2018-09-07) - Logging
v1.4 (2018-07-03) - Event suppression
v1.3.10 (2018-02-05) - Do not crash under Xephyr
v1.3.9 (2018-01-12) - Correctly handle the letter A on OSX
v1.3.8 (2017-12-08) - Do not crash on some keyboard layouts on OSX
v1.3.7 (2017-08-23) - Xorg corrections
v1.3.6 (2017-08-13) - win32 corrections
v1.3.5 (2017-06-07) - Corrected dependencies again
v1.3.4 (2017-06-05) - Xorg corrections
v1.3.3 (2017-06-05) - Make dependencies non-optional
v1.3.2 (2017-05-15) - Fix for button click on Mac
v1.3.1 (2017-05-12) - Fixes for unknown buttons on Linux
v1.3 (2017-04-10) - Platform specific features
v1.2 (2017-01-06) - Improved error handling
v1.1.7 (2017-01-02) - Handle middle button on Windows
v1.1.6 (2016-11-24) - Corrected context manager for pressing keys
v1.1.5 (2016-11-17) - Corrected modifier key combinations on Linux
v1.1.4 (2016-10-30) - Small bugfixes
v1.1.3 (2016-09-27) - Changed Xlib backend library
v1.1.2 (2016-09-26) - Added missing type for Python 2
v1.1.1 (2016-09-26) - Fixes for listeners and controllers on Windows
v1.1 (2016-06-22) - Simplified usage on Linux
v1.0.6 (2016-04-19) - Universal wheel
v1.0.5 (2016-04-11) - Fixes for dragging on OSX
v1.0.4 (2016-04-11) - Fixes for clicking and scrolling on Windows
v1.0.3 (2016-04-05) - Fixes for Python 3 on Windows
v1.0.2 (2016-04-03) - Fixes for thread identifiers
v1.0.1 (2016-04-03) - Fixes for Python 3
v1.0 (2016-02-28) - Stable Release
v0.6 (2016-02-08) - Keyboard Monitor
v0.5.1 (2016-01-26) - Do not die on dead keys
v0.5 (2016-01-18) - Keyboard Modifiers
v0.4 (2015-12-22) - Keyboard Controller
v0.3 (2015-12-22) - Cleanup
v0.2 (2015-10-28) - Initial Release
Project linksKey datesPyPI dataData sourced directly from PyPI's database.
1 maintainerPyPI dataData sourced directly from PyPI's database.
CreditsAuthor: Moses Palmér LicenseGNU Lesser General Public License v3 (LGPLv3) (LGPLv3) Tags
Classifiers
MetadataProject linksKey datesPyPI dataData sourced directly from PyPI's database.
1 maintainerPyPI dataData sourced directly from PyPI's database.
CreditsAuthor: Moses Palmér LicenseGNU Lesser General Public License v3 (LGPLv3) (LGPLv3) Tags
Classifiers
Release files for pynput 1.8.2For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation. Source distribution (sdist)
Built distribution (wheel)
Total release size: 174.8 kB Release files / pynput-1.8.2.tar.gz
Release files / pynput-1.8.2-py2.py3-none-any.whl
Security for pynput 1.8.2ProvenanceProvenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats. No provenance foundNone of the files in this release were uploaded with provenance metadata. A note on package securityPyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package. To learn more about protecting yourself from malicious actors, visit our package security documentation. Release history Release notifications | RSS feed | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |