Top Related Projects
A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
Hook and simulate global keyboard events on Windows and Linux.
A module for cross-platform control of the mouse and keyboard in python that is simple to install and use.
Quick Overview
Pynput is a Python library that allows you to control and monitor input devices such as the keyboard and mouse. It provides a simple, cross-platform interface for listening to events from these devices and programmatically generating input events.
Pros
- Cross-platform compatibility (Windows, macOS, Linux)
- Easy-to-use API for both listening to and controlling input devices
- Supports both synchronous and asynchronous event handling
- Well-documented with clear examples
Cons
- May require root/admin privileges on some systems for certain functionalities
- Limited support for complex keyboard layouts or non-standard input devices
- Potential security concerns when used maliciously (e.g., keylogging)
- Performance may be affected when handling high-frequency events
Code Examples
- Listening to keyboard events:
from pynput import keyboard
def on_press(key):
print(f'Key {key} pressed')
def on_release(key):
print(f'Key {key} released')
if key == keyboard.Key.esc:
return False
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
- Controlling the mouse:
from pynput.mouse import Button, Controller
mouse = Controller()
# Move the mouse to a specific position
mouse.position = (100, 200)
# Perform a click
mouse.click(Button.left, 1)
# Scroll the mouse wheel
mouse.scroll(0, 2)
- Simulating keyboard input:
from pynput.keyboard import Key, Controller
keyboard = Controller()
# Type a string
keyboard.type('Hello, World!')
# Press and release a key
keyboard.press(Key.shift)
keyboard.press('a')
keyboard.release('a')
keyboard.release(Key.shift)
Getting Started
To get started with pynput, follow these steps:
-
Install pynput using pip:
pip install pynput
-
Import the necessary modules in your Python script:
from pynput import keyboard, mouse
-
Create listeners or controllers as needed:
keyboard_listener = keyboard.Listener(on_press=on_press, on_release=on_release) mouse_controller = mouse.Controller()
-
Start the listeners or use the controllers in your code:
keyboard_listener.start() mouse_controller.move(100, 0)
For more detailed information and advanced usage, refer to the official documentation at https://pynput.readthedocs.io/.
Competitor Comparisons
A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
Pros of PyAutoGUI
- Easier to use for beginners with simpler API and more intuitive functions
- Provides cross-platform support for Windows, macOS, and Linux
- Includes additional features like screenshot capabilities and image recognition
Cons of PyAutoGUI
- Less precise control over keyboard and mouse events
- May have performance issues with rapid input sequences
- Limited support for complex key combinations and special keys
Code Comparison
PyAutoGUI:
import pyautogui
pyautogui.moveTo(100, 100)
pyautogui.click()
pyautogui.typewrite('Hello, world!')
pynput:
from pynput.mouse import Controller as MouseController
from pynput.keyboard import Controller as KeyboardController
mouse = MouseController()
keyboard = KeyboardController()
mouse.position = (100, 100)
mouse.click(mouse.Button.left)
keyboard.type('Hello, world!')
PyAutoGUI offers a more straightforward approach with fewer lines of code and more intuitive function names. pynput provides more granular control over input devices but requires separate controllers for mouse and keyboard.
Both libraries serve similar purposes, but PyAutoGUI is generally better suited for simpler automation tasks and beginners, while pynput offers more precise control for advanced users and complex input scenarios.
Hook and simulate global keyboard events on Windows and Linux.
Pros of keyboard
- Simpler API with more intuitive function names
- Built-in hotkey support for easy keyboard shortcut creation
- Cross-platform support without additional dependencies
Cons of keyboard
- Less comprehensive mouse control functionality
- May have compatibility issues with some non-standard keyboard layouts
- Limited support for complex input scenarios
Code Comparison
keyboard:
import keyboard
keyboard.press_and_release('shift+s, space')
keyboard.write('Hello, World!')
keyboard.add_hotkey('ctrl+alt+p', print, args=('Hotkey pressed!',))
pynput:
from pynput.keyboard import Key, Controller
keyboard = Controller()
keyboard.press(Key.shift)
keyboard.press('s')
keyboard.release('s')
keyboard.release(Key.shift)
keyboard.press(Key.space)
keyboard.release(Key.space)
keyboard.type('Hello, World!')
The keyboard library offers a more concise and readable syntax for common operations, while pynput provides finer-grained control over individual key presses and releases. keyboard's hotkey functionality is built-in, whereas pynput requires additional code to implement similar behavior. However, pynput offers more extensive mouse control options and may be better suited for complex input scenarios or applications requiring precise control over input devices.
A module for cross-platform control of the mouse and keyboard in python that is simple to install and use.
Pros of PyUserInput
- Supports a wider range of platforms, including Linux, Windows, and macOS
- Provides a more comprehensive set of input simulation functions
- Offers a simpler API for basic input operations
Cons of PyUserInput
- Less actively maintained, with fewer recent updates
- May have compatibility issues with newer operating system versions
- Documentation is less extensive compared to pynput
Code Comparison
PyUserInput:
from pymouse import PyMouse
from pykeyboard import PyKeyboard
m = PyMouse()
k = PyKeyboard()
m.click(100, 100, 1)
k.type_string('Hello, World!')
pynput:
from pynput.mouse import Controller as MouseController
from pynput.keyboard import Controller as KeyboardController
mouse = MouseController()
keyboard = KeyboardController()
mouse.position = (100, 100)
mouse.click(Button.left)
keyboard.type('Hello, World!')
Both libraries offer similar functionality for mouse and keyboard control, but pynput provides a more modern and consistent API across different input devices. PyUserInput's separate classes for mouse and keyboard may be more intuitive for some users, while pynput's unified approach can lead to cleaner code in complex projects.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
pynput
This library allows you to control and monitor input devices.
Currently, mouse and keyboard input and monitoring are supported.
See here <https://pynput.readthedocs.io/en/latest/>
_ for the full
documentation.
Top Related Projects
A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
Hook and simulate global keyboard events on Windows and Linux.
A module for cross-platform control of the mouse and keyboard in python that is simple to install and use.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot