firefox_webdriver/browser/keyboard.rs
1//! Keyboard key definitions and utilities.
2//!
3//! Provides ergonomic key constants for common navigation and control keys.
4//!
5//! # Example
6//!
7//! ```ignore
8//! use firefox_webdriver::Key;
9//!
10//! // Navigation keys
11//! element.press(Key::Enter).await?;
12//! element.press(Key::Tab).await?;
13//! element.press(Key::Escape).await?;
14//!
15//! // For typing text, use type_text instead:
16//! element.type_text("Hello, World!").await?;
17//! ```
18
19// ============================================================================
20// Key Enum
21// ============================================================================
22
23/// Common keyboard keys for navigation and control.
24///
25/// This enum provides ergonomic constants for frequently used keys.
26/// For typing text/letters, use `element.type_text()` instead.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28pub enum Key {
29 // ========================================================================
30 // Navigation & Control
31 // ========================================================================
32 /// Enter/Return key
33 Enter,
34 /// Tab key
35 Tab,
36 /// Escape key
37 Escape,
38 /// Backspace key
39 Backspace,
40 /// Delete key
41 Delete,
42 /// Space bar
43 Space,
44
45 // ========================================================================
46 // Arrow Keys
47 // ========================================================================
48 /// Arrow Up
49 ArrowUp,
50 /// Arrow Down
51 ArrowDown,
52 /// Arrow Left
53 ArrowLeft,
54 /// Arrow Right
55 ArrowRight,
56
57 // ========================================================================
58 // Page Navigation
59 // ========================================================================
60 /// Home key
61 Home,
62 /// End key
63 End,
64 /// Page Up key
65 PageUp,
66 /// Page Down key
67 PageDown,
68}
69
70impl Key {
71 /// Returns the key properties: (key, code, keyCode, printable).
72 #[must_use]
73 pub fn properties(self) -> (&'static str, &'static str, u32, bool) {
74 match self {
75 Key::Enter => ("Enter", "Enter", 13, false),
76 Key::Tab => ("Tab", "Tab", 9, false),
77 Key::Escape => ("Escape", "Escape", 27, false),
78 Key::Backspace => ("Backspace", "Backspace", 8, false),
79 Key::Delete => ("Delete", "Delete", 46, false),
80 Key::Space => (" ", "Space", 32, true),
81 Key::ArrowUp => ("ArrowUp", "ArrowUp", 38, false),
82 Key::ArrowDown => ("ArrowDown", "ArrowDown", 40, false),
83 Key::ArrowLeft => ("ArrowLeft", "ArrowLeft", 37, false),
84 Key::ArrowRight => ("ArrowRight", "ArrowRight", 39, false),
85 Key::Home => ("Home", "Home", 36, false),
86 Key::End => ("End", "End", 35, false),
87 Key::PageUp => ("PageUp", "PageUp", 33, false),
88 Key::PageDown => ("PageDown", "PageDown", 34, false),
89 }
90 }
91
92 /// Returns the key value string.
93 #[inline]
94 #[must_use]
95 pub fn key(self) -> &'static str {
96 self.properties().0
97 }
98
99 /// Returns the code string.
100 #[inline]
101 #[must_use]
102 pub fn code(self) -> &'static str {
103 self.properties().1
104 }
105
106 /// Returns the legacy keyCode.
107 #[inline]
108 #[must_use]
109 pub fn key_code(self) -> u32 {
110 self.properties().2
111 }
112
113 /// Returns whether this key produces printable output.
114 #[inline]
115 #[must_use]
116 pub fn is_printable(self) -> bool {
117 self.properties().3
118 }
119}
120
121// ============================================================================
122// Tests
123// ============================================================================
124
125#[cfg(test)]
126mod tests {
127 use super::*;
128
129 #[test]
130 fn test_key_properties() {
131 let (key, code, key_code, printable) = Key::Enter.properties();
132 assert_eq!(key, "Enter");
133 assert_eq!(code, "Enter");
134 assert_eq!(key_code, 13);
135 assert!(!printable);
136 }
137
138 #[test]
139 fn test_space_is_printable() {
140 assert!(Key::Space.is_printable());
141 assert!(!Key::Enter.is_printable());
142 }
143}