java.lang.Object
org.htmlunit.html.Keyboard
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 ClassesModifier and TypeClassDescriptionstatic interfaceRepresents a single keyboard input action.static final recordA key-code press or release, e.g. viapress(int)orrelease(int).static final recordA character typed directly, e.g. viatype(char). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all keys.booleanReturns whether typing should start at the text end or not.voidpress(int keyCode) Press the specified key code (without releasing it).voidrelease(int keyCode) Releases the specified key code.voidtype(char ch) Types the specified character.
-
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
-