Skip to content

A high-level wrapper for sending mouse and keyboard messages to Windows using windows-api-utils

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE
Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

YMC-GitHub/window-message-sender

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Window Message Sender

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.

Features

  • 🖱️ 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) and PostMessage (asynchronous)
  • âś… Error Handling: Comprehensive error types and validation
  • 🛡️ Safe Wrapper: Safe Rust bindings around Windows API

Installation

Add this to your Cargo.toml:

[dependencies]
window-message-sender = "0.1.0"

Quick Start

Basic Usage

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(())
}

Advanced Usage with Custom Delays

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(())
}

API Overview

Mouse Operations

  • send_mouse_move(x, y) - Move mouse to coordinates
  • send_left_click(x, y) - Left click
  • send_right_click(x, y) - Right click
  • send_middle_click(x, y) - Middle click
  • send_double_click(x, y, button) - Double click
  • send_mouse_click_with_delay(x, y, button, delay) - Click with custom delay

Keyboard Operations

  • send_key(key) - Press and release a key
  • send_key_press(key) - Press key (without release)
  • send_key_release(key) - Release key
  • send_combination(modifier, main_key) - Send key combination (e.g., Ctrl+C)
  • send_string(text) - Type a string
  • send_string_with_delay(text, delay) - Type with custom delay between characters

Common Shortcuts

  • send_copy() - Ctrl+C
  • send_paste() - Ctrl+V
  • send_cut() - Ctrl+X
  • send_select_all() - Ctrl+A
  • send_undo() - Ctrl+Z
  • send_redo() - Ctrl+Y
  • send_save() - Ctrl+S
  • send_alt_f4() - Alt+F4

Configuration

  • set_delay(duration) - Set global delay between operations
  • set_delay_ms(milliseconds) - Set delay in milliseconds
  • disable_delay() - Disable all delays
  • set_message_type(type) - Choose SendMessage or PostMessage
  • set_require_focus(require) - Require window focus for operations

Supported Virtual Keys

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

Error Handling

The library uses the WindowMessageError enum for comprehensive error handling:

  • InvalidWindow - Invalid window handle
  • SendFailed - Failed to send message
  • PostFailed - Failed to post message
  • WindowNotFocused - Window requires focus but is not focused
  • UnsupportedCharacter - Character not supported for typing
  • Timeout - Message timeout
  • InvalidCoordinates - Invalid mouse coordinates
  • OperationNotSupported - Operation not supported

Message Types

SendMessage (Synchronous)

  • Waits for the message to be processed
  • Returns the result immediately
  • More reliable but may block

PostMessage (Asynchronous)

  • Places the message in the queue and returns immediately
  • Non-blocking but less reliable
  • Better for performance

Examples

Automated Form Filling

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(())
}

Game Automation

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));
    }
}

Requirements

  • Windows 7 or later
  • Rust 1.70 or later
  • Appropriate permissions for window manipulation

License

Licensed under either of:

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Documentation

For detailed API documentation, visit: https://docs.rs/window-message-sender

About

A high-level wrapper for sending mouse and keyboard messages to Windows using windows-api-utils

Resources

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE
Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages