Simulate keystrokes
Examples
Browser Keys
sendKeys(Keys.chord(Keys.LEFT_ALT, 'n'))
// By TAB > ARROW_DOWN > ARROW_DOWN > ENTER pull up 'Save As' and click context menu item
pressTab()
sendKeys(Keys.ARROW_DOWN)
sendKeys(Keys.ARROW_DOWN)
pressEnter()
Desktop keys
sendKeys('vim the-new-file.txt{ENTER}')
windowContent = copyPuttyWindowText()
sendKeys('aaaaaaa')
sendKeys('{TAB}{ENTER 2}')
sendKeys('{UP}{UP}{DOWN}{DOWN}')
sendKeys('i');
sendKeys('{UP}{DOWN}{RIGHT}{RIGHT}{TAB}{TAB}')
sendKeys('Hello, Robot{!}')
sendKeys('{CTRLDOWN}c{CTRLUP}')
sendKeys(':q{!}')
sendKeys('{ENTER}')
sendKeys('{CTRLDOWN}d{CTRLUP}')
Selenium keys enum
tip
For details, see the official guide. The constants can also be used for sending keys into desktop apps.
Expand to see org.openqa.selenium.Keys enum constants
- ADD
- ALT
- ARROW_DOWN
- ARROW_LEFT
- ARROW_RIGHT
- ARROW_UP
- BACK_SPACE
- CANCEL
- CLEAR
- COMMAND
- CONTROL
- DECIMAL
- DELETE
- DIVIDE
- DOWN
- END
- ENTER
- EQUALS
- ESCAPE
- F1
- F10
- F12
- F2
- F3
- F4
- F5
- F6
- F7
- F8
- F9
- HELP
- HOME
- INSERT
- LEFT
- LEFT_ALT
- LEFT_CONTROL
- LEFT_SHIFT
- META
- MULTIPLY
- NULL
- NUMPAD0
- NUMPAD1
- NUMPAD2
- NUMPAD3
- NUMPAD4
- NUMPAD5
- NUMPAD6
- NUMPAD7
- NUMPAD8
- NUMPAD9
- PAGE_DOWN
- PAGE_UP
- PAUSE
- RETURN
- RIGHT
- SEMICOLON
- SEPARATOR
- SHIFT
- SPACE
- SUBTRACT
- TAB
- UP
- ZENKAKU_HANKAKU
Simulating multiple keys press
sendKeys(Keys.chord(Keys.LEFT_ALT, 'n'))
sendKeys(Keys.chord(Keys.LEFT_CONTROL, 'c'))
sendKeys(Keys.chord(Keys.LEFT_CONTROL, 'v'))
Technical details
Use org.openqa.selenium.chord:
public static java.lang.String chord(java.lang.CharSequence... value). Simulate pressing many keys at once in a "chord". Takes a sequence of Keys.XXXX or strings; appends each of the values to a string, and adds the chord termination key (Keys.NULL) and returns the resultant string.
When the low-level webdriver key handlers see Keys.NULL, active modifier keys (CTRL/ALT/SHIFT/etc) release via a keyup event. Parameters:value - characters to sendReturns:String representation of the char sequence.
public static java.lang.String chord(java.lang.Iterable<java.lang.CharSequence> value). Parameters:value - characters to sendReturns:String representation of the char sequenceSee Also:chord(CharSequence...)
AutoIt keys guide for desktop automation
tip
For details, see the official guide.
! – This tells AutoIt to send an ALT keystroke, therefore sendKeys("This is text!a") would send the keys "This is text" and then press "ALT+a".
note
Some programs are very choosy about capital letters and ALT keys, i.e., "!A" is different from "!a". The first says ALT+SHIFT+A, the second is ALT+a. If in doubt, use lowercase.
+ – This tells AutoIt to send a SHIFT keystroke; therefore, sendKeys("Hell+o") would send the text "HellO". SendKeys("!+a") would send "ALT+SHIFT+a".
^ – This tells AutoIt to send a CONTROL keystroke; therefore, sendKeys("^!a") would send "CTRL+ALT+a".
note
Some programs are very choosy about capital letters and CTRL keys, i.e., "^A" is different from "^a". The first says CTRL+SHIFT+A, the second is CTRL+a. If in doubt, use lowercase.
# – The hash now sends a Windows keystroke; therefore, sendKeys("#r") would send Win+r which launches the Run() dialog box.
If a user is holding down the Shift key when a SendKeys function begins, text may be sent in uppercase. One workaround is to sendKeys("{SHIFTDOWN}{SHIFTUP}") before the other sendKeys operations.
Certain keyboards as the Czech keyboard sends different characters when using the Shift Key or with CAPS LOCK enabled while sending a character. Due to the send AutoIt implementation the CAPS LOCKed character will be sent as a Shifted character so it will not work.
| Command (if zero flag) | resulting Keypress |
|---|---|
| {!} | ! |
| {#} | # |
| {+} | + |
| {^} | ^ |
| {{} | { |
| {}} | } |
| {SPACE} | SPACE |
| {ENTER} | ENTER key on the main keyboard |
| {ALT} | ALT |
| {BACKSPACE} or {BS} | BACKSPACE |
| {DELETE} or {DEL} | DELETE |
| {UP} | Up arrow |
| {DOWN} | Down arrow |
| {LEFT} | Left arrow |
| {RIGHT} | Right arrow |
| {HOME} | HOME |
| {END} | END |
| {ESCAPE} or {ESC} | ESCAPE |
| {INSERT} or {INS} | INS |
| {PGUP} | PageUp |
| {PGDN} | PageDown |
| {F1} - {F12} | Function keys |
| {TAB} | TAB |
| {PRINTSCREEN} | Print Screen key |
| {LWIN} | Left Windows key |
| {RWIN} | Right Windows key |
| {NUMLOCK on} | NUMLOCK (on/off/toggle) |
| {CAPSLOCK off} | CAPSLOCK (on/off/toggle) |
| {SCROLLLOCK toggle} | SCROLLLOCK (on/off/toggle) |
| {BREAK} | for Ctrl+Break processing |
| {PAUSE} | PAUSE |
| {NUMPAD0} - {NUMPAD9} | Numpad digits |
| {NUMPADMULT} | Numpad Multiply |
| {NUMPADADD} | Numpad Add |
| {NUMPADSUB} | Numpad Subtract |
| {NUMPADDIV} | Numpad Divide |
| {NUMPADDOT} | Numpad period |
| {NUMPADENTER} | Enter key on the numpad |
| {APPSKEY} | Windows App key |
| {LALT} | Left ALT key |
| {RALT} | Right ALT key |
| {LCTRL} | Left CTRL key |
| {RCTRL} | Right CTRL key |
| {LSHIFT} | Left Shift key |
| {RSHIFT} | Right Shift key |
| {SLEEP} | Computer SLEEP key |
| {ALTDOWN} | Holds the ALT key down until {ALTUP} is sent |
| {SHIFTDOWN} | Holds the SHIFT key down until {SHIFTUP} is sent |
| {CTRLDOWN} | Holds the CTRL key down until {CTRLUP} is sent |
| {LWINDOWN} | Holds the left Windows key down until {LWINUP} is sent |
| {RWINDOWN} | Holds the right Windows key down until {RWINUP} is sent |
| {ASC nnnn} | Send the ALT+nnnn key combination |
| {BROWSER_BACK} | Select the browser "back" button |
| {BROWSER_FORWARD} | Select the browser "forward" button |
| {BROWSER_REFRESH} | Select the browser "refresh" button |
| {BROWSER_STOP} | Select the browser "stop" button |
| {BROWSER_SEARCH} | Select the browser "search" button |
| {BROWSER_FAVORITES} | Select the browser "favorites" button |
| {BROWSER_HOME} | Launch the browser and go to the home page |
| {VOLUME_MUTE} | Mute the volume |
| {VOLUME_DOWN} | Reduce the volume |
| {VOLUME_UP} | Increase the volume |
| {MEDIA_NEXT} | Select next track in media player |
| {MEDIA_PREV} | Select previous track in media player |
| {MEDIA_STOP} | Stop media player |
| {MEDIA_PLAY_PAUSE} | Play/pause media player |
| {LAUNCH_MAIL} | Launch the email application |
| {LAUNCH_MEDIA} | Launch media player |
| {LAUNCH_APP1} | Launch user app1 |
| {LAUNCH_APP2} | Launch user app2 |
| {OEM_102} | Either the angle bracket key or the backslash key on the RT 102-key keyboard |
To send the ASCII value A (same as pressing ALT+065 on the numeric keypad):
sendKeys("{ASC 065}")
// When using 2 digit ASCII codes you must use a leading 0, otherwise an obsolete 437 code page is used.
To send UNICODE characters, enter the character code (decimal or hex), for example, this sends a Chinese character.
sendKeys("{ASC 2709}")
sendKeys("{ASC 0xA95}")
Single keys can also be sent repeated:
sendKeys("{DEL 4}") // Presses the DEL key 4 times
sendKeys("{S 30}") // Sends 30 'S' characters
sendKeys("+{TAB 4}") // Presses SHIFT+TAB 4 times
To hold a key down:
sendKeys("{a down}") // Holds the A key down
sendKeys("{a up}") // Releases the A key
To set the state of the capslock, numlock and scrolllock keys:
sendKeys("{NumLock on}") // Turns the NumLock key on
sendKeys("{CapsLock off}") // Turns the CapsLock key off
sendKeys("{ScrollLock toggle}") // Toggles the state of ScrollLock
Most laptop computer keyboards have a special Fn key. This key cannot be simulated.
note
By setting the flag parameter to 1, the above "special" processing will be disabled. This is useful when you want to send some text copied from a variable and you want the text sent exactly as written.
For example, open Folder Options (in the control panel) and try the following:
| Command | Description |
|---|---|
| sendKeys("{TAB}") | Navigate to next control (button, checkbox, etc) |
| sendKeys("+{TAB}") | Navigate to previous control. |
| sendKeys("^{TAB}") | Navigate to next WindowTab (on a Tabbed dialog window) |
| sendKeys("^+{TAB}") | Navigate to previous WindowTab. |
| sendKeys("{SPACE}") | Can be used to toggle a checkbox or click a button. |
| sendKeys("{+}") | Usually checks a checkbox (if it's a "real" checkbox.) |
| sendKeys("{-}") | Usually unchecks a checkbox. |
| sendKeys("{NumPadMult}") | Recursively expands folders in a SysTreeView32. |