Class KeyboardImpl

  • All Implemented Interfaces:
    Keyboard

    class KeyboardImpl
    extends java.lang.Object
    implements Keyboard
    • Method Detail

      • down

        public void down​(java.lang.String key)
        Description copied from interface: Keyboard
        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().

        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.

        Specified by:
        down in interface Keyboard
        Parameters:
        key - Name of the key to press or a character to generate, such as ArrowLeft or a.
      • insertText

        public void insertText​(java.lang.String text)
        Description copied from interface: Keyboard
        Dispatches only input event, does not emit the keydown, keyup or keypress events.

        **Usage**

        
         page.keyboard().insertText("嗨");
         

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

        Specified by:
        insertText in interface Keyboard
        Parameters:
        text - Sets input to the specified text value.
      • press

        public void press​(java.lang.String key,
                          Keyboard.PressOptions options)
        Description copied from interface: Keyboard
        NOTE: In most cases, you should use Locator.press() instead.

        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 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() and Keyboard.up().

        Specified by:
        press in interface Keyboard
        Parameters:
        key - Name of the key to press or a character to generate, such as ArrowLeft or a.
      • type

        public void type​(java.lang.String text,
                         Keyboard.TypeOptions options)
        Description copied from interface: Keyboard
        NOTE: 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.pressSequentially().

        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().

        **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 down Shift will not type the text in upper case.

        NOTE: For characters that are not on a US keyboard, only an input event will be sent.

        Specified by:
        type in interface Keyboard
        Parameters:
        text - A text to type into a focused element.
      • up

        public void up​(java.lang.String key)
        Description copied from interface: Keyboard
        Dispatches a keyup event.
        Specified by:
        up in interface Keyboard
        Parameters:
        key - Name of the key to press or a character to generate, such as ArrowLeft or a.