Playwright文档 - Keyboard(键盘)


Keyboard 提供了用于管理虚拟键盘的 api。高级 API 是Keyboard.type(),它接受原始字符并在页面上生成正确的keydown、keypress/input和事件。keyup

class: Keyboard

  • since: v1.8

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:

1await page.keyboard.type('Hello World!'); 2await page.keyboard.press('ArrowLeft'); 3 4await page.keyboard.down('Shift'); 5for (let i = 0; i < ' World'.length; i++) 6 await page.keyboard.press('ArrowLeft'); 7await page.keyboard.up('Shift'); 8 9await page.keyboard.press('Backspace'); 10// Result text will end up saying 'Hello!' 11
1page.keyboard().type("Hello World!"); 2page.keyboard().press("ArrowLeft"); 3page.keyboard().down("Shift"); 4for (int i = 0; i < " World".length(); i++) 5 page.keyboard().press("ArrowLeft"); 6page.keyboard().up("Shift"); 7page.keyboard().press("Backspace"); 8// Result text will end up saying "Hello!" 9
1await page.keyboard.type("Hello World!") 2await page.keyboard.press("ArrowLeft") 3await page.keyboard.down("Shift") 4for i in range(6): 5 await page.keyboard.press("ArrowLeft") 6await page.keyboard.up("Shift") 7await page.keyboard.press("Backspace") 8# result text will end up saying "Hello!" 9
1page.keyboard.type("Hello World!") 2page.keyboard.press("ArrowLeft") 3page.keyboard.down("Shift") 4for i in range(6): 5 page.keyboard.press("ArrowLeft") 6page.keyboard.up("Shift") 7page.keyboard.press("Backspace") 8# result text will end up saying "Hello!" 9
1await page.Keyboard.TypeAsync("Hello World!"); 2await page.Keyboard.PressAsync("ArrowLeft"); 3 4await page.Keyboard.DownAsync("Shift"); 5for (int i = 0; i < " World".Length; i++) 6 await page.Keyboard.PressAsync("ArrowLeft"); 7 8await page.Keyboard.UpAsync("Shift"); 9 10await page.Keyboard.PressAsync("Backspace"); 11// Result text will end up saying "Hello!" 12

An example of pressing uppercase A

1await page.keyboard.press('Shift+KeyA'); 2// or 3await page.keyboard.press('Shift+A'); 4
1page.keyboard().press("Shift+KeyA"); 2// or 3page.keyboard().press("Shift+A"); 4
1await page.keyboard.press("Shift+KeyA") 2# or 3await page.keyboard.press("Shift+A") 4
1page.keyboard.press("Shift+KeyA") 2# or 3page.keyboard.press("Shift+A") 4
1await page.Keyboard.PressAsync("Shift+KeyA"); 2// or 3await page.Keyboard.PressAsync("Shift+A"); 4

An example to trigger select-all with the keyboard

1// on Windows and Linux 2await page.keyboard.press('Control+A'); 3// on macOS 4await page.keyboard.press('Meta+A'); 5
1// on Windows and Linux 2page.keyboard().press("Control+A"); 3// on macOS 4page.keyboard().press("Meta+A"); 5
1# on windows and linux 2await page.keyboard.press("Control+A") 3# on mac_os 4await page.keyboard.press("Meta+A") 5
1# on windows and linux 2page.keyboard.press("Control+A") 3# on mac_os 4page.keyboard.press("Meta+A") 5
1// on Windows and Linux 2await page.Keyboard.PressAsync("Control+A"); 3// on macOS 4await page.Keyboard.PressAsync("Meta+A"); 5

async method: Keyboard.down

  • since: v1.8

Dispatches a keydown event.

