KeyDown Method (Desktop Objects)

Applies to TestComplete 14.71, last modified on April 22, 2021

Description

Use this property to simulate pressing a single key specified by its virtual key code.

Declaration

TestObj.KeyDown(VK)

TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
VK [in]    Required    Integer    
Result None

Applies To

The method is applied to the following object:

View Mode

To view this method in the Object Browser panel and in other panels and dialogs, activate the Advanced view mode.

Parameters

The method has the following parameter:

VK

Specifies the virtual-key code of the key to be pressed. For a list of the available codes, see the Virtual-Key Codes article in the MSDN library. Note that if the Win32API plugin is installed in TestComplete, you can refer to key codes by their names without defining constants in your test.

The KeyDown method presses Ctrl, Alt, Shift or other system keys (if its virtual key code is specified), but it does not simulate pressing a key with the Ctrl, Alt, Shift or other system key pressed. To simulate pressing a key with a system key pressed, you can use either the Keys method or a combination of the KeyDown and KeyUp methods. See the example below.

Result Value

None.

Remarks

To release the key, use the KeyUp method.

To simulate several keypresses, use the Keys method.

TestComplete cannot send the KeyDownmethod to an application with higher elevated permissions than the ones of TestComplete. To avoid possible problems, run TestComplete as administrator. See the Restrictions section of the Keys Method topic.

Example

The example below demonstrates how to simulate Ctrl-A key pressing with the KeyDown and KeyUp methods.

JavaScript, JScript

Sys.Desktop.KeyDown(0x11); // Press Ctrl
Sys.Desktop.KeyDown(65);                  // Press A
Sys.Desktop.KeyUp(65);                    // Release A
Sys.Desktop.KeyUp(0x11);   // Release Ctrl

Python

Sys.Desktop.KeyDown(0x11); # Press Ctrl
Sys.Desktop.KeyDown(65);                  # Press A
Sys.Desktop.KeyUp(65);                    # Release A
Sys.Desktop.KeyUp(0x11);   # Release Ctrl

VBScript

Sys.Desktop.KeyDown(&H11) ' Press Ctrl
Sys.Desktop.KeyDown(65)                  ' Press A
Sys.Desktop.KeyUp(65)                    ' Release A
Sys.Desktop.KeyUp(&H11)   ' Release Ctrl

DelphiScript

Sys.Desktop.KeyDown($11); // Press Ctrl
Sys.Desktop.KeyDown(65);                  // Press A
Sys.Desktop.KeyUp(65);                    // Release A
Sys.Desktop.KeyUp($11);   // Release Ctrl

C++Script, C#Script

Sys["Desktop"]["KeyDown"](0x11); // Press Ctrl
Sys["Desktop"]["KeyDown"](65);                  // Press A
Sys["Desktop"]["KeyUp"](65);                    // Release A
Sys["Desktop"]["KeyUp"](0x11);   // Release Ctrl

See Also

KeyUp Method (Desktop Objects)
Keys Method

Highlight search results