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, META) 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 final recordOnly the key down or only the key up of a character, e.g. viakeyDown(char)orkeyUp(char).static interfaceRepresents a single keyboard input action.static final recordA key-code press or release, e.g. viapress(int)orrelease(int).static final recordOnly the key down or only the key up of a key code, e.g. viakeyDown(int)orkeyUp(int, int).static final recordA character typed directly, e.g. viatype(char). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intUse the default location of the key code (e.g. the left one for the shift key). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all keys.booleanReturns whether typing should start at the text end or not.voidkeyDown(char ch) Presses only the key of the specified character; the key stays pressed untilkeyUp(char)is called for the same character.voidkeyDown(int keyCode) Presses only the specified key code; the key stays pressed untilkeyUp(int)is called for the same key code (in contrast topress(int)nokeyupis fired for keys that are not modifiers).voidkeyDown(int keyCode, int location) Presses only the specified key code at the specified location; the key stays pressed untilkeyUp(int, int)is called for the same key.voidkeyUp(char ch) Releases the key of the specified character pressed bykeyDown(char); only thekeyupevent is fired.voidkeyUp(int keyCode) Releases the specified key code pressed bykeyDown(int); only thekeyupevent is fired.voidkeyUp(int keyCode, int location) Releases the specified key code at the specified location pressed bykeyDown(int, int); only thekeyupevent is fired.voidpress(int keyCode) Press the specified key code (without releasing it).voidrelease(int keyCode) Releases the specified key code.voidsetPressedModifiers(boolean shift, boolean ctrl, boolean alt, boolean meta) Sets the state of the modifier keys that are already pressed when this keyboard is typed (e.g. pressed by a previous keyboard); no events are fired for these keys.voidtype(char ch) Types the specified character.
-
Field Details
-
DEFAULT_LOCATION
public static final int DEFAULT_LOCATIONUse the default location of the key code (e.g. the left one for the shift key).- See Also:
-
-
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.
-
keyDown
public void keyDown(char ch) Presses only the key of the specified character; the key stays pressed untilkeyUp(char)is called for the same character. Like in the browsers, thekeydownandkeypressevents are fired and the character is typed.- Parameters:
ch- the character
-
keyUp
public void keyUp(char ch) Releases the key of the specified character pressed bykeyDown(char); only thekeyupevent is fired.- Parameters:
ch- the character
-
keyDown
public void keyDown(int keyCode) Presses only the specified key code; the key stays pressed untilkeyUp(int)is called for the same key code (in contrast topress(int)nokeyupis fired for keys that are not modifiers).- Parameters:
keyCode- the key code (e.g.KeyboardEvent.DOM_VK_LEFT)
-
keyDown
public void keyDown(int keyCode, int location) Presses only the specified key code at the specified location; the key stays pressed untilkeyUp(int, int)is called for the same key.- Parameters:
keyCode- the key code (e.g.KeyboardEvent.DOM_VK_SHIFT)location- the location of the key (e.g.KeyboardEvent.DOM_KEY_LOCATION_RIGHTfor the right shift key) orDEFAULT_LOCATION
-
keyUp
public void keyUp(int keyCode) Releases the specified key code pressed bykeyDown(int); only thekeyupevent is fired.- Parameters:
keyCode- the key code (e.g.KeyboardEvent.DOM_VK_LEFT)
-
keyUp
public void keyUp(int keyCode, int location) Releases the specified key code at the specified location pressed bykeyDown(int, int); only thekeyupevent is fired.- Parameters:
keyCode- the key code (e.g.KeyboardEvent.DOM_VK_SHIFT)location- the location of the key (e.g.KeyboardEvent.DOM_KEY_LOCATION_RIGHTfor the right shift key) orDEFAULT_LOCATION
-
setPressedModifiers
public void setPressedModifiers(boolean shift, boolean ctrl, boolean alt, boolean meta) Sets the state of the modifier keys that are already pressed when this keyboard is typed (e.g. pressed by a previous keyboard); no events are fired for these keys.- Parameters:
shift- whether the shift key is pressedctrl- whether the control key is pressedalt- whether the alt key is pressedmeta- whether the meta key is pressed
-
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
-