Mouse Helper Classο
The Mouse class is an internal helper that handles the low-level mouse operations for the MakcuController. While you typically wonβt use this class directly, understanding it can help with advanced usage and troubleshooting.
Class Overviewο
The Mouse class provides:
Raw Command Generation: Creates the actual device commands
State Management: Tracks button states and lock status
Protocol Handling: Formats commands for the Makcu device
Response Parsing: Interprets device responses
Note: You typically donβt create Mouse instances directly. The MakcuController creates and manages one for you.
Internal Architectureο
The Mouse class works as a bridge between your Python code and the Makcu deviceβs protocol:
Your Code β MakcuController β Mouse β SerialTransport β Device
makcu.click() β mouse.click() β "km.click(1)" β Device
This layered approach provides: - Clean separation of concerns - Easy testing and debugging - Protocol abstraction - State management
Core Operationsο
Click Operationsο
The click method generates commands like:
# Internal command generation:
mouse.click(MouseButton.LEFT) # β "km.click(1)"
mouse.click(MouseButton.RIGHT) # β "km.click(2)"
mouse.click(MouseButton.MIDDLE) # β "km.click(4)"
# Press/release generate separate commands:
mouse.press(MouseButton.LEFT) # β "km.press(1)"
mouse.release(MouseButton.LEFT) # β "km.release(1)"
Movement Operationsο
# Movement commands:
mouse.move(100, 50) # β "km.move(100,50)"
mouse.move(-25, 0) # β "km.move(-25,0)"
# Scroll commands:
mouse.scroll(-3) # β "km.scroll(-3)"
mouse.scroll(2) # β "km.scroll(2)"
State Managementο
Lock State Managementο
The locking system prevents buttons/axes from functioning:
# Lock commands:
mouse.lock(MouseButton.LEFT) # β "km.lock.button(1)"
mouse.lock("X") # β "km.lock.axis(1)"
mouse.lock("Y") # β "km.lock.axis(2)"
# Query lock state:
mouse.is_locked(MouseButton.LEFT) # β "km.lock.query(1)"
mouse.is_locked("X") # β "km.lock.query.axis(1)"
# Batch query for performance:
locks = mouse.get_all_lock_states()
# β "km.lock.query.all()" β parsing response
Protocol Detailsο
Command Formatο
The Mouse class generates commands in the Makcu protocol format:
Basic Commands: .. code-block:: text
km.click(button) # Click button (1=LEFT, 2=RIGHT, 4=MIDDLE, etc.) km.press(button) # Press and hold button km.release(button) # Release button km.move(x,y) # Move mouse relatively km.scroll(delta) # Scroll wheel
State Commands: .. code-block:: text
km.button.state() # Get button states (returns bitmask) km.button.query(btn) # Check if button is pressed
Lock Commands: .. code-block:: text
km.lock.button(btn) # Lock button km.unlock.button(btn) # Unlock button km.lock.axis(axis) # Lock movement axis (1=X, 2=Y) km.lock.query.all() # Get all lock states
Response Parsingο
The Mouse class parses device responses:
# Example response parsing:
raw_response = ">>> button_state:7" # Bitmask: 7 = LEFT+RIGHT+MIDDLE
# Parsed into:
parsed_states = {
"LEFT": True, # Bit 0 set
"RIGHT": True, # Bit 1 set
"MIDDLE": True, # Bit 2 set
"MOUSE4": False, # Bit 3 not set
"MOUSE5": False # Bit 4 not set
}
Advanced Featuresο
Batch Operationsο
# Execute multiple operations efficiently:
mouse.batch_execute([
lambda: mouse.move(50, 0),
lambda: mouse.click(MouseButton.LEFT),
lambda: mouse.scroll(-1)
])
Human-Like Timingο
The Mouse class includes timing profiles for realistic behavior:
# Timing profiles (internal delays):
PROFILES = {
"gaming": (0.001, 0.003), # 1-3ms between clicks
"fast": (0.02, 0.08), # 20-80ms
"normal": (0.1, 0.3), # 100-300ms
"slow": (0.3, 0.8), # 300-800ms
"variable": (0.05, 0.5) # 50-500ms (random)
}
Error Handlingο
The Mouse class handles various error conditions:
# Common error scenarios:
try:
mouse.click(MouseButton.LEFT)
except Exception as e:
# Device communication errors
# Invalid button parameters
# Timeout errors
# Connection lost errors
pass
Internal State Propertiesο
The Mouse class maintains several internal properties:
# Button states (cached for performance)
mouse._button_states = 0 # Bitmask of pressed buttons
# Lock states (cached)
mouse._lock_states = {
"LEFT": False, "RIGHT": False, "MIDDLE": False,
"MOUSE4": False, "MOUSE5": False,
"X": False, "Y": False
}
# Transport reference
mouse.transport = serial_transport_instance
Performance Optimizationsο
The Mouse class includes several performance optimizations:
Pre-computed Commands: .. code-block:: python
# Commands are pre-formatted for speed: CLICK_COMMANDS = {
MouseButton.LEFT: βkm.click(1)β, MouseButton.RIGHT: βkm.click(2)β, # β¦ etc
}
State Caching: .. code-block:: python
# Button states cached to avoid repeated queries: if not self._state_cache_expired():
return self._cached_button_states
Bitwise Operations: .. code-block:: python
# Fast button state checking: def is_pressed(self, button):
mask = BUTTON_MAPPING[button] return bool(self._button_states & mask)
Usage Examplesο
Direct Usage (Advanced):
# You typically don't do this - use MakcuController instead
from makcu.mouse import Mouse
from makcu.connection import SerialTransport
transport = SerialTransport("COM3")
mouse = Mouse(transport)
# Direct mouse operations:
mouse.click(MouseButton.LEFT)
mouse.move(100, 50)
Accessing via MakcuController:
from makcu import create_controller, MouseButton
makcu = create_controller()
# Access the internal mouse instance:
internal_mouse = makcu.mouse
# Now you can call mouse methods directly if needed:
internal_mouse.click(MouseButton.LEFT)
Debuggingο
The Mouse class supports debug output:
# Debug output shows command generation:
mouse.debug = True
mouse.click(MouseButton.LEFT)
# Output:
# [DEBUG] Generated command: km.click(1)
# [DEBUG] Sending to transport...
# [DEBUG] Response received: >>> clicked
Thread Safetyο
The Mouse class is not thread-safe. It maintains internal state that can be corrupted by concurrent access. Always use proper synchronization if accessing from multiple threads.
Integration with Transport Layerο
The Mouse class works closely with the SerialTransport:
# Command flow:
mouse.click(MouseButton.LEFT)
β
command = "km.click(1)" # Generated by Mouse
β
transport.send_command(command) # Sent via SerialTransport
β
response = ">>> clicked" # Received from device
β
mouse.parse_response(response) # Processed by Mouse
Limitationsο
Synchronous Only: The Mouse class is designed for synchronous operations. For async usage, see the AsyncMouse class.
Single Device: Each Mouse instance works with one device connection.
State Timing: Button state queries may have slight delays (~1ms) due to device communication.
See Alsoο
MakcuController API Reference - Main controller that uses this class
Connection and Transport API Reference - Transport layer details
Async Usage - Async version of mouse operations
Examples - Practical usage examples
Enums and Constants - MouseButton enumeration details