Keyboard
Keyboard provides an api for managing a virtual keyboard. The high level api is keyboard.type(text, **kwargs), which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page.
For finer control, you can use keyboard.down(key), keyboard.up(key), and keyboard.insert_text(text) 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:
- Sync
- Async
An example of pressing uppercase A
- Sync
- Async
An example to trigger select-all with the keyboard
- Sync
- Async
- keyboard.down(key)
- keyboard.insert_text(text)
- keyboard.press(key, **kwargs)
- keyboard.type(text, **kwargs)
- keyboard.up(key)
#
keyboard.down(key)key
<str> Name of the key to press or a character to generate, such asArrowLeft
ora
.
Dispatches a keydown
event.
key
can specify the intended keyboardEvent.key value or a single character to generate the text for. A superset of the 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 key
in the upper case.
If key
is a single character, it is case-sensitive, so the values a
and A
will generate different respective texts.
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(key).
After the key is pressed once, subsequent calls to keyboard.down(key) will have repeat set to true. To release the key, use keyboard.up(key).
note
Modifier keys DO influence keyboard.down
. Holding down Shift
will type the text in upper case.
#
keyboard.insert_text(text)text
<str> Sets input to the specified text value.
Dispatches only input
event, does not emit the keydown
, keyup
or keypress
events.
- Sync
- Async
note
Modifier keys DO NOT effect keyboard.insertText
. Holding down Shift
will not type the text in upper case.
#
keyboard.press(key, **kwargs)key
<str> Name of the key to press or a character to generate, such asArrowLeft
ora
.delay
<float> Time to wait betweenkeydown
andkeyup
in milliseconds. Defaults to 0.
key
can specify the intended keyboardEvent.key value or a single character to generate the text for. A superset of the 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 key
in the upper case.
If 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 speficied with the modifier, modifier is pressed and being held while the subsequent key is being pressed.
- Sync
- Async
Shortcut for keyboard.down(key) and keyboard.up(key).
#
keyboard.type(text, **kwargs)text
<str> A text to type into a focused element.delay
<float> Time to wait between key presses in milliseconds. Defaults to 0.
Sends a keydown
, keypress
/input
, and keyup
event for each character in the text.
To press a special key, like Control
or ArrowDown
, use keyboard.press(key, **kwargs).
- Sync
- Async
note
Modifier keys DO NOT effect keyboard.type
. Holding down Shift
will not type the text in upper case.
#
keyboard.up(key)key
<str> Name of the key to press or a character to generate, such asArrowLeft
ora
.
Dispatches a keyup
event.