hookmap_core/
event.rs

1//! Representation of keyboard and mouse events.
2//!
3//! When an event is generated, [`EventReceiver::recv`] can be called to receive the event.
4//! The received event is blocked (other programs are not yet notified).
5//!
6//! Calling [`NativeEventHandler::block`] will continue blocking and no notification will be made.
7//! Alternatively, calling [`NativeEventHandler::dispatch`] will notify other programs of the event.
8//! If neither is called, the event is notified.
9//!
10//! # Warning
11//!
12//! On Windows, Calling function that perform input (e.g. [`Button::press`]) before calling [`NativeEventHandler::block`]
13//! or [`NativeEventHandler::dispatch`] will take time.
14//! Therefore, call these functions before calling function that performs the input.
15//!
16//! # Examples
17//!
18//! ```no_run
19//! let rx = hookmap_core::install_hook();
20//! while let Ok((event, native_handler)) = rx.recv() {
21//!     // Blocks all the events
22//!     native_handler.block();
23//! }
24//! ```
25
26use super::button::{Button, ButtonAction};
27use std::sync::mpsc::{self, Receiver, Sender, SyncSender};
28
29/// Indicates whether to pass the generated event to the next program or not.
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
31pub enum NativeEventOperation {
32    /// Do not pass the generated event to the next program.
33    Block,
34
35    /// Pass the generated event to the next program.
36    Dispatch,
37}
38
39impl Default for &NativeEventOperation {
40    fn default() -> Self {
41        &NativeEventOperation::Dispatch
42    }
43}
44
45impl Default for NativeEventOperation {
46    fn default() -> Self {
47        *<&NativeEventOperation>::default()
48    }
49}
50
51/// Indicates button event.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
53pub struct ButtonEvent {
54    /// Target of the generated event.
55    pub target: Button,
56
57    /// Action of the generated event.
58    pub action: ButtonAction,
59
60    /// Whether this event was generated by this program.
61    /// If you type on your keyboard and an event is generated, this value will be `false`.
62    pub injected: bool,
63}
64
65/// Indicates mouse cursor event.
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
67pub struct CursorEvent {
68    /// Mouse cursor movement `(x, y)`
69    pub delta: (i32, i32),
70
71    /// Whether this event was generated by this program.
72    pub injected: bool,
73}
74
75/// Indicates mouse wheel event.
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
77pub struct WheelEvent {
78    /// Amout of mouse wheel rotation
79    /// Upward rotation takes a positive value, downward rotation a negative value.
80    pub delta: i32,
81
82    /// Whether this event was generated by this program.
83    pub injected: bool,
84}
85
86/// An event
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
88pub enum Event {
89    /// Button event
90    Button(ButtonEvent),
91
92    /// Mouse wheel event
93    Wheel(WheelEvent),
94
95    /// Mouse cursor event
96    Cursor(CursorEvent),
97}
98
99/// Decide whether to notify other programs of generated events.
100#[derive(Debug)]
101pub struct NativeEventHandler {
102    tx: Option<Sender<NativeEventOperation>>,
103}
104
105impl NativeEventHandler {
106    fn new(tx: Sender<NativeEventOperation>) -> Self {
107        Self { tx: Some(tx) }
108    }
109
110    /// Decides whether or not to notify by argument.
111    pub fn handle(mut self, operation: NativeEventOperation) {
112        self.tx.take().unwrap().send(operation).unwrap();
113    }
114
115    // Notifies an event.
116    pub fn dispatch(self) {
117        self.handle(NativeEventOperation::Dispatch);
118    }
119
120    // Does not notify an event.
121    pub fn block(self) {
122        self.handle(NativeEventOperation::Block);
123    }
124}
125
126impl Drop for NativeEventHandler {
127    fn drop(&mut self) {
128        if let Some(tx) = self.tx.take() {
129            tx.send(NativeEventOperation::default()).unwrap();
130        }
131    }
132}
133
134#[derive(Debug, Clone)]
135pub(crate) struct EventSender {
136    tx: SyncSender<(Event, NativeEventHandler)>,
137}
138
139impl EventSender {
140    pub(crate) fn new(tx: SyncSender<(Event, NativeEventHandler)>) -> Self {
141        Self { tx }
142    }
143
144    pub(crate) fn send(&self, event: Event) -> NativeEventOperation {
145        let (tx, rx) = mpsc::channel();
146        let sent_data = (event, NativeEventHandler::new(tx));
147
148        self.tx.send(sent_data).unwrap();
149        rx.recv().unwrap()
150    }
151}
152
153pub type EventReceiver = Receiver<(Event, NativeEventHandler)>;
154
155pub(crate) fn channel() -> (EventSender, EventReceiver) {
156    const BOUND: usize = 1;
157    let (tx, rx) = mpsc::sync_channel(BOUND);
158    (EventSender::new(tx), rx)
159}