docs.rs failed to build browser_input-0.2.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
🎮 browser_input
Ergonomic input handling for WebAssembly applications in the browser
A lightweight, efficient input handling library specifically designed for Rust WebAssembly applications. Provides a unified interface for capturing and processing keyboard, mouse, and other input events in web browsers.
✨ Features
🎯 Input Types
- ✅ Keyboard Input - Full keyboard event capture with modifier support
- ✅ Mouse Buttons - Left, right, middle mouse button handling
- ✅ Mouse Movement - Precise pointer position and movement tracking
- ❌ Mouse Wheel - Scroll events (planned)
🛠️ Core Capabilities
- Event Queue System - Buffered input events with modifier key states
- State Tracking - Real-time key and button state queries
- Modifier Support - Alt, Ctrl, Shift key combination detection
- Browser Integration - Direct DOM event binding with WebAssembly
- Memory Efficient - Minimal overhead for web deployment
🚀 Quick Start
Add to Your Project
[]
= { = true, = ["enabled"] }
Basic Input Loop
use *;
let mut input = new;
loop
Game Input Example
use *;
📚 API Reference
Core Types
Input
Main input manager struct:
Event
Input event with modifier keys:
EventType
Different types of input events:
Key Constants
Keyboard Keys
use KeyboardKey;
// Movement keys
KeyW
KeyA
KeyS
KeyD
// Arrow keys
ArrowUp
ArrowDown
ArrowLeft
ArrowRight
// Action keys
Space
Enter
Escape
Tab
// Number keys
Digit1
Digit2
// ... etc
Mouse Buttons
use MouseButton;
Left
Right
Middle
🎯 Usage Patterns
Real-Time Controls
For games and interactive applications requiring immediate response:
// Query current state directly
let move_up = input.is_key_down;
let move_down = input.is_key_down;
let shooting = input.is_key_down;
Event-Driven Actions
For UI interactions and discrete actions:
for event in input.event_queue.as_slice
Modifier Key Combinations
for Event in input.event_queue.as_slice
⚠️ Important Notes
Frame Management
- Always call
update_state()
at the beginning of your main loop - Always call
clear_events()
at the end of your main loop - Failure to clear events will cause the event queue to grow indefinitely
Canvas Binding
// Bind to specific canvas
let canvas = document.get_element_by_id
.unwrap
.
.unwrap;
let input = new;
// Bind to entire document (default)
let input = new;
Performance
- Event processing is O(n) where n is the number of events per frame
- State queries (
is_key_down
) are O(1) constant time - Memory usage scales with the number of simultaneous key presses
🤝 Contributing
This crate is part of the CGTools workspace. Feel free to submit issues and pull requests on GitHub.
📄 License
MIT