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) | 分发一个 🌐 Dispatches a 附注 如果 🌐 If 按下键一次后,对 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()。按住 🌐 Modifier keys DO influence Keyboard.down(). Holding down | |
| press(key, options) | Keyboard.down() 和 Keyboard.up() 的快捷方式。 🌐 Shortcut for Keyboard.down() and Keyboard.up(). 附注 如果 🌐 If 修改键会影响 Keyboard.press()。按住 🌐 Modifier keys DO effect Keyboard.press(). Holding down | |
| sendCharacter(char) | 分发一个 🌐 Dispatches a 附注 修饰键不会影响 Keyboard.sendCharacter。按住 🌐 Modifier keys DO NOT effect Keyboard.sendCharacter. Holding down | |
| type(text, options) | 为文本中的每个字符发送一个 🌐 Sends a 附注 要按下特殊键,例如 🌐 To press a special key, like 修饰键不会影响 🌐 Modifier keys DO NOT effect | |
| up(key) | 分发一个 🌐 Dispatches a |