Skip to main content

Python wrapper for Interception driver with hotkey support

Project description

PyInterceptor

PyInterceptor is a Python library for intercepting and manipulating keyboard and mouse inputs on Windows systems. It provides a high-level interface for working with keyboard and mouse events, including hotkey management and input simulation.

Features

  • Intercept keyboard and mouse inputs at the system level
  • Register and manage hotkeys with custom callbacks
  • Simulate keyboard and mouse inputs
  • Track the state of pressed keys
  • Filter specific types of input events
  • Support for both keyboard and mouse devices

Requirements

  • Windows operating system
  • Python 3.6+
  • Interception driver installed on the system

Installation

pip install pyinterceptor-hotkeys

Usage Examples

Basic Hotkey Registration

from pyinterceptor import HotkeyManager, Key, Device, KeyStroke


def on_hotkey_pressed(device: Device, stroke: KeyStroke, pressed_keys: set[Key]):
    print("Hotkey was pressed!")


# Create a hotkey manager for keyboard and use it as a context manager
with HotkeyManager(keyboard=True) as hotkey_manager:
    # Register Ctrl+Shift+A as a hotkey
    hotkey = hotkey_manager.register_hotkey([Key.LEFT_CTRL, Key.LEFT_SHIFT, Key.A], on_hotkey_pressed)

    # The manager automatically starts listening when entering the 'with' block
    print("Listening for hotkeys... Press Ctrl+Shift+A")

    # Keep the main thread alive to listen for hotkeys
    try:
        while True:
            pass
    except KeyboardInterrupt:
        pass

    # The manager automatically stops listening when exiting the 'with' block
    # To unregister a hotkey (optional, as manager cleans up on exit)
    # hotkey_manager.unregister_hotkey(hotkey)

HotkeyManager without 'with' statement

from pyinterceptor import HotkeyManager, Key, Device, KeyStroke
import time

def on_hotkey_pressed_no_with(device: Device, stroke: KeyStroke, pressed_keys: set[Key]):
    print("Hotkey was pressed (without 'with' statement)!")

# Create a hotkey manager instance
hotkey_manager_no_with = HotkeyManager(keyboard=True)

try:
    # Manually start listening
    hotkey_manager_no_with.listen()
    print("Listening for hotkeys (without 'with' statement)... Press Ctrl+Shift+B")

    # Register Ctrl+Shift+B as a hotkey
    hotkey_no_with = hotkey_manager_no_with.register_hotkey([Key.LEFT_CTRL, Key.LEFT_SHIFT, Key.B], on_hotkey_pressed_no_with)

    # Keep the main thread alive to listen for hotkeys
    while True:
        time.sleep(0.1) # Small delay to prevent busy-waiting
except KeyboardInterrupt:
    pass
finally:
    # Manually stop listening and clean up resources
    if hotkey_manager_no_with:
        hotkey_manager_no_with.close()
    # To unregister a hotkey (optional)
    # hotkey_manager_no_with.unregister_hotkey(hotkey_no_with)

Simulating Keyboard Input

from pyinterceptor import Keyboard, Key

# Create a keyboard instance (device ID 1)
keyboard = Keyboard(device=1)

# Simulate pressing and releasing the 'A' key
keyboard.tap(Key.A)

# Press a key
keyboard.press(Key.B)

# Release a key
keyboard.release(Key.B)

Intercepting Input Events

from pyinterceptor import Interception

# Get the singleton instance of Interception
interception = Interception()

# Set up a filter for keyboard events
interception.set_filter_keyboard()


# Add an event listener
def input_callback(stroke):
    print(f"Key: {stroke.code}, State: {stroke.flags}")
    # Return True to suppress the input, False to let it through
    return False


interception.add_event_listener(input_callback)

# Main event loop
try:
    while True:
        # Wait for and process input events
        interception.receive()
except KeyboardInterrupt:
    # Clean up
    interception.close()

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

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

Source Distribution

pyinterceptor_hotkeys-0.2.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

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

pyinterceptor_hotkeys-0.2.0-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file pyinterceptor_hotkeys-0.2.0.tar.gz.

File metadata

  • Download URL: pyinterceptor_hotkeys-0.2.0.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for pyinterceptor_hotkeys-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c7e9c3f5bc83fec5bc8a8ac359fc4cec04d4cada8897047f9e93c2fc740451e2
MD5 fe9eb6f54492d57a2934b93309447895
BLAKE2b-256 3491791a57db14a37a022b5f807d4db95e5f8731fa7e1b4fd70e8617a605168c

See more details on using hashes here.

File details

Details for the file pyinterceptor_hotkeys-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pyinterceptor_hotkeys-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8554c9fa097db964d488dab6861a768922fdc387dd78d43ef0044eb571804a62
MD5 239556dfed892f435dc4967da4a11121
BLAKE2b-256 b7a00464374644ab5de80dcd1962c428b84bc6108242c658f332ee53d278b360

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