2021-01-07 11:46:05 -08:00
# class: Keyboard
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
Keyboard provides an api for managing a virtual keyboard. The high level api is [`method: Keyboard.type` ], which takes
2022-10-20 16:46:37 -04:00
raw characters and generates proper `keydown` , `keypress` /`input` , and `keyup` events on your page.
2021-01-07 11:46:05 -08:00
2021-01-14 07:48:56 -08:00
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.
2021-01-07 11:46:05 -08:00
An example of holding down `Shift` in order to select and delete some text:
```js
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');
// Result text will end up saying 'Hello!'
```
2021-02-25 22:03:39 -08:00
```java
page.keyboard().type("Hello World!");
page.keyboard().press("ArrowLeft");
page.keyboard().down("Shift");
for (int i = 0; i < " World".length(); i++)
page.keyboard().press("ArrowLeft");
page.keyboard().up("Shift");
page.keyboard().press("Backspace");
// Result text will end up saying "Hello!"
```
2021-01-14 07:48:56 -08:00
```python async
await page.keyboard.type("Hello World!")
await page.keyboard.press("ArrowLeft")
await page.keyboard.down("Shift")
for i in range(6):
await page.keyboard.press("ArrowLeft")
await page.keyboard.up("Shift")
await page.keyboard.press("Backspace")
# result text will end up saying "Hello!"
```
```python sync
page.keyboard.type("Hello World!")
page.keyboard.press("ArrowLeft")
page.keyboard.down("Shift")
for i in range(6):
page.keyboard.press("ArrowLeft")
page.keyboard.up("Shift")
page.keyboard.press("Backspace")
# result text will end up saying "Hello!"
```
2021-05-13 11:15:27 +02:00
```csharp
await page.Keyboard.TypeAsync("Hello World!");
await page.Keyboard.PressAsync("ArrowLeft");
await page.Keyboard.DownAsync("Shift");
for (int i = 0; i < " World".Length; i++)
await page.Keyboard.PressAsync("ArrowLeft");
await page.Keyboard.UpAsync("Shift");
await page.Keyboard.PressAsync("Backspace");
// Result text will end up saying "Hello!"
```
2021-01-07 11:46:05 -08:00
An example of pressing uppercase `A`
```js
await page.keyboard.press('Shift+KeyA');
// or
await page.keyboard.press('Shift+A');
```
2021-02-25 22:03:39 -08:00
```java
page.keyboard().press("Shift+KeyA");
// or
page.keyboard().press("Shift+A");
```
2021-01-14 07:48:56 -08:00
```python async
await page.keyboard.press("Shift+KeyA")
# or
await page.keyboard.press("Shift+A")
```
```python sync
page.keyboard.press("Shift+KeyA")
# or
page.keyboard.press("Shift+A")
```
2021-05-13 11:15:27 +02:00
```csharp
await page.Keyboard.PressAsync("Shift+KeyA");
2022-10-20 16:46:37 -04:00
// or
2021-05-13 11:15:27 +02:00
await page.Keyboard.PressAsync("Shift+A");
```
2021-01-07 11:46:05 -08:00
An example to trigger select-all with the keyboard
```js
// on Windows and Linux
await page.keyboard.press('Control+A');
// on macOS
await page.keyboard.press('Meta+A');
```
2021-02-25 22:03:39 -08:00
```java
// on Windows and Linux
page.keyboard().press("Control+A");
// on macOS
page.keyboard().press("Meta+A");
```
2021-01-14 07:48:56 -08:00
```python async
# on windows and linux
await page.keyboard.press("Control+A")
# on mac_os
await page.keyboard.press("Meta+A")
```
```python sync
# on windows and linux
page.keyboard.press("Control+A")
# on mac_os
page.keyboard.press("Meta+A")
```
2021-05-13 11:15:27 +02:00
```csharp
// on Windows and Linux
await page.Keyboard.PressAsync("Control+A");
// on macOS
await page.Keyboard.PressAsync("Meta+A");
```
2021-01-07 11:46:05 -08:00
## async method: Keyboard.down
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
Dispatches a `keydown` event.
[`param: key` ] can specify the intended
[keyboardEvent.key ](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key ) value or a single character to
generate the text for. A superset of the [`param: key` ] values can be found
[here ](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values ). 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.
2024-04-29 08:15:12 -07:00
Following modification shortcuts are also supported: `Shift` , `Control` , `Alt` , `Meta` , `ShiftLeft` , `ControlOrMeta` .
`ControlOrMeta` resolves to `Control` on Windows and Linux and to `Meta` on macOS.
2021-01-07 11:46:05 -08:00
Holding down `Shift` will type the text that corresponds to the [`param: key` ] in the upper case.
If [`param: key` ] is a single character, it is case-sensitive, so the values `a` and `A` will generate different
respective texts.
2021-01-14 07:48:56 -08:00
If [`param: 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` ].
2021-01-07 11:46:05 -08:00
After the key is pressed once, subsequent calls to [`method: Keyboard.down` ] will have
[repeat ](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat ) set to true. To release the key, use
[`method: Keyboard.up` ].
2021-01-12 12:14:27 -08:00
:::note
Modifier keys DO influence `keyboard.down` . Holding down `Shift` will type the text in upper case.
:::
2021-01-07 11:46:05 -08:00
### param: Keyboard.down.key
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- `key` < [string]>
Name of the key to press or a character to generate, such as `ArrowLeft` or `a` .
## async method: Keyboard.insertText
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
Dispatches only `input` event, does not emit the `keydown` , `keyup` or `keypress` events.
2022-11-21 10:40:21 -08:00
**Usage**
2021-01-07 11:46:05 -08:00
```js
page.keyboard.insertText('嗨');
```
2021-02-25 22:03:39 -08:00
```java
page.keyboard().insertText("嗨");
```
2021-01-14 07:48:56 -08:00
```python async
await page.keyboard.insert_text("嗨")
```
```python sync
page.keyboard.insert_text("嗨")
```
2021-05-13 11:15:27 +02:00
```csharp
await page.Keyboard.PressAsync("嗨");
```
2021-01-12 12:14:27 -08:00
:::note
Modifier keys DO NOT effect `keyboard.insertText` . Holding down `Shift` will not type the text in upper case.
:::
2021-01-07 11:46:05 -08:00
### param: Keyboard.insertText.text
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- `text` < [string]>
Sets input to the specified text value.
## async method: Keyboard.press
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
2023-09-06 12:41:12 -07:00
:::tip
In most cases, you should use [`method: Locator.press` ] instead.
:::
2021-01-07 11:46:05 -08:00
[`param: key` ] can specify the intended
[keyboardEvent.key ](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key ) value or a single character to
generate the text for. A superset of the [`param: key` ] values can be found
[here ](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values ). 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.
2024-04-29 08:15:12 -07:00
Following modification shortcuts are also supported: `Shift` , `Control` , `Alt` , `Meta` , `ShiftLeft` , `ControlOrMeta` .
`ControlOrMeta` resolves to `Control` on Windows and Linux and to `Meta` on macOS.
2021-01-07 11:46:05 -08:00
Holding down `Shift` will type the text that corresponds to the [`param: key` ] in the upper case.
If [`param: key` ] is a single character, it is case-sensitive, so the values `a` and `A` will generate different
respective texts.
2024-01-29 23:57:10 +01:00
Shortcuts such as `key: "Control+o"` , `key: "Control++` or `key: "Control+Shift+T"` are supported as well. When specified with the
2021-01-07 11:46:05 -08:00
modifier, modifier is pressed and being held while the subsequent key is being pressed.
2022-11-21 10:40:21 -08:00
**Usage**
2021-01-07 11:46:05 -08:00
```js
const page = await browser.newPage();
await page.goto('https://keycode.info');
await page.keyboard.press('A');
await page.screenshot({ path: 'A.png' });
await page.keyboard.press('ArrowLeft');
await page.screenshot({ path: 'ArrowLeft.png' });
await page.keyboard.press('Shift+O');
await page.screenshot({ path: 'O.png' });
await browser.close();
```
2021-02-25 22:03:39 -08:00
```java
Page page = browser.newPage();
page.navigate("https://keycode.info");
page.keyboard().press("A");
2021-03-05 13:50:34 -08:00
page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("A.png"));
2021-02-25 22:03:39 -08:00
page.keyboard().press("ArrowLeft");
2021-03-05 13:50:34 -08:00
page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("ArrowLeft.png")));
2021-02-25 22:03:39 -08:00
page.keyboard().press("Shift+O");
2021-03-05 13:50:34 -08:00
page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("O.png")));
2021-02-25 22:03:39 -08:00
browser.close();
```
2021-01-14 07:48:56 -08:00
```python async
page = await browser.new_page()
await page.goto("https://keycode.info")
await page.keyboard.press("a")
await page.screenshot(path="a.png")
await page.keyboard.press("ArrowLeft")
await page.screenshot(path="arrow_left.png")
await page.keyboard.press("Shift+O")
await page.screenshot(path="o.png")
await browser.close()
```
```python sync
page = browser.new_page()
page.goto("https://keycode.info")
page.keyboard.press("a")
page.screenshot(path="a.png")
page.keyboard.press("ArrowLeft")
page.screenshot(path="arrow_left.png")
page.keyboard.press("Shift+O")
page.screenshot(path="o.png")
browser.close()
```
2021-05-13 11:15:27 +02:00
```csharp
2021-05-13 11:57:02 -07:00
await page.GotoAsync("https://keycode.info");
2021-05-13 11:15:27 +02:00
await page.Keyboard.PressAsync("A");
2023-03-16 17:23:31 +01:00
await page.ScreenshotAsync(new() { Path = "A.png" });
2021-05-13 11:15:27 +02:00
await page.Keyboard.PressAsync("ArrowLeft");
2023-03-16 17:23:31 +01:00
await page.ScreenshotAsync(new() { Path = "ArrowLeft.png" });
2021-05-13 11:15:27 +02:00
await page.Keyboard.PressAsync("Shift+O");
2023-03-16 17:23:31 +01:00
await page.ScreenshotAsync(new() { Path = "O.png" });
2021-05-13 11:15:27 +02:00
await browser.CloseAsync();
```
2021-01-07 11:46:05 -08:00
Shortcut for [`method: Keyboard.down` ] and [`method: Keyboard.up` ].
### param: Keyboard.press.key
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- `key` < [string]>
Name of the key to press or a character to generate, such as `ArrowLeft` or `a` .
### option: Keyboard.press.delay
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- `delay` < [float]>
Time to wait between `keydown` and `keyup` in milliseconds. Defaults to 0.
## async method: Keyboard.type
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
2023-09-06 12:41:12 -07:00
:::caution
In most cases, you should use [`method: Locator.fill` ] instead. You only need to press keys one by one if there is special keyboard handling on the page - in this case use [`method: Locator.pressSequentially` ].
:::
2021-01-07 11:46:05 -08:00
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` ].
2022-11-21 10:40:21 -08:00
**Usage**
2021-01-07 11:46:05 -08:00
```js
await page.keyboard.type('Hello'); // Types instantly
2023-06-27 11:53:53 +02:00
await page.keyboard.type('World', { delay: 100 }); // Types slower, like a user
2021-01-07 11:46:05 -08:00
```
2021-02-25 22:03:39 -08:00
```java
// Types instantly
page.keyboard().type("Hello");
// Types slower, like a user
2021-03-05 13:50:34 -08:00
page.keyboard().type("World", new Keyboard.TypeOptions().setDelay(100));
2021-02-25 22:03:39 -08:00
```
2021-01-14 07:48:56 -08:00
```python async
await page.keyboard.type("Hello") # types instantly
await page.keyboard.type("World", delay=100) # types slower, like a user
```
```python sync
page.keyboard.type("Hello") # types instantly
page.keyboard.type("World", delay=100) # types slower, like a user
```
2021-05-13 11:15:27 +02:00
```csharp
await page.Keyboard.TypeAsync("Hello"); // types instantly
2023-03-16 17:23:31 +01:00
await page.Keyboard.TypeAsync("World", new() { Delay = 100 }); // types slower, like a user
2021-05-13 11:15:27 +02:00
```
2021-01-12 12:14:27 -08:00
:::note
Modifier keys DO NOT effect `keyboard.type` . Holding down `Shift` will not type the text in upper case.
:::
2021-01-07 11:46:05 -08:00
2021-04-26 20:54:40 -07:00
:::note
For characters that are not on a US keyboard, only an `input` event will be sent.
:::
2021-01-07 11:46:05 -08:00
### param: Keyboard.type.text
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- `text` < [string]>
A text to type into a focused element.
### option: Keyboard.type.delay
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- `delay` < [float]>
Time to wait between key presses in milliseconds. Defaults to 0.
## async method: Keyboard.up
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
Dispatches a `keyup` event.
### param: Keyboard.up.key
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- `key` < [string]>
Name of the key to press or a character to generate, such as `ArrowLeft` or `a` .