Module org.htmlunit

Class Keyboard

java.lang.Object
org.htmlunit.html.Keyboard

public class Keyboard extends Object
Represents a sequence of keyboard actions—such as typed characters, key presses, and key releases—used to simulate realistic user input on HTML elements.

A Keyboard instance acts as a keystroke recorder that queues key actions and maintains modifier state (SHIFT, CONTROL, ALT) across sequential events. When executed against an element via element.type(keyboard), it drives the full browser event lifecycle (keydown, keypress, input, and keyup) and mutates DOM text values accordingly.

Core Features

  • Character and Key Code Queuing: Supports typing printable characters as well as holding and releasing raw key codes (e.g., navigation, BACK_SPACE, or modifier keys).
  • Modifier State Persistence: Tracks modifier keys so subsequent keystrokes inherit the active modifier flags during JavaScript event dispatching.
  • Caret Positioning: Supports configured cursor placement (isStartAtEnd()) to position the selection caret at the end of input fields before typing begins.
Author:
Ahmed Ashour, Ronald Brill
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Represents a single keyboard input action.
    static final record 
    A key-code press or release, e.g. via press(int) or release(int).
    static final record 
    A character typed directly, e.g. via type(char).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance.
    Keyboard(boolean startAtEnd)
    Creates a new instance, specifying whether typing should start at the text end or not.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears all keys.
    boolean
    Returns whether typing should start at the text end or not.
    void
    press(int keyCode)
    Press the specified key code (without releasing it).
    void
    release(int keyCode)
    Releases the specified key code.
    void
    type(char ch)
    Types the specified character.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Keyboard

      public Keyboard()
      Creates a new instance.
    • Keyboard

      public Keyboard(boolean startAtEnd)
      Creates a new instance, specifying whether typing should start at the text end or not.
      Parameters:
      startAtEnd - whether typing should start at the text end or not
  • Method Details

    • type

      public void type(char ch)
      Types the specified character.
      Parameters:
      ch - the character
    • press

      public void press(int keyCode)
      Press the specified key code (without releasing it).

      An example of predefined values is KeyboardEvent.DOM_VK_PAGE_DOWN.

      Parameters:
      keyCode - the key code
    • release

      public void release(int keyCode)
      Releases the specified key code.

      An example of predefined values is KeyboardEvent.DOM_VK_PAGE_DOWN.

      Parameters:
      keyCode - the key code.
    • clear

      public void clear()
      Clears all keys.
    • isStartAtEnd

      public boolean isStartAtEnd()
      Returns whether typing should start at the text end or not.
      Returns:
      whether typing should start at the text end or not