Skip to main content

KeyInjector

Trait KeyInjector 

Source
pub trait KeyInjector: Send {
    // Required methods
    fn type_text(
        &mut self,
        text: &str,
        gate: &mut dyn FnMut(usize) -> ControlFlow<()>,
    ) -> Result<Typed>;
    fn backspace(&mut self, count: usize) -> Result<()>;
    fn send_combo(&mut self, keys: &[Key]) -> Result<()>;
    fn set_key_delay(&mut self, delay: Duration);
}
Expand description

High-level text-injection abstraction.

This unifies the available keystroke backends behind a single trait so a caller can pick an implementation at runtime (e.g. the layout-independent Wayland [wayland_vk::WaylandVkKeyboard] when the compositor supports zwp_virtual_keyboard_v1, falling back to the uinput Keyboard otherwise) without changing the typing code.

The semantics mirror Keyboard’s inherent methods, except that type_text takes a gate.

Required Methods§

Source

fn type_text( &mut self, text: &str, gate: &mut dyn FnMut(usize) -> ControlFlow<()>, ) -> Result<Typed>

Type text by injecting the corresponding keystrokes, asking gate before each one.

gate gets the byte offset in text of the next character to send. It is asked before every key tap, and once before each clipboard paste of a run of characters the keymap lacks (with the offset of the run’s first character). A character typed as two taps (a dead key and its base letter) is asked about once and never split. A character the backend cannot type at all is skipped without asking.

ControlFlow::Continue sends the character. ControlFlow::Break stops before it: what was already sent is flushed, and the result is Typed::StoppedAt that offset. The gate may block, for example until the user lets go of a modifier key. Pass &mut |_| ControlFlow::Continue(()) to type everything.

Source

fn backspace(&mut self, count: usize) -> Result<()>

Emit Backspace count times.

Source

fn send_combo(&mut self, keys: &[Key]) -> Result<()>

Press all keys in keys, then release them in reverse order.

Source

fn set_key_delay(&mut self, delay: Duration)

Set the inter-event delay used between injected key events.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§