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
// Keyboard - Low-level keyboard control
//
// See: https://playwright.dev/docs/api/class-keyboard
use crate::error::Result;
use crate::protocol::page::Page;
/// Keyboard provides low-level keyboard control.
///
/// See: <https://playwright.dev/docs/api/class-keyboard>
#[derive(Clone)]
pub struct Keyboard {
page: Page,
}
impl Keyboard {
/// Creates a new Keyboard instance for the given page
pub(crate) fn new(page: Page) -> Self {
Self { page }
}
/// Dispatches a `keydown` event.
///
/// See: <https://playwright.dev/docs/api/class-keyboard#keyboard-down>
pub async fn down(&self, key: &str) -> Result<()> {
self.page.keyboard_down(key).await
}
/// Dispatches a `keyup` event.
///
/// See: <https://playwright.dev/docs/api/class-keyboard#keyboard-up>
pub async fn up(&self, key: &str) -> Result<()> {
self.page.keyboard_up(key).await
}
/// Executes a complete key press (down + up sequence).
///
/// See: <https://playwright.dev/docs/api/class-keyboard#keyboard-press>
pub async fn press(
&self,
key: &str,
options: Option<crate::protocol::KeyboardOptions>,
) -> Result<()> {
self.page.keyboard_press(key, options).await
}
/// Sends a `keydown`, `keypress`/`input`, and `keyup` event for each character.
///
/// See: <https://playwright.dev/docs/api/class-keyboard#keyboard-type>
pub async fn type_text(
&self,
text: &str,
options: Option<crate::protocol::KeyboardOptions>,
) -> Result<()> {
self.page.keyboard_type(text, options).await
}
/// Dispatches only `input` event, does not emit `keydown`, `keyup` or `keypress` events.
///
/// See: <https://playwright.dev/docs/api/class-keyboard#keyboard-insert-text>
pub async fn insert_text(&self, text: &str) -> Result<()> {
self.page.keyboard_insert_text(text).await
}
}