Mouse
Mouse state singleton for position and button state.
Overview
Mouse is a singleton object that provides real-time access to mouse state including position, button states, and cursor properties. Use it to poll mouse state outside of event callbacks or to control cursor visibility and grabbing.
Quick Reference
# Check mouse position
x, y = mcrfpy.mouse.pos
print(f"Mouse at ({x}, {y})")
# Check button states
if mcrfpy.mouse.left:
print("Left button is held")
# Hide cursor for custom cursor sprite
mcrfpy.mouse.visible = False
# Grab mouse for camera control
mcrfpy.mouse.grabbed = True
Properties
Position
| Property |
Type |
Description |
pos |
Vector |
Current mouse position as Vector (read-only) |
x |
int |
Current mouse X position (read-only) |
y |
int |
Current mouse Y position (read-only) |
| Property |
Type |
Description |
left |
bool |
True if left button is held (read-only) |
middle |
bool |
True if middle button is held (read-only) |
right |
bool |
True if right button is held (read-only) |
Cursor Control
| Property |
Type |
Description |
visible |
bool |
Show/hide system cursor (read-write) |
grabbed |
bool |
Lock cursor to window (read-write) |
Usage Patterns
Custom Cursor
import mcrfpy
scene = mcrfpy.Scene("custom_cursor")
mcrfpy.current_scene = scene
# Create a custom cursor sprite (uses the default texture atlas)
cursor = mcrfpy.Sprite(pos=(0, 0), sprite_index=1)
scene.children.append(cursor)
# Hide the system cursor
mcrfpy.mouse.visible = False
# Update the cursor's position every frame via a Timer
def update(timer, runtime):
pos = mcrfpy.mouse.pos
cursor.x, cursor.y = pos.x, pos.y
mcrfpy.Timer("cursor_follow", update, 16)