.SendKeys - VBScript


Overview

The .SendKeys command allows you to simulate keystrokes and input text into any active application from within VB Script. It’s particularly useful for automating tasks that require user input, such as filling out forms or navigating menus.

Syntax

.SendKeys <keystrokes>

<keystrokes>: Specifies the keystrokes you want to simulate. Use special keys enclosed in braces, e.g., {TAB}, {ENTER}, or {ALT}.

Options/Flags

| Option | Purpose | Default |
|—|—|—|
| {BREAK} | Sends a BREAK keystroke | N/A |
| {CAPSLOCK} | Toggles caps lock on or off | N/A |
| {CLEAR} | Clears the current line | N/A |
| {DEL} | Sends a DELETE keystroke | N/A |
| {DOWN} | Sends a DOWN ARROW keystroke | N/A |
| {END} | Moves the cursor to the end of the line | N/A |
| {ENTER} | Sends an ENTER keystroke | N/A |
| {ESC} | Sends an ESCAPE keystroke | N/A |
| {HELP} | Opens the help window | N/A |
| {HOME} | Moves the cursor to the beginning of the line | N/A |
| {INS} | Toggles insert mode on or off | N/A |
| {LEFT} | Sends a LEFT ARROW keystroke | N/A |
| {NUMLOCK} | Toggles num lock on or off | N/A |
| {PGDN} | Sends a PAGE DOWN keystroke | N/A |
| {PGUP} | Sends a PAGE UP keystroke | N/A |
| {PRINT} | Sends a PRINT SCREEN keystroke | N/A |
| {RIGHT} | Sends a RIGHT ARROW keystroke | N/A |
| {SCROLLLOCK} | Toggles scroll lock on or off | N/A |
| {TAB} | Sends a TAB keystroke | N/A |
| {UP} | Sends an UP ARROW keystroke | N/A |

Examples

  • Type “Hello World” into an input field:
.SendKeys "Hello World"
  • Navigate a menu with arrow keys and enter:
.SendKeys "{DOWN}{DOWN}{ENTER}"
  • Toggle caps lock and type a password:
.SendKeys "{CAPSLOCK}"
.SendKeys "myPassword"
.SendKeys "{CAPSLOCK}"

Common Issues

  • Lost focus: If the target application loses focus during .SendKeys, it may not receive the simulated keystrokes. Ensure the application has focus before using .SendKeys.
  • Unrecognized special keys: Some special keys may require specific syntax. For example, to simulate the Windows key, use {VK_LWIN} instead of {WIN}.

Integration

.SendKeys can be combined with other VB Script commands to automate complex tasks. For instance, you can use it with .GetObject to access specific elements on a web page and input data or click buttons.

  • .ClipboardData: Gets or sets the text in the clipboard.
  • .InStr: Searches for a substring within a string.
  • .Replace: Replaces a substring within a string.