Keyboard

Struct Keyboard 

Source
pub struct Keyboard {
    inner: Weak<Page>,
}
Expand description

Keyboard provides an api for managing a virtual keyboard. The high level api is [method: Keyboard.type], which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page.

For finer control, you can use [method: Keyboard.down], [method: Keyboard.up], and [method: Keyboard.insertText] to manually fire events as if they were generated from a real keyboard.

An example of holding down Shift in order to select and delete some text:

await page.keyboard.type('Hello World!');
await page.keyboard.press('ArrowLeft');

await page.keyboard.down('Shift');
for (let i = 0; i < ' World'.length; i++)
 await page.keyboard.press('ArrowLeft');
await page.keyboard.up('Shift');

await page.keyboard.press('Backspace');

An example of pressing uppercase A

await page.keyboard.press('Shift+KeyA');
await page.keyboard.press('Shift+A');

An example to trigger select-all with the keyboard

await page.keyboard.press('Control+A');
await page.keyboard.press('Meta+A');

Fields§

§inner: Weak<Page>

Implementations§

Source§

impl Keyboard

Source

pub(crate) fn new(inner: Weak<PageImpl>) -> Self

Source

pub async fn down(&self, key: &str) -> Result<(), Arc<Error>>

Dispatches a keydown event.

key can specify the intended keyboardEvent.key value or a single character to generate the text for. A superset of the key values can be found here. Examples of the keys are:

F1 - F12, Digit0- Digit9, KeyA- KeyZ, Backquote, Minus, Equal, Backslash, Backspace, Tab, Delete, Escape, ArrowDown, End, Enter, Home, Insert, PageDown, PageUp, ArrowRight, ArrowUp, etc.

Following modification shortcuts are also supported: Shift, Control, Alt, Meta, ShiftLeft.

Holding down Shift will type the text that corresponds to the key in the upper case.

If key is a single character, it is case-sensitive, so the values a and A will generate different respective texts.

If key is a modifier key, Shift, Meta, Control, or Alt, subsequent key presses will be sent with that modifier active. To release the modifier key, use [method: Keyboard.up].

After the key is pressed once, subsequent calls to [method: Keyboard.down] will have repeat set to true. To release the key, use [method: Keyboard.up].

NOTE: Modifier keys DO influence keyboard.down. Holding down Shift will type the text in upper case.

Source

pub async fn up(&self, key: &str) -> Result<(), Arc<Error>>

Source

pub async fn input_text(&self, text: &str) -> Result<(), Arc<Error>>

Dispatches only input event, does not emit the keydown, keyup or keypress events.

page.keyboard.insertText('嗨');

NOTE: Modifier keys DO NOT effect keyboard.insertText. Holding down Shift will not type the text in upper case.

Source

pub async fn type( &self, text: &str, delay: Option<f64>, ) -> Result<(), Arc<Error>>

Sends a keydown, keypress/input, and keyup event for each character in the text.

To press a special key, like Control or ArrowDown, use [method: Keyboard.press].

await page.keyboard.type('Hello'); // Types instantly
await page.keyboard.type('World', {delay: 100}); // Types slower, like a user

NOTE: Modifier keys DO NOT effect keyboard.type. Holding down Shift will not type the text in upper case. NOTE: For characters that are not on a US keyboard, only an input event will be sent.

Source

pub async fn press( &self, key: &str, delay: Option<f64>, ) -> Result<(), Arc<Error>>

Shortcut for [method: Keyboard.down] and [method: Keyboard.up].

Trait Implementations§

Source§

impl Clone for Keyboard

Source§

fn clone(&self) -> Keyboard

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Keyboard

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.