Struct Triggers

Source
pub struct Triggers { /* private fields */ }
Expand description

Data structure containing the triggers registered for all the keyboards. Once triggers have been registered, use the Triggers::listen method to enable the detection.

Implementations§

Source§

impl Triggers

Source

pub fn insert(&mut self, triggers: KeyboardTriggers)

Registers all the triggers for a specific keyboard, overriding previous definitions.

Examples found in repository?
examples/basic.rs (line 41)
5fn main() {
6    std::thread::spawn(|| {
7        // Security in case devices were grabbed unintentionally
8        // to avoid being locked out of the computer
9        std::thread::sleep(Duration::from_secs(120));
10        std::process::exit(1);
11    });
12
13    // Obtain the name of the keyboard
14    let mut keyboard_name = String::new();
15    print!("Please provide the name of your macro keyboard: ");
16    std::io::stdout().flush().expect("failed to flush stdout");
17    std::io::stdin()
18        .read_line(&mut keyboard_name)
19        .expect("failed to read keyboard name");
20
21    // Register the keyboard triggers
22    let mut keyboard_triggers = KeyboardTriggers::new(keyboard_name.trim());
23
24    keyboard_triggers.insert_with_release(
25        &[KeyMappingCode::ControlLeft, KeyMappingCode::KeyE],
26        || println!("Ran combination!"),
27        || println!("Released combination!"),
28    );
29
30    keyboard_triggers.insert_with_release(
31        &[KeyMappingCode::KeyR],
32        || println!("Trigger start!"),
33        || println!("Trigger stop!"),
34    );
35
36    keyboard_triggers.insert(&[KeyMappingCode::KeyE], || println!("Hello!"));
37
38    keyboard_triggers.insert(&[KeyMappingCode::Escape], || ListeningCmd::Stop);
39
40    let mut triggers = Triggers::default();
41    triggers.insert(keyboard_triggers);
42
43    // Start the event loop
44    triggers.listen();
45
46    println!("Listener gracefully stopped.");
47}
Source

pub fn listen(self)

Blocks the thread to detect inputs for the configured keyboards and run the actions associated to key combinations.

Examples found in repository?
examples/basic.rs (line 44)
5fn main() {
6    std::thread::spawn(|| {
7        // Security in case devices were grabbed unintentionally
8        // to avoid being locked out of the computer
9        std::thread::sleep(Duration::from_secs(120));
10        std::process::exit(1);
11    });
12
13    // Obtain the name of the keyboard
14    let mut keyboard_name = String::new();
15    print!("Please provide the name of your macro keyboard: ");
16    std::io::stdout().flush().expect("failed to flush stdout");
17    std::io::stdin()
18        .read_line(&mut keyboard_name)
19        .expect("failed to read keyboard name");
20
21    // Register the keyboard triggers
22    let mut keyboard_triggers = KeyboardTriggers::new(keyboard_name.trim());
23
24    keyboard_triggers.insert_with_release(
25        &[KeyMappingCode::ControlLeft, KeyMappingCode::KeyE],
26        || println!("Ran combination!"),
27        || println!("Released combination!"),
28    );
29
30    keyboard_triggers.insert_with_release(
31        &[KeyMappingCode::KeyR],
32        || println!("Trigger start!"),
33        || println!("Trigger stop!"),
34    );
35
36    keyboard_triggers.insert(&[KeyMappingCode::KeyE], || println!("Hello!"));
37
38    keyboard_triggers.insert(&[KeyMappingCode::Escape], || ListeningCmd::Stop);
39
40    let mut triggers = Triggers::default();
41    triggers.insert(keyboard_triggers);
42
43    // Start the event loop
44    triggers.listen();
45
46    println!("Listener gracefully stopped.");
47}

Trait Implementations§

Source§

impl Default for Triggers

Source§

fn default() -> Triggers

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V