[param: key] can specify the intended keyboardEvent.key value or a single character to generate the text for. A superset of the [param: 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 [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.

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].

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. :::

param: Keyboard.down.key

  • since: v1.8
  • key <[string]>

Name of the key to press or a character to generate, such as ArrowLeft or a.

async method: Keyboard.insertText

  • since: v1.8

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

Usage

1page.keyboard.insertText('嗨'); 2
1page.keyboard().insertText("嗨"); 2
1await page.keyboard.insert_text("嗨") 2
1page.keyboard.insert_text("嗨") 2
1await page.Keyboard.PressAsync("嗨"); 2

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

param: Keyboard.insertText.text

  • since: v1.8
  • text <[string]>

Sets input to the specified text value.

async method: Keyboard.press

  • since: v1.8

:::tip In most cases, you should use [method: Locator.press] instead. :::

[param: key] can specify the intended keyboardEvent.key value or a single character to generate the text for. A superset of the [param: 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 [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.

Shortcuts such as key: "Control+o" or key: "Control+Shift+T" are supported as well. When specified with the modifier, modifier is pressed and being held while the subsequent key is being pressed.

Usage

1const page = await browser.newPage(); 2await page.goto('https://keycode.info'); 3await page.keyboard.press('A'); 4await page.screenshot({ path: 'A.png' }); 5await page.keyboard.press('ArrowLeft'); 6await page.screenshot({ path: 'ArrowLeft.png' }); 7await page.keyboard.press('Shift+O'); 8await page.screenshot({ path: 'O.png' }); 9await browser.close(); 10
1Page page = browser.newPage(); 2page.navigate("https://keycode.info"); 3page.keyboard().press("A"); 4page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("A.png")); 5page.keyboard().press("ArrowLeft"); 6page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("ArrowLeft.png"))); 7page.keyboard().press("Shift+O"); 8page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("O.png"))); 9browser.close(); 10
1page = await browser.new_page() 2await page.goto("https://keycode.info") 3await page.keyboard.press("a") 4await page.screenshot(path="a.png") 5await page.keyboard.press("ArrowLeft") 6await page.screenshot(path="arrow_left.png") 7await page.keyboard.press("Shift+O") 8await page.screenshot(path="o.png") 9await browser.close() 10
1page = browser.new_page() 2page.goto("https://keycode.info") 3page.keyboard.press("a") 4page.screenshot(path="a.png") 5page.keyboard.press("ArrowLeft") 6page.screenshot(path="arrow_left.png") 7page.keyboard.press("Shift+O") 8page.screenshot(path="o.png") 9browser.close() 10
1await page.GotoAsync("https://keycode.info"); 2await page.Keyboard.PressAsync("A"); 3await page.ScreenshotAsync(new() { Path = "A.png" }); 4await page.Keyboard.PressAsync("ArrowLeft"); 5await page.ScreenshotAsync(new() { Path = "ArrowLeft.png" }); 6await page.Keyboard.PressAsync("Shift+O"); 7await page.ScreenshotAsync(new() { Path = "O.png" }); 8await browser.CloseAsync(); 9

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

param: Keyboard.press.key

  • since: v1.8
  • key <[string]>

Name of the key to press or a character to generate, such as ArrowLeft or a.

option: Keyboard.press.delay

  • since: v1.8
  • delay <[float]>

Time to wait between keydown and keyup in milliseconds. Defaults to 0.

async method: Keyboard.type

  • since: v1.8

:::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]. :::

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].

Usage

1await page.keyboard.type('Hello'); // Types instantly 2await page.keyboard.type('World', { delay: 100 }); // Types slower, like a user 3
1// Types instantly 2page.keyboard().type("Hello"); 3// Types slower, like a user 4page.keyboard().type("World", new Keyboard.TypeOptions().setDelay(100)); 5
1await page.keyboard.type("Hello") # types instantly 2await page.keyboard.type("World", delay=100) # types slower, like a user 3
1page.keyboard.type("Hello") # types instantly 2page.keyboard.type("World", delay=100) # types slower, like a user 3
1await page.Keyboard.TypeAsync("Hello"); // types instantly 2await page.Keyboard.TypeAsync("World", new() { Delay = 100 }); // types slower, like a user 3

:::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. :::

param: Keyboard.type.text

  • since: v1.8
  • text <[string]>

A text to type into a focused element.

option: Keyboard.type.delay

  • since: v1.8
  • delay <[float]>

Time to wait between key presses in milliseconds. Defaults to 0.

async method: Keyboard.up

  • since: v1.8

Dispatches a keyup event.

param: Keyboard.up.key

  • since: v1.8
  • key <[string]>

Name of the key to press or a character to generate, such as ArrowLeft or a.