inputbot

repository·develop·Indexed 19 days ago

https://github.com/obv-mikhail/inputbot

A cross-platform Rust library for Windows and Linux used to simulate keyboard and mouse input events and register global input device event handlers. It provides functionality to bind keys and mouse buttons to closures, block input events, and send character sequences via KeySequence. Version 0.6.0.

Tokens
2.7K
Snippets
14
Records
15
Agent score
67%

What's inside inputbot

  1. Install build dependencies on Linux

    develop

    InputBot requires specific system libraries to function on Linux.

    Debian/Ubuntu based distros

    Install the following packages:

    • libx11-dev
    • libxtst-dev
    • libudev-dev
    • libinput-dev

    Running with sudo

    Because libinput is used, you must run your compiled binary with sudo on Linux:

    sudo ./target/debug/<program_name>
  2. Simulate input and bind global event handlers

    develop

    InputBot allows you to bind specific keys or mouse buttons to closures that execute when the input is detected. You can use KeySequence to simulate typing strings and press()/release() methods for mouse buttons.

    To start the event loop and begin listening for bound inputs, you must call inputbot::handle_input_events(false).

    use inputbot::{KeySequence, KeybdKey::*, MouseButton::*};
    use std::{thread::sleep, time::Duration};
    
    fn main() {
        // Bind the number 1 key to type a string
        Numrow1Key.bind(|| KeySequence("Hello, world!").send());
    
        // Bind CapsLock to an autoclicker loop
        CapsLockKey.bind(move || {
            while CapsLockKey.is_toggled() {
                LeftButton.press();
                LeftButton.release();
    
                sleep(Duration::from_millis(30));
            }
        });
    
        // Start listening for bound inputs
        inputbot::handle_input_events(false);
    }
  3. Run library examples

    develop

    To run the provided examples, clone the repository and use cargo run.

    On Linux: You must build the examples first and then run the resulting binary with sudo due to libinput requirements:

    cargo build --examples && sudo ./target/debug/<example_name>
    cargo run --example <example_name>
  4. Install InputBot

    develop

    Add inputbot to your Cargo.toml dependencies.

    Note: The documentation and examples are based on the develop branch. If features are missing or not working, you may be using the stable version from crates.io. To use the latest features from the develop branch, install directly from the GitHub repository.

    # Standard installation
    [dependencies]
    inputbot = "0.6"
    
    # To use the latest features from the develop branch
    [dependencies]
    inputbot = { git = "https://github.com/obv-mikhail/InputBot", branch = "develop" }
  5. Understand the Bind enum and handler types

    develop

    InputBot uses the Bind enum to define how different input events should be handled. There are four types of bindings:

    • Normal(Handler): A standard handler that executes when an input event occurs.
    • Release(Handler): (Windows only) A handler specifically for release events.
    • Block(BlockHandler): A handler used to block an input event.
    • Blockable(BlockableHandler): A handler that returns a BlockInput value to determine if the input should be blocked.

    Handlers are defined as thread-safe, static closures (Arc<dyn Fn() + Send + Sync + 'static>).

    // Example of the handler types used in Bind
    pub type Handler = Arc<dyn Fn() + Send + Sync + 'static>;
    pub type BlockHandler = Arc<dyn Fn() + Send + Sync + 'static>;
    pub type BlockableHandler = Arc<dyn Fn() -> BlockInput + Send + Sync + 'static>;
  6. Send a sequence of characters as keystrokes

    develop

    The KeySequence struct allows you to simulate typing a string of characters. It automatically handles case sensitivity by pressing and releasing KeybdKey::LShiftKey when uppercase characters or specific symbols are encountered. Each character is followed by a 20ms sleep to ensure reliable input simulation.

    use inputbot::KeySequence;
    
    // Simulates typing 'Hello!'
    let seq = KeySequence("Hello!");
    seq.send();
  7. Bind keyboard keys to callbacks

    develop

    You can bind specific KeybdKey variants to closures to execute code when that key is pressed.

    Available binding methods:

    • bind(callback): Executes the callback on key press.
    • block_bind(callback): Executes the callback and blocks the input from reaching the system.
    • blockable_bind(callback): Executes a callback that returns a BlockInput enum (Block or DontBlock), allowing you to dynamically decide whether to block the input.

    On Windows, you can also use bind_release(callback) to trigger a callback when a key is released.

    use inputbot::KeybdKey;
    
    // Standard bind
    KeybdKey::AKey.bind(|| {
        println!("A key was pressed!");
    });
    
    // Blocking bind
    KeybdKey::EscapeKey.block_bind(|| {
        println!("Escape pressed, blocking input.");
    });
    
    // Dynamic blocking bind
    KeybdKey::SpaceKey.blockable_bind(|| {
        // Return Block to stop the space from being typed, or DontBlock to let it through
        inputbot::BlockInput::Block
    });
  8. Manage key and button bindings

    develop

    You can check if a key or button is currently bound or remove an existing binding using the following methods on KeybdKey and MouseButton:

    • is_bound(): Returns true if the key/button has an active binding.
    • unbind(): Removes the binding for the key/button.
    use inputbot::KeybdKey;
    
    if KeybdKey::EnterKey.is_bound() {
        KeybdKey::EnterKey.unbind();
    }
  9. Bind all keyboard keys to a callback

    develop

    Use bind_all(callback) to register a single callback for every supported KeybdKey. The callback receives the KeybdKey that triggered the event as an argument.

    use inputbot::KeybdKey;
    
    KeybdKey::bind_all(|key| {
        println!("Key pressed: {:?}", key);
    });
  10. Bind mouse buttons to callbacks

    develop

    You can bind MouseButton variants to closures.

    Available binding methods:

    • bind(callback): Executes the callback on button press.
    • block_bind(callback): Executes the callback and blocks the mouse input.
    • blockable_bind(callback): Executes a callback that returns a BlockInput enum (Block or DontBlock).

    On Windows, you can also use bind_release(callback) to trigger a callback when a button is released.

    use inputbot::MouseButton;
    
    // Standard bind
    MouseButton::LeftButton.bind(|| {
        println!("Left click!");
    });
    
    // Blocking bind
    MouseButton::RightButton.block_bind(|| {
        println!("Right click blocked!");
    });
  11. Control the event loop with should_continue()

    develop

    The should_continue(auto_stop: bool) function determines whether the input event loop should keep running.

    • If auto_stop is false, the loop continues as long as HANDLE_EVENTS is true.
    • If auto_stop is true, the loop will automatically stop if there are no active keyboard or mouse bindings (including release bindings) registered in the global maps.
    // Returns true if the event loop should keep running
    if should_continue(true) {
        // process events
    }