Linux permission requirements
mainsudo) are required to use enigo on Linux.repository·main·Indexed 23 days ago
https://github.com/enigo-rs/enigoA cross-platform Rust library for simulating keyboard and mouse events on Windows, macOS, and Linux (X11, Wayland, and libei). It provides traits for keyboard and mouse control, allowing developers to programmatically move the cursor, click buttons, type text, and send raw keycodes. The library includes a Token enum and Agent trait for orchestrating input sequences and supports serialization via the serde feature.
sudo) are required to use enigo on Linux.To use Enigo, initialize an Enigo instance with Settings::default(). You can then perform mouse movements using absolute or relative coordinates, trigger mouse buttons, and type text strings. Note that most methods return a Result, so you should handle potential errors with .unwrap() or proper error handling.
let mut enigo = Enigo::new(&Settings::default()).unwrap();
enigo.move_mouse(500, 200, Abs).unwrap();
enigo.button(Button::Left, Click).unwrap();
enigo.text("Hello World! here is a lot of text ❤️").unwrap();If you are using the xdo feature on Linux, you must install the corresponding development libraries for your distribution:
apt install libxdo-devpacman -S xdotooldnf install libX11-devel libxdo-develemerge -a xdotoolEnigo uses the log crate for internal messaging. To see debug output, you must use a logging implementation (like env_logger used in the project's examples) in your application. You can control the verbosity using the RUST_LOG environment variable.
To see all debug messages, set RUST_LOG=debug. To limit output to a specific module, use the format RUST_LOG=module_path=level (e.g., RUST_LOG=enigo::platform::x11=debug).
To inspect X11 messages, you must use a proxy server. The recommended method is using the xtrace-example from the x11rb crate.
x11rb repository.x11rb/xtrace-example.Example usage pattern:
/path/to/xtrace-example cargo run --example keyboard --features x11rbgit clone https://github.com/psychon/x11rb.git
cd x11rb/xtrace-example
cargo run
# Follow the output instructions to run your specific command through the proxyWarning: Enigo tests actively control your mouse, keyboard, and applications. To prevent tests from interfering with each other or your system, run them sequentially using a single thread. It is recommended to close all other applications before running tests.
cargo test --all-features -- --test-threads=1On Linux, you can inspect the messages exchanged between Enigo and the compositor by setting specific environment variables depending on the protocol in use:
REIS_DEBUG=1.WAYLAND_DEBUG=1.On macOS, applications require explicit user permission to access accessibility features. enigo automatically checks for these permissions and will prompt the user to grant them if they are missing.
Users can manually grant these permissions via System Settings. You can also customize how enigo handles permission checks by adjusting the settings when initializing the Enigo struct.
Enigo's default configuration supports Windows, macOS, and Linux (X11). You can enable additional capabilities via Cargo features:
serde: Enables serialization and deserialization of commands.libei support are currently experimental and are hidden behind feature flags.The Key enum represents various keyboard keys used for input simulation.
If a specific key is not present in the Key enum, you can simulate it using:
Key::Unicode(char): To enter arbitrary Unicode characters.Key::Other(u32): To provide a raw value. The resulting value depends on your platform:keysym.Virtual_Key.KeyCode.crate::Keyboard::raw: An alternative method for raw input.Many keys are only available on specific platforms (e.g., certain Windows-only keys or macOS-only brightness keys). Use conditional compilation (#[cfg(target_os = "...")]) when referencing platform-specific variants to ensure your code compiles across different operating systems.
Enigo allows you to simulate mouse and keyboard events as if they were made by actual hardware. To use it, you primarily interact with the Enigo struct, which implements the Keyboard and Mouse traits.
To get started, create a new instance of Enigo using Enigo::new(&Settings::default()). You can then use its methods to press keys, type text, move the mouse, or click buttons.
use enigo::{
Button, Coordinate,
Direction::{Click, Press, Release},
Enigo, Key, Keyboard, Mouse, Settings,
};
let mut enigo = Enigo::new(&Settings::default()).unwrap();
// Paste
enigo.key(Key::Control, Press);
enigo.key(Key::Unicode('v'), Click);
enigo.key(Key::Control, Release);
// Do things with the mouse
enigo.move_mouse(500, 200, Coordinate::Abs);
enigo.button(Button::Left, Press);
enigo.move_mouse(100, 100, Coordinate::Rel);
enigo.button(Button::Left, Release);
// Enter text
enigo.text("hello world");enigo will fail to interact with those specific processes.