viewpoint_cdp/protocol/
input.rs

1//! Input domain types.
2//!
3//! The Input domain provides methods for simulating user input events.
4
5use serde::Serialize;
6
7/// Mouse button type.
8#[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/// Type of mouse event.
20#[derive(Debug, Clone, Copy, Serialize)]
21#[serde(rename_all = "camelCase")]
22pub enum MouseEventType {
23    MousePressed,
24    MouseReleased,
25    MouseMoved,
26    MouseWheel,
27}
28
29/// Type of key event.
30#[derive(Debug, Clone, Copy, Serialize)]
31#[serde(rename_all = "camelCase")]
32pub enum KeyEventType {
33    KeyDown,
34    KeyUp,
35    RawKeyDown,
36    Char,
37}
38
39/// Parameters for Input.dispatchMouseEvent.
40#[derive(Debug, Clone, Serialize)]
41#[serde(rename_all = "camelCase")]
42pub struct DispatchMouseEventParams {
43    /// Type of the mouse event.
44    #[serde(rename = "type")]
45    pub event_type: MouseEventType,
46    /// X coordinate of the event relative to the main frame's viewport.
47    pub x: f64,
48    /// Y coordinate of the event relative to the main frame's viewport.
49    pub y: f64,
50    /// Mouse button.
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub button: Option<MouseButton>,
53    /// Number of times the mouse button was clicked.
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub click_count: Option<i32>,
56    /// Bit field representing pressed modifier keys.
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub modifiers: Option<i32>,
59    /// Time at which the event occurred.
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub timestamp: Option<f64>,
62}
63
64impl DispatchMouseEventParams {
65    /// Create a mouse move event.
66    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    /// Create a mouse down event.
79    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    /// Create a mouse up event.
92    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/// Parameters for Input.dispatchKeyEvent.
106#[derive(Debug, Clone, Serialize)]
107#[serde(rename_all = "camelCase")]
108pub struct DispatchKeyEventParams {
109    /// Type of the key event.
110    #[serde(rename = "type")]
111    pub event_type: KeyEventType,
112    /// Bit field representing pressed modifier keys.
113    #[serde(skip_serializing_if = "Option::is_none")]
114    pub modifiers: Option<i32>,
115    /// Time at which the event occurred.
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub timestamp: Option<f64>,
118    /// Text as generated by processing a virtual key code.
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub text: Option<String>,
121    /// Text that would have been generated without any modifiers.
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub unmodified_text: Option<String>,
124    /// Unique key identifier.
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub key_identifier: Option<String>,
127    /// Unique DOM defined string value for each key.
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub code: Option<String>,
130    /// Unique DOM defined string value describing the meaning of the key.
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub key: Option<String>,
133    /// Windows virtual key code.
134    #[serde(skip_serializing_if = "Option::is_none")]
135    pub windows_virtual_key_code: Option<i32>,
136    /// Native virtual key code.
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub native_virtual_key_code: Option<i32>,
139    /// Whether the event was generated from auto repeat.
140    #[serde(skip_serializing_if = "Option::is_none")]
141    pub auto_repeat: Option<bool>,
142    /// Whether the event was generated from the keypad.
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub is_keypad: Option<bool>,
145    /// Whether the event was a system key event.
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub is_system_key: Option<bool>,
148    /// Editing commands to send with the event.
149    #[serde(skip_serializing_if = "Option::is_none")]
150    pub commands: Option<Vec<String>>,
151}
152
153impl DispatchKeyEventParams {
154    /// Create a key down event.
155    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    /// Create a key up event.
175    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    /// Create a char event for text input.
195    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/// Parameters for Input.insertText.
216#[derive(Debug, Clone, Serialize)]
217#[serde(rename_all = "camelCase")]
218pub struct InsertTextParams {
219    /// The text to insert.
220    pub text: String,
221}
222
223/// Modifier keys bit flags.
224pub 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}