Interface Keyboard
-
public interface KeyboardKeyboard provides an api for managing a virtual keyboard. The high level api isKeyboard.type(), which takes raw characters and generates properkeydown,keypress/input, andkeyupevents on your page.For finer control, you can use
Keyboard.down(),Keyboard.up(), andKeyboard.insertText()to manually fire events as if they were generated from a real keyboard.An example of holding down
Shiftin order to select and delete some text:page.keyboard().type("Hello World!"); page.keyboard().press("ArrowLeft"); page.keyboard().down("Shift"); for (int i = 0; i < " World".length(); i++) page.keyboard().press("ArrowLeft"); page.keyboard().up("Shift"); page.keyboard().press("Backspace"); // Result text will end up saying "Hello!"An example of pressing uppercase
Apage.keyboard().press("Shift+KeyA"); // or page.keyboard().press("Shift+A");An example to trigger select-all with the keyboard
// on Windows and Linux page.keyboard().press("Control+A"); // on macOS page.keyboard().press("Meta+A");
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classKeyboard.PressOptionsstatic classKeyboard.TypeOptions
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description voiddown(String key)Dispatches akeydownevent.voidinsertText(String text)Dispatches onlyinputevent, does not emit thekeydown,keyuporkeypressevents.default voidpress(String key)NOTE: In most cases, you should useLocator.press()instead.voidpress(String key, Keyboard.PressOptions options)NOTE: In most cases, you should useLocator.press()instead.default voidtype(String text)NOTE: In most cases, you should useLocator.fill()instead.voidtype(String text, Keyboard.TypeOptions options)NOTE: In most cases, you should useLocator.fill()instead.voidup(String key)Dispatches akeyupevent.
-
-
-
Method Detail
-
down
void down(String key)
Dispatches akeydownevent.keycan specify the intended keyboardEvent.key value or a single character to generate the text for. A superset of thekeyvalues 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.ControlOrMetaresolves toControlon Windows and Linux and toMetaon macOS.Holding down
Shiftwill type the text that corresponds to thekeyin the upper case.If
keyis a single character, it is case-sensitive, so the valuesaandAwill generate different respective texts.If
keyis a modifier key,Shift,Meta,Control, orAlt, subsequent key presses will be sent with that modifier active. To release the modifier key, useKeyboard.up().After the key is pressed once, subsequent calls to
Keyboard.down()will have repeat set to true. To release the key, useKeyboard.up().NOTE: Modifier keys DO influence
keyboard.down. Holding downShiftwill type the text in upper case.- Parameters:
key- Name of the key to press or a character to generate, such asArrowLeftora.- Since:
- v1.8
-
insertText
void insertText(String text)
Dispatches onlyinputevent, does not emit thekeydown,keyuporkeypressevents.Usage
page.keyboard().insertText("嗨");NOTE: Modifier keys DO NOT effect
keyboard.insertText. Holding downShiftwill not type the text in upper case.- Parameters:
text- Sets input to the specified text value.- Since:
- v1.8
-
press
default void press(String key)
NOTE: In most cases, you should useLocator.press()instead.keycan specify the intended keyboardEvent.key value or a single character to generate the text for. A superset of thekeyvalues 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.ControlOrMetaresolves toControlon Windows and Linux and toMetaon macOS.Holding down
Shiftwill type the text that corresponds to thekeyin the upper case.If
keyis a single character, it is case-sensitive, so the valuesaandAwill generate different respective texts.Shortcuts such as
key: "Control+o",key: "Control++orkey: "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
Page page = browser.newPage(); page.navigate("https://keycode.info"); page.keyboard().press("A"); page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("A.png")); page.keyboard().press("ArrowLeft"); page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("ArrowLeft.png"))); page.keyboard().press("Shift+O"); page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("O.png"))); browser.close();Shortcut for
Keyboard.down()andKeyboard.up().- Parameters:
key- Name of the key to press or a character to generate, such asArrowLeftora.- Since:
- v1.8
-
press
void press(String key, Keyboard.PressOptions options)
NOTE: In most cases, you should useLocator.press()instead.keycan specify the intended keyboardEvent.key value or a single character to generate the text for. A superset of thekeyvalues 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.ControlOrMetaresolves toControlon Windows and Linux and toMetaon macOS.Holding down
Shiftwill type the text that corresponds to thekeyin the upper case.If
keyis a single character, it is case-sensitive, so the valuesaandAwill generate different respective texts.Shortcuts such as
key: "Control+o",key: "Control++orkey: "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
Page page = browser.newPage(); page.navigate("https://keycode.info"); page.keyboard().press("A"); page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("A.png")); page.keyboard().press("ArrowLeft"); page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("ArrowLeft.png"))); page.keyboard().press("Shift+O"); page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("O.png"))); browser.close();Shortcut for
Keyboard.down()andKeyboard.up().- Parameters:
key- Name of the key to press or a character to generate, such asArrowLeftora.- Since:
- v1.8
-
type
default void type(String text)
NOTE: In most cases, you should useLocator.fill()instead. You only need to press keys one by one if there is special keyboard handling on the page - in this case useLocator.pressSequentially().Sends a
keydown,keypress/input, andkeyupevent for each character in the text.To press a special key, like
ControlorArrowDown, useKeyboard.press().Usage
// Types instantly page.keyboard().type("Hello"); // Types slower, like a user page.keyboard().type("World", new Keyboard.TypeOptions().setDelay(100));NOTE: Modifier keys DO NOT effect
keyboard.type. Holding downShiftwill not type the text in upper case.NOTE: For characters that are not on a US keyboard, only an
inputevent will be sent.- Parameters:
text- A text to type into a focused element.- Since:
- v1.8
-
type
void type(String text, Keyboard.TypeOptions options)
NOTE: In most cases, you should useLocator.fill()instead. You only need to press keys one by one if there is special keyboard handling on the page - in this case useLocator.pressSequentially().Sends a
keydown,keypress/input, andkeyupevent for each character in the text.To press a special key, like
ControlorArrowDown, useKeyboard.press().Usage
// Types instantly page.keyboard().type("Hello"); // Types slower, like a user page.keyboard().type("World", new Keyboard.TypeOptions().setDelay(100));NOTE: Modifier keys DO NOT effect
keyboard.type. Holding downShiftwill not type the text in upper case.NOTE: For characters that are not on a US keyboard, only an
inputevent will be sent.- Parameters:
text- A text to type into a focused element.- Since:
- v1.8
-
up
void up(String key)
Dispatches akeyupevent.- Parameters:
key- Name of the key to press or a character to generate, such asArrowLeftora.- Since:
- v1.8
-
-