Basic Mouse Events
The simplest mouse example fromexamples/mouse/main.go shows how to capture mouse coordinates:
Key Concepts
Enabling Mouse Mode
Set the mouse mode in yourView() method:
Available mouse modes:
tea.MouseModeNone- Disable mouse eventstea.MouseModeClick- Only click eventstea.MouseModeMotion- Clicks and motion when button pressedtea.MouseModeAllMotion- All mouse events including hover
Handling Mouse Messages
Mouse events are delivered astea.MouseMsg:
Mouse Message Types
Check the specific mouse event type:Advanced: Clickable Interface
Theexamples/clickable directory contains a sophisticated example with draggable dialog boxes. Here are the key patterns:
Layer-Based Hit Detection
Use Lip Gloss layers to detect which element was clicked:Drag and Drop Implementation
Track mouse state for drag operations:Hover States
Track which element is being hovered:Z-Index Management
Bring clicked elements to front:Mouse Button Types
Common Patterns
Click Detection
Region-Based Detection
Best Practices
Use Layers
Use Lip Gloss layers and compositor for accurate hit detection
Track State
Maintain mouseDown state to distinguish clicks from releases
Visual Feedback
Show hover and active states for better UX
Bounds Checking
Clamp positions when dragging to keep elements on screen
Running the Examples
- Click background to spawn dialog boxes
- Drag dialogs to move them
- Click buttons to close dialogs
- See hover effects
Source Code
- examples/mouse - Basic mouse events
- examples/clickable - Advanced drag-and-drop interface