class: Keyboard

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.

For finer control, you can use keyboard.down, keyboard.up, and keyboard.insertText 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:

  1. await page.keyboard.type('Hello World!');
  2. await page.keyboard.press('ArrowLeft');
  3. await page.keyboard.down('Shift');
  4. for (let i = 0; i < ' World'.length; i++)
  5. await page.keyboard.press('ArrowLeft');
  6. await page.keyboard.up('Shift');
  7. await page.keyboard.press('Backspace');
  8. // Result text will end up saying 'Hello!'

An example of pressing A

  1. await page.keyboard.press('Shift+KeyA');
  2. // or
  3. await page.keyboard.press('Shift+A');

NOTE On MacOS, keyboard shortcuts like ⌘ A -> Select All do not work. See #1313

keyboard.down(key)

  • key <string> Name of the key to press or a character to generate, such as ArrowLeft or a.
  • returns: <Promise>

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, ArrayRight, ArrowUp, etc.

Following modification shortcuts are also suported: 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.

After the key is pressed once, subsequent calls to keyboard.down will have repeat set to true. To release the key, use keyboard.up.

NOTE Modifier keys DO influence keyboard.down. Holding down Shift will type the text in upper case.

keyboard.insertText(text)

  • text <string> Sets input to the specified text value.
  • returns: <Promise>

Dispatches only input event, does not emit the keydown, keyup or keypress events.

  1. page.keyboard.insertText('嗨');

NOTE Modifier keys DO NOT effect keyboard.insertText. Holding down Shift will not type the text in upper case.

keyboard.press(key[, options])

  • key <string> Name of the key to press or a character to generate, such as ArrowLeft or a.
  • options <Object>
    • delay <number> Time to wait between keydown and keyup in milliseconds. Defaults to 0.
  • returns: <Promise>

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, ArrayRight, ArrowUp, etc.

Following modification shortcuts are also suported: 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.

  1. const page = await browser.newPage();
  2. await page.goto('https://keycode.info');
  3. await page.keyboard.press('A');
  4. await page.screenshot({ path: 'A.png' });
  5. await page.keyboard.press('ArrowLeft');
  6. await page.screenshot({ path: 'ArrowLeft.png' });
  7. await page.keyboard.press('Shift+O');
  8. await page.screenshot({ path: 'O.png' });
  9. await browser.close();

Shortcut for keyboard.down and keyboard.up.

keyboard.type(text[, options])

  • text <string> A text to type into a focused element.
  • options <Object>
    • delay <number> Time to wait between key presses in milliseconds. Defaults to 0.
  • returns: <Promise>

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.

  1. await page.keyboard.type('Hello'); // Types instantly
  2. await page.keyboard.type('World', {delay: 100}); // Types slower, like a user

NOTE Modifier keys DO NOT effect keyboard.type. Holding down Shift will not type the text in upper case.

keyboard.up(key)

  • key <string> Name of the key to press or a character to generate, such as ArrowLeft or a.
  • returns: <Promise>

Dispatches a keyup event.