1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Async channel example with Tokio.
//!
//! Run with: cargo run --example channel_async --features tokio
//!
//! This example shows how to use async channels with Tokio
//! to receive events in an async context.
use monio::EventType;
use monio::channel::listen_async_channel;
use std::time::Duration;
use tokio::time::interval;
#[tokio::main]
async fn main() {
println!("monio channel example (async/tokio)");
println!("=====================================\n");
println!("Events will be received asynchronously.");
println!("Press Ctrl+C to exit.\n");
// Start the hook with an async channel
let (handle, mut rx) = listen_async_channel(100).expect("Failed to start hook");
println!("Hook started, waiting for events...\n");
let mut event_count = 0u32;
let mut heartbeat = interval(Duration::from_secs(5));
loop {
tokio::select! {
// Receive events from the hook
event = rx.recv() => {
match event {
Some(event) => {
event_count += 1;
match event.event_type {
EventType::KeyPressed => {
if let Some(kb) = &event.keyboard {
println!("[{}] Key pressed: {:?}", event_count, kb.key);
}
}
EventType::KeyReleased => {
if let Some(kb) = &event.keyboard {
println!("[{}] Key released: {:?}", event_count, kb.key);
}
}
EventType::MousePressed => {
if let Some(mouse) = &event.mouse {
println!(
"[{}] Mouse {:?} pressed at ({:.0}, {:.0})",
event_count, mouse.button, mouse.x, mouse.y
);
}
}
EventType::MouseDragged => {
// Only print every 20th drag event
if event_count % 20 == 0 {
if let Some(mouse) = &event.mouse {
println!(
"[{}] Dragging at ({:.0}, {:.0})",
event_count, mouse.x, mouse.y
);
}
}
}
EventType::HookEnabled => {
println!("[{}] Hook enabled!", event_count);
}
_ => {}
}
}
None => {
println!("Channel closed, hook stopped.");
break;
}
}
}
// Periodic heartbeat to show the async loop is responsive
_ = heartbeat.tick() => {
println!("... heartbeat (received {} events so far)", event_count);
}
}
}
// Clean up
let _ = handle.stop();
}