Crate afrim_preprocessor

source ·
Expand description

Preprocess keyboard events for an input method.

Enables the generation of keyboard event responses from a keyboard input event in an input method engine. The afrim-preprocessor crate is built on the top of the afrim-memory crate.

§Example

use afrim_preprocessor::{utils, Command, Preprocessor};
use keyboard_types::{
    webdriver::{self, Event},
    Key::*,
};
use std::{collections::VecDeque, rc::Rc};

// Prepares the memory.
let data = utils::load_data("cc ç");
let text_buffer = utils::build_map(data);
let memory = Rc::new(text_buffer);

// Builds the preprocessor.
let mut preprocessor = Preprocessor::new(memory, 8);

// Process an input.
let input = "cc";
webdriver::send_keys(input)
    .into_iter()
    .for_each(|event| {
        match event {
            // Triggers the generated keyboard input event.
            Event::Keyboard(event) => preprocessor.process(event),
            _ => unimplemented!(),
        };
    });

// Now let's look at the generated commands.
// The expected results without `inhibit` feature.
#[cfg(not(feature = "inhibit"))]
let mut expecteds = VecDeque::from(vec![
    Command::Pause,
    Command::Delete,
    Command::Delete,
    Command::CommitText("ç".to_owned()),
    Command::Resume,
]);

// The expected results with `inhibit` feature.
#[cfg(feature = "inhibit")]
let mut expecteds = VecDeque::from(vec![
    Command::Pause,
    Command::Delete,
    Command::Resume,
    Command::Pause,
    Command::Delete,
    Command::CommitText("ç".to_owned()),
    Command::Resume,
]);

// Verification.
while let Some(command) = preprocessor.pop_queue() {
    assert_eq!(command, expecteds.pop_front().unwrap());
}

Note: When dealing with non latin languages. The inhibit feature allows for the removal of unwanted characters typically latin characters, as much as posssible.

Modules§

  • Set of tools to facilitate the loading of data.

Structs§

  • Keyboard events are issued for all pressed and released keys.
  • The main structure of the preprocessor.

Enums§

  • Possible commands that can be generated by the afrim-preprocessor.
  • Key represents the meaning of a keypress.
  • Describes the state the key is in.