Skip to main content

KeyboardListener

Struct KeyboardListener 

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

Platform-agnostic Keyboard Listener

Streams all keyboard events. Can optionally block events that match registered hotkeys.

Implementations§

Source§

impl KeyboardListener

Source

pub fn new() -> Result<Self>

Create a new KeyboardListener (non-blocking mode)

Events are observed but not blocked. Use this for “record hotkey” UI flows.

On macOS, this will check for accessibility permissions and fail if not granted.

Examples found in repository?
examples/record_hotkey.rs (line 16)
11fn main() -> Result<()> {
12    println!("Recording keyboard events...");
13    println!("Press keys to see events. Press Escape to exit.");
14    println!();
15
16    let listener = KeyboardListener::new()?;
17
18    loop {
19        // Non-blocking check for events
20        if let Some(event) = listener.try_recv() {
21            let state = if event.is_key_down { "DOWN" } else { "UP" };
22
23            // Build a display string for the current combination
24            let combo = if event.modifiers.is_empty() {
25                match event.key {
26                    Some(key) => format!("{}", key),
27                    None => "(no key)".to_string(),
28                }
29            } else {
30                match event.key {
31                    Some(key) => format!("{}+{}", event.modifiers, key),
32                    None => format!("{}", event.modifiers),
33                }
34            };
35
36            println!("[{}] {}", state, combo);
37
38            // Check for Escape to exit
39            if event.key == Some(handy_keys::Key::Escape) && event.is_key_down {
40                println!("\nEscape pressed, exiting...");
41                break;
42            }
43        }
44
45        // Small sleep to avoid busy-waiting
46        std::thread::sleep(Duration::from_millis(10));
47    }
48
49    Ok(())
50}
Source

pub fn new_with_blocking(blocking_hotkeys: BlockingHotkeys) -> Result<Self>

Create a new KeyboardListener with blocking support

Events matching hotkeys in the provided set will be blocked from reaching other applications. The set can be modified after creation to add/remove hotkeys dynamically.

Note: On Wayland, blocking may not work due to compositor restrictions.

Source

pub fn blocking_hotkeys(&self) -> Option<&BlockingHotkeys>

Get a reference to the blocking hotkeys set (if blocking is enabled)

Source

pub fn recv(&self) -> Result<KeyEvent>

Blocking receive for key events

Blocks until a key event is received or the listener stops.

Source

pub fn recv_timeout(&self, timeout: Duration) -> Result<KeyEvent>

Blocking receive with timeout

Blocks until a key event is received, the timeout expires, or the listener stops.

Source

pub fn try_recv(&self) -> Option<KeyEvent>

Non-blocking receive for key events

Returns Some(event) if an event is available, None otherwise.

Examples found in repository?
examples/record_hotkey.rs (line 20)
11fn main() -> Result<()> {
12    println!("Recording keyboard events...");
13    println!("Press keys to see events. Press Escape to exit.");
14    println!();
15
16    let listener = KeyboardListener::new()?;
17
18    loop {
19        // Non-blocking check for events
20        if let Some(event) = listener.try_recv() {
21            let state = if event.is_key_down { "DOWN" } else { "UP" };
22
23            // Build a display string for the current combination
24            let combo = if event.modifiers.is_empty() {
25                match event.key {
26                    Some(key) => format!("{}", key),
27                    None => "(no key)".to_string(),
28                }
29            } else {
30                match event.key {
31                    Some(key) => format!("{}+{}", event.modifiers, key),
32                    None => format!("{}", event.modifiers),
33                }
34            };
35
36            println!("[{}] {}", state, combo);
37
38            // Check for Escape to exit
39            if event.key == Some(handy_keys::Key::Escape) && event.is_key_down {
40                println!("\nEscape pressed, exiting...");
41                break;
42            }
43        }
44
45        // Small sleep to avoid busy-waiting
46        std::thread::sleep(Duration::from_millis(10));
47    }
48
49    Ok(())
50}

Trait Implementations§

Source§

impl Drop for KeyboardListener

Source§

fn drop(&mut self)

Executes the destructor for this 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.