Keyboard
Keyboard 提供了一个用于管理虚拟键盘的 API。高级 API 是 Keyboard.TypeAsync(),它接受原始字符并在你的页面上生成适当的 keydown、keypress/input 和 keyup 事件。
🌐 Keyboard provides an api for managing a virtual keyboard. The high level api is Keyboard.TypeAsync(), which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page.
为了更精确的控制,你可以使用 Keyboard.DownAsync()、Keyboard.UpAsync() 和 Keyboard.InsertTextAsync() 来手动触发事件,就像它们是从真实键盘生成的一样。
🌐 For finer control, you can use Keyboard.DownAsync(), Keyboard.UpAsync(), and Keyboard.InsertTextAsync() to manually fire events as if they were generated from a real keyboard.
按住 Shift 键以选择并删除部分文本的示例:
🌐 An example of holding down Shift in order to select and delete some text:
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!"
按大写键 A 的示例
🌐 An example of pressing uppercase A
await page.Keyboard.PressAsync("Shift+KeyA");
// or
await page.Keyboard.PressAsync("Shift+A");
使用键盘触发全选的示例
🌐 An example to trigger select-all with the keyboard
await page.Keyboard.PressAsync("ControlOrMeta+A");
方法
🌐 Methods
DownAsync
Added before v1.9派发一个 keydown 事件。
🌐 Dispatches a keydown event.
key 可以指定预期的 keyboardEvent.key 值或用于生成文本的单个字符。key 值的超集可以在 这里 找到。键的示例有:
F1 - F12、Digit0- Digit9、KeyA- KeyZ、Backquote、Minus、Equal、Backslash、Backspace、Tab、Delete、Escape、ArrowDown、End、Enter、Home、Insert、PageDown、PageUp、ArrowRight、ArrowUp,等等。
以下修改快捷键也受支持:Shift、Control、Alt、Meta、ShiftLeft、ControlOrMeta。ControlOrMeta 在 Windows 和 Linux 上解析为 Control,在 macOS 上解析为 Meta。
🌐 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.
按住 Shift 将会输入与大写形式的 key 对应的文本。
🌐 Holding down Shift will type the text that corresponds to the key in the upper case.
如果 key 是单个字符,则区分大小写,因此值 a 和 A 会生成不同的文本。
🌐 If key is a single character, it is case-sensitive, so the values a and A will generate different respective texts.
如果 key 是一个修饰键,如 Shift、Meta、Control 或 Alt,后续的按键将会在该修饰键处于激活状态下发送。要释放修饰键,请使用 Keyboard.UpAsync()。
🌐 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.UpAsync().
按下一次键后,对 Keyboard.DownAsync() 的后续调用将会把 repeat 设置为 true。要释放键,请使用 Keyboard.UpAsync()。
🌐 After the key is pressed once, subsequent calls to Keyboard.DownAsync() will have repeat set to true. To release the key, use Keyboard.UpAsync().
修改键确实会影响 keyboard.down。按住 Shift 会以大写输入文本。
🌐 Modifier keys DO influence keyboard.down. Holding down Shift will type the text in upper case.
:::
用法
await Keyboard.DownAsync(key);
参数
返回
InsertTextAsync
Added before v1.9仅分发 input 事件,不触发 keydown、keyup 或 keypress 事件。
🌐 Dispatches only input event, does not emit the keydown, keyup or keypress events.
用法
await page.Keyboard.PressAsync("嗨");
修改键不会影响 keyboard.insertText。按住 Shift 不会以大写输入文本。
参数
返回
PressAsync
Added before v1.9在大多数情况下,你应该使用 Locator.PressAsync() 替代。
🌐 In most cases, you should use Locator.PressAsync() instead.
key 可以指定预期的 keyboardEvent.key 值或用于生成文本的单个字符。key 值的超集可以在 这里 找到。键的示例有:
F1 - F12、Digit0- Digit9、KeyA- KeyZ、Backquote、Minus、Equal、Backslash、Backspace、Tab、Delete、Escape、ArrowDown、End、Enter、Home、Insert、PageDown、PageUp、ArrowRight、ArrowUp,等等。
以下修改快捷键也受支持:Shift、Control、Alt、Meta、ShiftLeft、ControlOrMeta。ControlOrMeta 在 Windows 和 Linux 上解析为 Control,在 macOS 上解析为 Meta。
🌐 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.
按住 Shift 将会输入与大写形式的 key 对应的文本。
🌐 Holding down Shift will type the text that corresponds to the key in the upper case.
如果 key 是单个字符,则区分大小写,因此值 a 和 A 会生成不同的文本。
🌐 If key is a single character, it is case-sensitive, so the values a and A will generate different respective texts.
也支持诸如 key: "Control+o"、key: "Control++ 或 key: "Control+Shift+T" 之类的快捷键。当与修饰键一起指定时,在按下后续按键的同时按住修饰键。
🌐 Shortcuts such as key: "Control+o", key: "Control++ 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.
用法
await page.GotoAsync("https://keycode.info");
await page.Keyboard.PressAsync("A");
await page.ScreenshotAsync(new() { Path = "A.png" });
await page.Keyboard.PressAsync("ArrowLeft");
await page.ScreenshotAsync(new() { Path = "ArrowLeft.png" });
await page.Keyboard.PressAsync("Shift+O");
await page.ScreenshotAsync(new() { Path = "O.png" });
await browser.CloseAsync();
Keyboard.DownAsync() 和 Keyboard.UpAsync() 的快捷方式。
🌐 Shortcut for Keyboard.DownAsync() and Keyboard.UpAsync().
参数
-
输入按键名称或生成字符名称,如“ArrowLeft”或“a”。
-
optionsKeyboardPressOptions?(optional)-
Delay[float]? (optional)#keydown和keyup之间的等待时间,以毫秒为单位。默认值为 0。
-
返回
TypeAsync
Added before v1.9在大多数情况下,你应该使用 Locator.FillAsync()。只有在页面上有特殊键盘处理时,才需要逐个按键 — 在这种情况下使用 Locator.PressSequentiallyAsync()。
🌐 In most cases, you should use Locator.FillAsync() instead. You only need to press keys one by one if there is special keyboard handling on the page - in this case use Locator.PressSequentiallyAsync().
为文本中的每个字符发送 keydown、keypress/input 和 keyup 事件。
🌐 Sends a keydown, keypress/input, and keyup event for each character in the text.
要按下特殊键,例如 Control 或 ArrowDown,请使用 Keyboard.PressAsync()。
🌐 To press a special key, like Control or ArrowDown, use Keyboard.PressAsync().
用法
await page.Keyboard.TypeAsync("Hello"); // types instantly
await page.Keyboard.TypeAsync("World", new() { Delay = 100 }); // types slower, like a user
修改键不会影响 keyboard.type。按住 Shift 不会以大写输入文本。
对于不在美式键盘上的字符,只会发送 input 事件。
参数
-
要输入到焦点元素中的文本。
-
optionsKeyboardTypeOptions?(optional)-
Delay[float]? (optional)#按键之间的等待时间,以毫秒为单位。默认为 0。
-
返回
UpAsync
Added before v1.9派发一个 keyup 事件。
🌐 Dispatches a keyup event.
用法
await Keyboard.UpAsync(key);
参数
返回