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
impl KeyboardListener
Sourcepub fn new() -> Result<Self>
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?
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}Sourcepub fn new_with_blocking(blocking_hotkeys: BlockingHotkeys) -> Result<Self>
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.
Sourcepub fn blocking_hotkeys(&self) -> Option<&BlockingHotkeys>
pub fn blocking_hotkeys(&self) -> Option<&BlockingHotkeys>
Get a reference to the blocking hotkeys set (if blocking is enabled)
Sourcepub fn recv(&self) -> Result<KeyEvent>
pub fn recv(&self) -> Result<KeyEvent>
Blocking receive for key events
Blocks until a key event is received or the listener stops.
Sourcepub fn recv_timeout(&self, timeout: Duration) -> Result<KeyEvent>
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.
Sourcepub fn try_recv(&self) -> Option<KeyEvent>
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?
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}