VirtualMouse.md

February 25, 2026 · View on GitHub

VirtualMouse Documentation

The VirtualMouse interface provides an easy way to simulate mouse inputs, including movement, scrolling, and button actions, using a virtual device. The VirtualMouseFactory is used to configure and create instances of VirtualMouse.


VirtualMouse

The VirtualMouse interface defines the core functionalities of a virtual mouse.

Methods

ActionDescription
RegisterRegisters the virtual mouse device with the system.
UnregisterUnregisters the virtual mouse device, releasing system resources.
MoveSimulates moving the mouse cursor by a relative amount in both X and Y directions.
MoveXSimulates moving the mouse cursor by a relative amount in the X direction.
MoveYSimulates moving the mouse cursor by a relative amount in the Y direction.
ScrollVerticalSimulates vertical scrolling. Positive values scroll up; negative values scroll down.
ScrollHorizontalSimulates horizontal scrolling. Positive values scroll right; negative values scroll left.
ButtonPressSimulates pressing a mouse button.
ButtonReleaseSimulates releasing a mouse button.
ScrollUpConvenience method to simulate a vertical scroll up.
ScrollDownConvenience method to simulate a vertical scroll down.
ScrollLeftConvenience method to simulate a horizontal scroll left.
ScrollRightConvenience method to simulate a horizontal scroll right.
ClickSimulates a single click of the specified button.
DoubleClickSimulates a double click of the specified button.
ClickLeftConvenience method to simulate a single left click.
ClickRightConvenience method to simulate a single right click.
ClickMiddleConvenience method to simulate a single middle click.
DoubleClickLeftConvenience method to simulate a double left click.
DoubleClickRightConvenience method to simulate a double right click.
DoubleClickMiddleConvenience method to simulate a double middle click.
SendSends a raw input event of the specified type, code, and value.
Custom Configuration

The behavior of a virtual mouse depends directly on its configuration. You can define and configure axes (REL_X, REL_Y or any other) with custom ranges, resolutions, and properties to suit your specific requirements.

For more detail, look the code of NewLogitechG402.


VirtualMouseFactory

The VirtualMouseFactory is used to configure and create instances of VirtualMouse. It supports method chaining for easy setup.

Methods

ActionDescription
WithDeviceAttaches an existing VirtualDevice to the mouse.
WithClickDelaySets the delay between press and release for a single click.
WithDoubleClickDelaySets the delay between two clicks for a double click.
WithHighResStepVerticalConfigures the step size for high-resolution vertical scrolling.
WithHighResStepHorizontalConfigures the step size for high-resolution horizontal scrolling.
CreateCreates an instance of VirtualMouse with the specified configuration.

Example Usage

Here’s how to configure and use a VirtualMouse:

package main

import (
	"fmt"
	virtual_device "github.com/jbdemonte/virtual-device"
	"github.com/jbdemonte/virtual-device/linux"
	"github.com/jbdemonte/virtual-device/mouse"
	"log"
)

func main() {
	m := mouse.NewVirtualMouseFactory().
		WithDevice(
			virtual_device.NewVirtualDevice().
				WithBusType(linux.BUS_USB).
				WithVendor(0xDEAD).
				WithProduct(0xBEEF).
				WithVersion(0x01).
				WithName("My Virtual Mouse").
				WithButtons([]linux.Button{
					linux.BTN_LEFT,
					linux.BTN_RIGHT,
					linux.BTN_MIDDLE,
				}).
				WithRelAxes([]linux.RelativeAxis{
					linux.REL_X,
					linux.REL_Y,
					linux.REL_WHEEL,
				}),
		).
		WithClickDelay(50).
		WithDoubleClickDelay(250).
		WithHighResStepVertical(120).
		WithHighResStepHorizontal(120).
		Create()

	err := m.Register()
	if err != nil {
		log.Fatalf("Failed to register virtual mouse: %v", err)
	}
	defer m.Unregister()

	m.Move(100, -50) // Move 100 units right and 50 units up

	// Simulate clicks
	m.ClickLeft()        // Single left click
	m.DoubleClickRight() // Double right click

	// Simulate scrolling
	m.ScrollUp()
	m.ScrollHorizontal(-1) // Scroll left
}

This documentation outlines the essential steps for configuring, registering, and using a VirtualMouse to simulate mouse inputs and control related features.