1use serde::Serialize;
6
7#[derive(Debug, Clone, Copy, Serialize)]
9#[serde(rename_all = "lowercase")]
10pub enum MouseButton {
11 None,
12 Left,
13 Middle,
14 Right,
15 Back,
16 Forward,
17}
18
19#[derive(Debug, Clone, Copy, Serialize)]
21#[serde(rename_all = "camelCase")]
22pub enum MouseEventType {
23 MousePressed,
24 MouseReleased,
25 MouseMoved,
26 MouseWheel,
27}
28
29#[derive(Debug, Clone, Copy, Serialize)]
31#[serde(rename_all = "camelCase")]
32pub enum KeyEventType {
33 KeyDown,
34 KeyUp,
35 RawKeyDown,
36 Char,
37}
38
39#[derive(Debug, Clone, Serialize)]
41#[serde(rename_all = "camelCase")]
42pub struct DispatchMouseEventParams {
43 #[serde(rename = "type")]
45 pub event_type: MouseEventType,
46 pub x: f64,
48 pub y: f64,
50 #[serde(skip_serializing_if = "Option::is_none")]
52 pub button: Option<MouseButton>,
53 #[serde(skip_serializing_if = "Option::is_none")]
55 pub click_count: Option<i32>,
56 #[serde(skip_serializing_if = "Option::is_none")]
58 pub modifiers: Option<i32>,
59 #[serde(skip_serializing_if = "Option::is_none")]
61 pub timestamp: Option<f64>,
62}
63
64impl DispatchMouseEventParams {
65 pub fn mouse_move(x: f64, y: f64) -> Self {
67 Self {
68 event_type: MouseEventType::MouseMoved,
69 x,
70 y,
71 button: None,
72 click_count: None,
73 modifiers: None,
74 timestamp: None,
75 }
76 }
77
78 pub fn mouse_down(x: f64, y: f64, button: MouseButton) -> Self {
80 Self {
81 event_type: MouseEventType::MousePressed,
82 x,
83 y,
84 button: Some(button),
85 click_count: Some(1),
86 modifiers: None,
87 timestamp: None,
88 }
89 }
90
91 pub fn mouse_up(x: f64, y: f64, button: MouseButton) -> Self {
93 Self {
94 event_type: MouseEventType::MouseReleased,
95 x,
96 y,
97 button: Some(button),
98 click_count: Some(1),
99 modifiers: None,
100 timestamp: None,
101 }
102 }
103}
104
105#[derive(Debug, Clone, Serialize)]
107#[serde(rename_all = "camelCase")]
108pub struct DispatchKeyEventParams {
109 #[serde(rename = "type")]
111 pub event_type: KeyEventType,
112 #[serde(skip_serializing_if = "Option::is_none")]
114 pub modifiers: Option<i32>,
115 #[serde(skip_serializing_if = "Option::is_none")]
117 pub timestamp: Option<f64>,
118 #[serde(skip_serializing_if = "Option::is_none")]
120 pub text: Option<String>,
121 #[serde(skip_serializing_if = "Option::is_none")]
123 pub unmodified_text: Option<String>,
124 #[serde(skip_serializing_if = "Option::is_none")]
126 pub key_identifier: Option<String>,
127 #[serde(skip_serializing_if = "Option::is_none")]
129 pub code: Option<String>,
130 #[serde(skip_serializing_if = "Option::is_none")]
132 pub key: Option<String>,
133 #[serde(skip_serializing_if = "Option::is_none")]
135 pub windows_virtual_key_code: Option<i32>,
136 #[serde(skip_serializing_if = "Option::is_none")]
138 pub native_virtual_key_code: Option<i32>,
139 #[serde(skip_serializing_if = "Option::is_none")]
141 pub auto_repeat: Option<bool>,
142 #[serde(skip_serializing_if = "Option::is_none")]
144 pub is_keypad: Option<bool>,
145 #[serde(skip_serializing_if = "Option::is_none")]
147 pub is_system_key: Option<bool>,
148 #[serde(skip_serializing_if = "Option::is_none")]
150 pub commands: Option<Vec<String>>,
151}
152
153impl DispatchKeyEventParams {
154 pub fn key_down(key: &str) -> Self {
156 Self {
157 event_type: KeyEventType::KeyDown,
158 modifiers: None,
159 timestamp: None,
160 text: None,
161 unmodified_text: None,
162 key_identifier: None,
163 code: Some(key.to_string()),
164 key: Some(key.to_string()),
165 windows_virtual_key_code: None,
166 native_virtual_key_code: None,
167 auto_repeat: None,
168 is_keypad: None,
169 is_system_key: None,
170 commands: None,
171 }
172 }
173
174 pub fn key_up(key: &str) -> Self {
176 Self {
177 event_type: KeyEventType::KeyUp,
178 modifiers: None,
179 timestamp: None,
180 text: None,
181 unmodified_text: None,
182 key_identifier: None,
183 code: Some(key.to_string()),
184 key: Some(key.to_string()),
185 windows_virtual_key_code: None,
186 native_virtual_key_code: None,
187 auto_repeat: None,
188 is_keypad: None,
189 is_system_key: None,
190 commands: None,
191 }
192 }
193
194 pub fn char(text: &str) -> Self {
196 Self {
197 event_type: KeyEventType::Char,
198 modifiers: None,
199 timestamp: None,
200 text: Some(text.to_string()),
201 unmodified_text: Some(text.to_string()),
202 key_identifier: None,
203 code: None,
204 key: None,
205 windows_virtual_key_code: None,
206 native_virtual_key_code: None,
207 auto_repeat: None,
208 is_keypad: None,
209 is_system_key: None,
210 commands: None,
211 }
212 }
213}
214
215#[derive(Debug, Clone, Serialize)]
217#[serde(rename_all = "camelCase")]
218pub struct InsertTextParams {
219 pub text: String,
221}
222
223pub mod modifiers {
225 pub const ALT: i32 = 1;
226 pub const CTRL: i32 = 2;
227 pub const META: i32 = 4;
228 pub const SHIFT: i32 = 8;
229}