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.1.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.1-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyinterceptor_hotkeys-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 12087bf2010c5e6f18f692edbcca0695433d5e56d6522958feac2da7f30294c8
MD5 a2a8958bfcf87a544a97a2abd13c3a0a
BLAKE2b-256 d7fc8f0a9a880557988bea7ef2ec8afbf38274e13a76268c9138e248d0c29e17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyinterceptor_hotkeys-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d3218ae07163b9b79438e079dc332497d014297880ad386b5e489e9a18c5a07e
MD5 b48d4ed800dd1fb89176bdf7eb11bd7f
BLAKE2b-256 3d6bf9262914554aecfe5204242704e5b568961b683a0e06136b34e5c22f8806

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