Interface IKeyboard
- Namespace
- Microsoft.Playwright
- Assembly
- Microsoft.Playwright.dll
Keyboard provides an api for managing a virtual keyboard. The high level api is
TypeAsync(string, KeyboardTypeOptions?), which takes raw characters and generates proper
keydown
, keypress
/input
, and keyup
events on your page.
For finer control, you can use DownAsync(string), UpAsync(string), and InsertTextAsync(string) 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:
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!"
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
// on Windows and Linux
await page.Keyboard.PressAsync("Control+A");
// on macOS
await page.Keyboard.PressAsync("Meta+A");
public interface IKeyboard
Methods
DownAsync(string)
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
, ControlOrMeta
. ControlOrMeta
resolves to Control
on Windows and Linux and to Meta
on macOS.
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 UpAsync(string).
After the key is pressed once, subsequent calls to DownAsync(string) will have repeat set to true. To release the key, use UpAsync(string).
Task DownAsync(string key)
Parameters
key
stringName of the key to press or a character to generate, such as
ArrowLeft
ora
.
Returns
Remarks
Modifier keys DO influence keyboard.down
. Holding down Shift
will
type the text in upper case.
InsertTextAsync(string)
Dispatches only input
event, does not emit the keydown
, keyup
or keypress
events.
**Usage**
await page.Keyboard.PressAsync("嗨");
Task InsertTextAsync(string text)
Parameters
text
stringSets input to the specified text value.
Returns
Remarks
Modifier keys DO NOT effect keyboard.insertText
. Holding down Shift
will not type the text in upper case.
PressAsync(string, KeyboardPressOptions?)
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
, ControlOrMeta
. ControlOrMeta
resolves to Control
on Windows and Linux and to Meta
on macOS.
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"
, 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.
**Usage**
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();
Shortcut for DownAsync(string) and UpAsync(string).
Task PressAsync(string key, KeyboardPressOptions? options = null)
Parameters
key
stringName of the key to press or a character to generate, such as
ArrowLeft
ora
.options
KeyboardPressOptionsCall options
Returns
Remarks
In most cases, you should use PressAsync(string, LocatorPressOptions?) instead.
TypeAsync(string, KeyboardTypeOptions?)
Sends a keydown
, keypress
/input
, and keyup
event for
each character in the text.
To press a special key, like Control
or ArrowDown
, use PressAsync(string, KeyboardPressOptions?).
**Usage**
await page.Keyboard.TypeAsync("Hello"); // types instantly
await page.Keyboard.TypeAsync("World", new() { Delay = 100 }); // types slower, like a user
Task TypeAsync(string text, KeyboardTypeOptions? options = null)
Parameters
text
stringA text to type into a focused element.
options
KeyboardTypeOptionsCall options
Returns
Remarks
In most cases, you should use FillAsync(string, LocatorFillOptions?) instead. You only need to press keys one by one if there is special keyboard handling on the page - in this case use PressSequentiallyAsync(string, LocatorPressSequentiallyOptions?).
Modifier keys DO NOT effect keyboard.type
. Holding down Shift
will
not type the text in upper case.
For characters that are not on a US keyboard, only an input
event will be
sent.
UpAsync(string)
Dispatches a keyup
event.
Task UpAsync(string key)
Parameters
key
stringName of the key to press or a character to generate, such as
ArrowLeft
ora
.