PressKeys Method

Class

AnyWin class.

Action

Presses (but does not release) a set of keys or mouse buttons.

Syntax

window.PressKeys (sKeys [, nDelay, ensureFocus])
Variable Description
sKeys The keys to type or mouse buttons to press. To use mouse buttons, use <Button1>, <Button2>, or <Button3>. For the Classic Agent, more than one key is supported. For the Open Agent, only one key is supported. The first key is used and the remaining keys are ignored. For example:
MainWin.PressKeys("<Shift><Left>")
will only press the Shift key. To press both keys, specify:
MainWin.PressKeys("<Shift>")
MainWin.PressKeys("<Left >")
STRING.
nDelay Optional: The delay, in seconds, between events. The Open Agent always uses 0 for this value regardless of what is specified in the script. NUMBER.
ensureFocus

Optional: Whether the agent brings the application to the foreground and then sets the keyboard focus to the control on which the method is executed. This is executed before the first key is pressed, which means it is not rechecked for each keystroke when you specify multiple keys to be pressed.

This variable is supported only in scripts that use the Open Agent.

By default, this variable is set to TRUE. BOOLEAN.

Notes

PressKeys is a low-level function that most scripts do not have to call. In general, when interacting with an application, you use the TypeKeys function to enter text.

PressKeys is not supported for native mobile applications.

Valid keystrokes for all browsers except Internet Explorer include:
  • <Alt> (left-side Alt key)
  • <Ctrl> (Left-side Ctrl key)
  • <Shift> (Left-side Shift key)

For all other applications, including web applications on Internet Explorer, see Key names for a list of valid key names.

If you specify nDelay, the value specified there temporarily overrides the global delay set with the OPT_KEYBOARD_DELAY option or set in the Agent Options dialog box.

A key can be followed by a counter, which defines how often the keys should be typed. The counter can be combined with the delay.

During the delay time, the screen is locked and interaction with the mouse and keyboard is no longer possible.

PressKeys does not actually generate keyboard events when called. Instead, the events are queued until all keyboard and mouse events have been released.

You can call the AgentClass method FlushEvents if you want to do anything between pressing and releasing the keys. Explicitly flushing events with FlushEvents is necessary if you want the key presses to happen now.

Windows

PressKeys first gives the current window the input focus.

Examples

Find.PressKeys ("<Enter>")
Find.ReleaseKeys ("<Enter>")

The following code will not generate events until the call to ReleaseKeys:

MainWin.PressKeys ("<Shift>")
MainWin.TypeKeys ("<Left>", 5)
MainWin.ReleaseKeys ("<Shift>") // events released

Because the Open Agent always uses the value 0 for the delay argument, the following code will yield the same results if replayed against the Open Agent:

MyAnyWin::PressKeys(“<Left>”)
MyAnyWin::PressKeys(“<Left>”, 0)
MyAnyWin::PressKeys(“<Left>”, 5)