Press or Release Key

Virtually presses or releases a key on the keyboard.

Command availability: IBM RPA SaaS and IBM RPA on premises

Script syntax

IBM RPA's proprietary script language has a syntax similar to other programming languages. The script syntax defines the command's syntax in the script file. You can work with this syntax in IBM RPA Studio's Script mode.

keyboard --type(KeyboardEventType) [--key(KeysWrapper)]

Dependencies

If your bot is interacting with an application, make sure the application's window is in focus before you use the keyboard command. Use one of the following commands to put a window in focus:

Input parameters

The following table displays the list of input parameters available in this command. In the table, you can see the parameter name when working in IBM RPA Studio's Script mode and its Designer mode equivalent label.

Designer mode label Script mode name Required Accepted variable types Description
Keys key Optional KeysWrapper Key to be pressed or released virtually from the keyboard.
Command Type type Required KeyboardEventType Virtual action taken on the keyboard.

See the type parameter options

type parameter options

The following table displays the options available for the key input parameter. The table shows the options available when working in Script mode and the equivalent label in the Designer mode.

Designer mode label Script mode name Description
Press and hold key KeyPress Matches the action of pressing and holding a key.
Press key KeyDown Matches the action of pressing a key.
Release key KeyUp Matches the action of releasing a key.

Example

A file opens in Notepad, in which you write the text "IBM". After that, it uses the Press or Release Key command to hold down the "Ctrl" key, press and release the "S" key and release the "Ctrl" key from the keyboard virtually. Then, the awaits the save dialog box, using again the Press or Release Key command to press the "Escape" key.

defVar --name notepadWindow --type Window
defVar --name saveAsWindow --type Window
defVar --name saveAsItAppearedWindow --type Boolean
// Open Notepad, via relative directory path, or identify an active Notepad window and attach it to execution.
launchOrAttach --executablepath "notepad.exe" --useregex--regexPattern Notepad --regexOptions "0" --timeout 00:00:10 notepadWindow=value
// Put Notepad on top of other windows to allow typing.
focusWindow --window ${notepadWindow}
// Type "IBM" in Notepad.
typeText --text "IBM"
// Keyboard command sequence.
// Save the file "Ctrl + S".
keyboard --type "KeyPress" --key "ControlKey"
keyboard --type "KeyDown" --key "S"
keyboard --type "KeyUp" --key "ControlKey"
// Waiting for the dialog box is the best practice.
waitWindow --useregex--regexPattern "Save As" --regexOptions "0" --classname "#32770" --byparent--window ${notepadWindow} --timeout 00:00:10 saveAsWindow=value saveAsItAppearedWindow=success
// Evaluates whether the "Save As" dialog was found successfully. If not, an error message is displayed.
assert --message " Dialog Window \"Save As\" Not Found!" --left "${saveAsItAppearedWindow}" --operator "Is_True"
// Press "Escape" to cancel the "Save As" dialog window.
keyboard --type "KeyDown" --key "Escape"