Python HID Mouse

Introduction

Human Interface Device (HID) is a type of device that allows humans to interact with computers. It includes devices such as keyboards, mice, game controllers, etc. In this article, we will focus on creating a simple virtual HID mouse using Python.

Requirements

To create a Python HID mouse, we need the following requirements:

  1. Python programming language
  2. PyUSB library - a Python interface for USB devices
  3. HID descriptor - a data structure that describes the HID device

Creating a Virtual HID Mouse

Step 1: Installing PyUSB

To install the PyUSB library, open a terminal and run the following command:

pip install pyusb

Step 2: USB Device Identification

Before we start interacting with the HID device, we need to identify the USB device details. We can use the lsusb command in Linux or the Device Manager in Windows to get the vendor ID (VID) and product ID (PID) of the HID mouse.

Step 3: HID Descriptor

The HID descriptor is a data structure that describes the HID device. It contains information about the device's capabilities, such as the number of buttons, axes, etc. We need to define this descriptor in our Python code.

Here is an example HID descriptor for a mouse:

HID_DESCRIPTOR = {
    'bLength': 9,
    'bDescriptorType': 0x21,
    'bDescriptorSubType': 0x01,
    'bcdHID': 0x0111,
    'bCountryCode': 0x00,
    'bNumDescriptors': 0x01,
    'bDescriptorType2': 0x22,
    'wDescriptorLength': 0x34
}

Step 4: Mouse Movement

To simulate mouse movement, we can use the PyUSB library to send HID reports to the USB device. Each report consists of a byte array that represents the state of the mouse buttons and the relative movement of the cursor.

Here is an example code that moves the mouse cursor to the right:

import usb.core

def move_mouse_right(device):
    report = [0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]
    device.ctrl_transfer(0x21, 0x09, 0x0200, 0, report)

In this code, device is an instance of the USB device that we want to send the report to. The ctrl_transfer method is used to send the report.

Step 5: Mouse Button Click

To simulate a mouse button click, we can modify the report to set the corresponding button state.

Here is an example code that performs a left button click:

def click_left_button(device):
    report = [0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]
    device.ctrl_transfer(0x21, 0x09, 0x0200, 0, report)

In this code, the third element of the report is set to 0x01 to indicate that the left button is pressed.

Step 6: Putting It All Together

Now let's put all the pieces together and create a virtual HID mouse.

import usb.core

HID_DESCRIPTOR = {
    'bLength': 9,
    'bDescriptorType': 0x21,
    'bDescriptorSubType': 0x01,
    'bcdHID': 0x0111,
    'bCountryCode': 0x00,
    'bNumDescriptors': 0x01,
    'bDescriptorType2': 0x22,
    'wDescriptorLength': 0x34
}

def move_mouse_right(device):
    report = [0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]
    device.ctrl_transfer(0x21, 0x09, 0x0200, 0, report)

def click_left_button(device):
    report = [0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]
    device.ctrl_transfer(0x21, 0x09, 0x0200, 0, report)

# Find the USB device
device = usb.core.find(idVendor=0x1234, idProduct=0x5678)

# Set the HID descriptor
device.set_configuration()
device.ctrl_transfer(0x21, 0x09, 0x0200, 0, list(HID_DESCRIPTOR.values()))

# Move the mouse right
move_mouse_right(device)

# Click the left button
click_left_button(device)

In this code, we first find the USB device using the vendor ID and product ID. Then we set the HID descriptor using the set_configuration and `ctrl