Keyboard
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.
为了更精细的控制,你可以使用 keyboard.down()、keyboard.up() 和 keyboard.insert_text() 来手动触发事件,就像它们是由真实键盘生成的一样。
🌐 For finer control, you can use keyboard.down(), keyboard.up(), and keyboard.insert_text() 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:
- Sync
- Async
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!"
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!"
按大写键 A 的示例
🌐 An example of pressing uppercase A
- Sync
- Async
page.keyboard.press("Shift+KeyA")
# or
page.keyboard.press("Shift+A")
await page.keyboard.press("Shift+KeyA")
# or
await page.keyboard.press("Shift+A")
使用键盘触发全选的示例
🌐 An example to trigger select-all with the keyboard
- Sync
- Async
page.keyboard.press("ControlOrMeta+A")
await page.keyboard.press("ControlOrMeta+A")
方法
🌐 Methods
down
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.up()。
🌐 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.
:::
用法
keyboard.down(key)
参数
返回
insert_text
Added before v1.9仅分发 input 事件,不触发 keydown、keyup 或 keypress 事件。
🌐 Dispatches only input event, does not emit the keydown, keyup or keypress events.
用法
- Sync
- Async
page.keyboard.insert_text("嗨")
await page.keyboard.insert_text("嗨")
修饰键不会影响 keyboard.insertText。按住 Shift 不会将文本输入为大写。:::
🌐 Modifier keys DO NOT effect keyboard.insertText. Holding down Shift will not type the text in upper case.
参数
返回
press
Added before v1.9在大多数情况下,你应该改用 locator.press()。
🌐 In most cases, you should use locator.press() 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.
用法
- Sync
- Async
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()
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()
keyboard.down() 和 keyboard.up() 的快捷方式。
🌐 Shortcut for keyboard.down() and keyboard.up().
参数
-
输入按键名称或生成字符名称,如“ArrowLeft”或“a”。
-
keydown和keyup之间的等待时间,以毫秒为单位。默认值为 0。
返回
type
Added before v1.9在大多数情况下,你应该使用 locator.fill()。只有在页面上有特殊的键盘处理时,才需要逐个按键——在这种情况下使用 locator.press_sequentially()。
🌐 In most cases, you should use 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 locator.press_sequentially().
为文本中的每个字符发送 keydown、keypress/input 和 keyup 事件。
🌐 Sends a keydown, keypress/input, and keyup event for each character in the text.
要按下特殊键,如 Control 或 ArrowDown,请使用 keyboard.press()。
🌐 To press a special key, like Control or ArrowDown, use keyboard.press().
用法
- Sync
- Async
page.keyboard.type("Hello") # types instantly
page.keyboard.type("World", delay=100) # types slower, like a user
await page.keyboard.type("Hello") # types instantly
await page.keyboard.type("World", delay=100) # types slower, like a user
修饰键不会影响 keyboard.type。按住 Shift 不会将文本输入为大写。:::
🌐 Modifier keys DO NOT effect keyboard.type. Holding down Shift will not type the text in upper case.
对于不在美式键盘上的字符,只会发送 input 事件。
参数
返回
up
Added before v1.9派发一个 keyup 事件。
🌐 Dispatches a keyup event.
用法
keyboard.up(key)
参数
返回