pyinterception

repository·master·Indexed 42 days ago

https://github.com/kennyhml/pyinterception

A Python wrapper for the low-level Interception device driver that enables hardware-level mouse and keyboard input control. It allows for simulating inputs without injected flags to avoid software detection and includes features for human-like mouse movement using Bezier curves via the pyclick library. Version 1.13.6.

Tokens
8.5K
Snippets
34
Records
43
Agent score
52%

What's inside pyinterception

  1. Implement human-like mouse movement with Bezier Curves

    master

    To avoid heuristic input detection, you can use Bezier Curves to generate 'human' mouse movements. This feature requires the pyclick library to be installed manually.

    Configuration Options

    • Individual calls: Pass params (an instance of beziercurve.BezierCurveParams) directly to move_to().
    • Global defaults: Use beziercurve.set_default_params(params) to apply settings to all subsequent move_to() calls.
    • Bypassing curves:
      • Calling move_to(x, y) without params will 'warp' the mouse to the location.
      • Calling move_to(x, y, allow_global_params=False) will override any global defaults and 'warp' the mouse instead.
    from interception import beziercurve
    import interception
    
    interception.auto_capture_devices()
    
    # 1. Create parameters
    curve_params = beziercurve.BezierCurveParams()
    
    # 2. Use specific params for one call
    interception.move_to(960, 540, curve_params)
    
    # 3. Warp (no curve) by omitting params
    interception.move_to(960, 540)
    
    # 4. Set global defaults
    beziercurve.set_default_params(curve_params)
    interception.move_to(960, 540)  # Now uses global params
    
    # 5. Force warp even if global params are set
    interception.move_to(960, 540, allow_global_params=False)
  2. Initialize device handles with auto_capture_devices()

    master

    Before sending any inputs, you must call interception.auto_capture_devices() to allow the library to obtain the correct device handles.

    Note: The interception-driver must be installed on your system for this to work.

    import interception
    
    interception.auto_capture_devices()
  3. How Stroke objects handle data conversion

    master

    The Stroke protocol defines how input events (mouse or key) are converted between the C-structs expected by the interception driver and Python objects.

    Every Stroke implementation must provide:

    1. A format class variable (bytes) defining the struct packing format.
    2. A parse(cls, data: bytes) class method to unpack raw bytes into a Python object.
    3. A data property to pack the Python object back into raw bytes for driver communication.

    This pattern ensures type safety and IDE support while maintaining the ability to interface with low-level C structures.

  4. Important compatibility warning for Anti-Cheats

    master
    Some advanced anti-cheat software (such as Vanguard and certain versions of EAC) may prevent the system from booting or prevent games from launching if the interception-driver is loaded. This is because the driver is a well-known tool used for input manipulation.
  5. Send mouse and keyboard inputs

    master

    The library provides a simple interface for sending inputs, similar to pyautogui or pydirectinput.

    Common operations include:

    • move_to(x, y): Moves the mouse to specific coordinates.
    • click(x, y, button='left', delay=0): Clicks at coordinates with a specified button and delay.
    • press(key): Presses a key.
    • hold_key(key): A context manager to hold a key down (e.g., for modifiers like ctrl).
    import interception
    
    interception.auto_capture_devices()
    
    # Move mouse
    interception.move_to(960, 540)
    
    # Use modifiers with a context manager
    with interception.hold_key("ctrl"):
        interception.press("v")
    
    # Click with specific button and delay
    interception.click(120, 160, button="right", delay=1)
  6. Configure BezierCurveParams for smooth input movement

    master

    The BezierCurveParams dataclass allows you to customize the parameters used for generating smooth, human-like mouse movements via Bezier curves. You can use these parameters to control the complexity and randomness of the movement path.

    Fields:

    • knots: The number of knots in the curve (default: 2).
    • distortion_mean: The mean of the distortion applied to the curve (default: 1).
    • distortion_stdev: The standard deviation of the distortion (default: 1).
    • distortion_frequency: The frequency of the distortion (default: 0.5).
    • tween: An optional Callable used for tweening logic.
    • target_points: The number of points to generate for the curve (default: 100).
    from interception.beziercurve import BezierCurveParams
    
    params = BezierCurveParams(
        knots=3,
        distortion_mean=2,
        distortion_stdev=0.5,
        distortion_frequency=0.8,
        target_points=150
    )
  7. Dependency requirement: pyclick

    master

    The smooth movement functionality (specifically the HumanCurve class) depends on the pyclick library.

    If pyclick is not installed in your environment, interception will initialize a dummy HumanCurve class. Attempting to use this dummy class will result in a PyClickNotInstalled exception from the interception.exceptions module.

  8. Receive input strokes from a device

    master

    Use receive() to retrieve the next available input stroke from the device.

    Requirement: For receive() to work correctly, the device must be the result of a kernel32 WaitForMultipleObjects call to ensure there is valid input waiting to be read. If no stroke is available, the method returns None.

    stroke = device.receive()
    if stroke is not None:
        # stroke will be an instance of KeyStroke or MouseStroke
        pass
  9. Press keys and write text

    master

    Use press(key, presses, interval) to simulate pressing a keyboard key a specific number of times.

    Use write(term, interval) to type a string of characters sequentially. Note that uppercase characters are not currently supported; the input will be converted to lowercase.

    Note: For mouse buttons, use the click functions instead of press.

    # Press the 'a' key twice
    interception.press("a", presses=2)
    
    # Type a string
    interception.write("hello world", interval=0.05)
  10. Configure active mouse and keyboard indices

    master

    The Interception class allows you to set which device index is treated as the primary mouse or keyboard for your context. This is useful for logic that needs to know which device is currently being targeted.

    • mouse: Must be a valid mouse index (typically 10 <= num <= 19). Setting an invalid index raises a ValueError.
    • keyboard: Must be a valid keyboard index (typically 0 <= num <= 9). Setting an invalid index raises a ValueError.
  11. Interact with input devices using the Device class

    master

    The Device class represents a Windows I/O mouse or keyboard device. It allows you to receive input strokes (keystrokes or mouse movements) and send strokes back to the device.

    Important Lifecycle Note: You must call destroy() on a Device instance before it is destroyed to close the underlying Windows handles and prevent handle leakage.

    from ctypes.wintypes import HANDLE
    from interception.device import Device
    
    # Note: handle and event must be obtained via appropriate Windows API calls (e.g., CreateFileA)
    # is_keyboard should be True for keyboards, False for mice
    device = Device(handle=my_handle, event=my_event, is_keyboard=True)
    
    try:
        # Perform operations
        stroke = device.receive()
        if stroke:
            print(f"Received: {stroke}")
    finally:
        device.destroy()