A high-level Rust library for sending mouse and keyboard messages to Windows applications using the windows-api-utils crate. This library provides an easy-to-use interface for automating window interactions with configurable delays and error handling.
- 🖱️ Mouse Events: Send mouse clicks, moves, and double clicks
- ⌨️ Keyboard Input: Send key presses, combinations, and strings
- ⏱️ Configurable Delays: Global delay settings and per-operation custom delays
- 🎯 Flexible Targeting: Send messages to both focused and non-focused windows
- đź”§ Message Types: Choose between
SendMessage(synchronous) andPostMessage(asynchronous) - âś… Error Handling: Comprehensive error types and validation
- 🛡️ Safe Wrapper: Safe Rust bindings around Windows API
Add this to your Cargo.toml:
[dependencies]
window-message-sender = "0.1.0"use window_message_sender::{WindowController, MessageType};
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let hwnd: isize = 123456; // Your window handle
let mut controller = WindowController::new(hwnd);
// Send to non-focused window using PostMessage (asynchronous)
controller.set_message_type(MessageType::PostMessage)
.send_left_click(100, 100)?;
// Send to focused window using SendMessage (synchronous)
controller.set_message_type(MessageType::SendMessage)
.send_string("Hello, World!")?;
Ok(())
}use window_message_sender::{WindowController, MessageType, MouseButton};
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let hwnd: isize = 123456;
let mut controller = WindowController::new(hwnd);
// Set global delay
controller.set_delay_ms(100); // 100ms between operations
// Or disable delay entirely
controller.disable_delay();
// Use custom delays for specific operations
controller.send_string_with_delay("Fast typing", Some(Duration::from_millis(10)))?;
// Custom click delay
controller.send_mouse_click_with_delay(100, 100, MouseButton::Left, Some(Duration::from_millis(200)))?;
Ok(())
}send_mouse_move(x, y)- Move mouse to coordinatessend_left_click(x, y)- Left clicksend_right_click(x, y)- Right clicksend_middle_click(x, y)- Middle clicksend_double_click(x, y, button)- Double clicksend_mouse_click_with_delay(x, y, button, delay)- Click with custom delay
send_key(key)- Press and release a keysend_key_press(key)- Press key (without release)send_key_release(key)- Release keysend_combination(modifier, main_key)- Send key combination (e.g., Ctrl+C)send_string(text)- Type a stringsend_string_with_delay(text, delay)- Type with custom delay between characters
send_copy()- Ctrl+Csend_paste()- Ctrl+Vsend_cut()- Ctrl+Xsend_select_all()- Ctrl+Asend_undo()- Ctrl+Zsend_redo()- Ctrl+Ysend_save()- Ctrl+Ssend_alt_f4()- Alt+F4
set_delay(duration)- Set global delay between operationsset_delay_ms(milliseconds)- Set delay in millisecondsdisable_delay()- Disable all delaysset_message_type(type)- Choose SendMessage or PostMessageset_require_focus(require)- Require window focus for operations
The library supports a comprehensive set of virtual keys:
- Letters: A-Z
- Numbers: 0-9
- Function Keys: F1-F12
- Control Keys: Enter, Escape, Backspace, Tab, Space, Delete
- Modifier Keys: Shift, Control, Alt
- Arrow Keys: Up, Down, Left, Right
- Special Keys: Home, End, PageUp, PageDown, Insert
The library uses the WindowMessageError enum for comprehensive error handling:
InvalidWindow- Invalid window handleSendFailed- Failed to send messagePostFailed- Failed to post messageWindowNotFocused- Window requires focus but is not focusedUnsupportedCharacter- Character not supported for typingTimeout- Message timeoutInvalidCoordinates- Invalid mouse coordinatesOperationNotSupported- Operation not supported
- Waits for the message to be processed
- Returns the result immediately
- More reliable but may block
- Places the message in the queue and returns immediately
- Non-blocking but less reliable
- Better for performance
use window_message_sender::{WindowController, VirtualKey};
fn fill_form(controller: &WindowController) -> Result<(), Box<dyn std::error::Error>> {
// Click on name field
controller.send_left_click(100, 150)?;
// Type name
controller.send_string("John Doe")?;
// Tab to next field
controller.send_key(VirtualKey::Tab)?;
// Type email
controller.send_string("john@example.com")?;
// Submit form (Enter)
controller.send_key(VirtualKey::Enter)?;
Ok(())
}use window_message_sender::{WindowController, VirtualKey};
use std::time::Duration;
fn game_macro(controller: &WindowController) -> Result<(), Box<dyn std::error::Error>> {
// Set fast delays for gaming
controller.set_delay_ms(50);
loop {
// Attack combo
controller.send_key(VirtualKey::Q)?;
controller.send_key(VirtualKey::W)?;
controller.send_key(VirtualKey::E)?;
// Use item
controller.send_key(VirtualKey::Num1)?;
// Wait for cooldown
std::thread::sleep(Duration::from_secs(2));
}
}- Windows 7 or later
- Rust 1.70 or later
- Appropriate permissions for window manipulation
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
Contributions are welcome! Please feel free to submit issues and pull requests.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
For detailed API documentation, visit: https://docs.rs/window-message-sender