Skip to main content
Version: 24.38.0

Keyboard 类

🌐 Keyboard class

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

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

语法

🌐 Signature

export declare abstract class Keyboard

附注

🌐 Remarks

为了更精细的控制,你可以使用 Keyboard.down()Keyboard.up()Keyboard.sendCharacter() 来手动触发事件,就像它们是由真实键盘生成的一样。

🌐 For finer control, you can use Keyboard.down(), Keyboard.up(), and Keyboard.sendCharacter() to manually fire events as if they were generated from a real keyboard.

在 macOS 上,像 ⌘ A -> 全选 这样的键盘快捷键不能使用。请参见 #1313

🌐 On macOS, keyboard shortcuts like ⌘ A -> Select All do not work. See #1313.

这个类的构造函数被标记为内部。第三方代码不应直接调用构造函数或创建继承 Keyboard 类的子类。

🌐 The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the Keyboard class.

示例 1

🌐 Example 1

按住 Shift 以选择并删除某些文本的示例:

🌐 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');
// Result text will end up saying 'Hello!'

示例 2

🌐 Example 2

按下 A 的一个例子

🌐 An example of pressing A

await page.keyboard.down('Shift');
await page.keyboard.press('KeyA');
await page.keyboard.up('Shift');

方法

🌐 Methods

方法

🌐 Method

修饰符

🌐 Modifiers

描述

🌐 Description

down(key, options)

分发一个 keydown 事件。

🌐 Dispatches a keydown event.

附注

如果 key 是单个字符,并且除了 Shift 外没有按下其他修改键,将会生成 keypress/input 事件。可以指定 text 选项来强制生成输入事件。如果 key 是一个修改键 ShiftMetaControlAlt,后续的按键将会在该修改键激活的状态下发送。要释放修改键,请使用 Keyboard.up()

🌐 If key is a single character and no modifier keys besides Shift are being held down, a keypress/input event will also generated. The text option can be specified to force an input event to be generated. 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 Keyboard.up().

按下键一次后,对 Keyboard.down() 的后续调用将会把 repeat 设置为 true。要释放按键,请使用 Keyboard.up()

🌐 After the key is pressed once, subsequent calls to Keyboard.down() will have repeat set to true. To release the key, use Keyboard.up().

修改键确实会影响 Keyboard.down()。按住 Shift 将会以大写输入文本。

🌐 Modifier keys DO influence Keyboard.down(). Holding down Shift will type the text in upper case.

press(key, options)

Keyboard.down()Keyboard.up() 的快捷方式。

🌐 Shortcut for Keyboard.down() and Keyboard.up().

附注

如果 key 是单个字符,并且除了 Shift 之外没有按下其他修饰键,将会生成一个 keypress/input 事件。可以指定 text 选项以强制生成输入事件。

🌐 If key is a single character and no modifier keys besides Shift are being held down, a keypress/input event will also generated. The text option can be specified to force an input event to be generated.

修改键会影响 Keyboard.press()。按住 Shift 将以大写形式输入文本。

🌐 Modifier keys DO effect Keyboard.press(). Holding down Shift will type the text in upper case.

sendCharacter(char)

分发一个 keypressinput 事件。这不会发送 keydownkeyup 事件。

🌐 Dispatches a keypress and input event. This does not send a keydown or keyup event.

附注

修饰键不会影响 Keyboard.sendCharacter。按住 Shift 不会将文本输入为大写。

🌐 Modifier keys DO NOT effect Keyboard.sendCharacter. Holding down Shift will not type the text in upper case.

type(text, options)

为文本中的每个字符发送一个 keydownkeypress/inputkeyup 事件。

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

附注

要按下特殊键,例如 ControlArrowDown,请使用 Keyboard.press()

🌐 To press a special key, like Control or ArrowDown, use Keyboard.press().

修饰键不会影响 keyboard.type。按住 Shift 不会将文本输入为大写。

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

up(key)

分发一个 keyup 事件。

🌐 Dispatches a keyup event.