Description
Use the KeyDown
method to simulate pressing of a key specified by its virtual-key code. The method performs the same action as the KeyDown event of a low-level procedure.
Declaration
LLPlayer.KeyDown(VKCode, Delay)
VKCode | [in] | Required | Integer | |
Delay | [in] | Required | Integer | |
Result | None |
Applies To
The method is applied to the following object:
Parameters
The method has the following parameters:
VKCode
The code of the key to press. For a list of key codes, see the Virtual-Key Codes article in the MSDN library.
If the Win32API plugin is installed and enabled in TestComplete, you can use the named constants VK_xxxx
instead of the hard-coded key code values. The available VK_xxxx
constants are listed under the Win32API
node in the Code Completion window.
Delay
The number of milliseconds passed after you command TestComplete to simulate the pressing of a key and before TestComplete starts to simulate the pressing. The delay gives you the possibility to better emulate real-life conditions. Also, it is necessary in order for the key pressing to be properly processed by the operating system.
If Delay is negative, it is set to the Auto-wait timeout.
If Delay is greater than 1000, the waiting progress will be shown in the TestComplete indicator during the test run.
Result Value
None.
Remarks
-
To simulate a complete keystroke, you need to execute both the
KeyDown
andKeyUp
methods. -
Since
KeyDown
simulates a pressing event, you can use it to emulate “hold and click” actions in your tests, that is, holding down a keyboard key while pressing a mouse button.
Example
The code below launches NOTEPAD.EXE and enters the "0A" string in Notepad's main window using the KeyDown
and KeyUp
methods.
To execute this script successfully, you should add NOTEPAD.EXE to your project's collection of tested applications. For detailed information, see Adding Tested Applications.
JavaScript, JScript
function LLPlayerExample()
{
// Launches Notepad
WshShell.Run("notepad.exe", SW_NORMAL);
// Enters "0" in Notepad's main window
LLPlayer.KeyDown(VK_NUMPAD0, 2000);
LLPlayer.KeyUp(VK_NUMPAD0, 2000);
// Enters "A" in Notepad's main window
LLPlayer.KeyDown(VK_SHIFT, 2000); // Presses Shift
LLPlayer.KeyDown(97, 2000); // Presses "a"
LLPlayer.KeyUp(97, 2000); // Releases "a"
LLPlayer.KeyUp(VK_SHIFT, 2000); // Releases Shift
}
Python
def LLPlayerExample():
# Launches Notepad
WshShell.Run("notepad.exe", SW_NORMAL)
# Enters "0" in Notepad's main window
LLPlayer.KeyDown(VK_NUMPAD0, 2000)
LLPlayer.KeyUp(VK_NUMPAD0, 2000)
# Enters "A" in Notepad's main window
LLPlayer.KeyDown(VK_SHIFT, 2000) # Presses Shift
LLPlayer.KeyDown(97, 2000) # Presses "a"
LLPlayer.KeyUp(97, 2000) # Releases "a"
LLPlayer.KeyUp(VK_SHIFT, 2000) # Releases Shift
VBScript
Sub LLPlayerExample()
' Launches Notepad
Call WshShell.Run("notepad.exe", SW_NORMAL)
' Enters "0" in Notepad's main window
Call LLPlayer.KeyDown(VK_NUMPAD0, 2000)
Call LLPlayer.KeyUp(VK_NUMPAD0, 2000)
' Enters "A" in Notepad's main window
Call LLPlayer.KeyDown(VK_SHIFT, 2000) ' Presses Shift
Call LLPlayer.KeyDown(97, 2000) ' Presses "a"
Call LLPlayer.KeyUp(97, 2000) ' Releases "a"
Call LLPlayer.KeyUp(VK_SHIFT, 2000) ' Releases Shift
End Sub
DelphiScript
function LLPlayerExample;
begin
// Launches Notepad
WshShell.Run('notepad.exe', SW_NORMAL);
// Enters "0" in Notepad's main window
LLPlayer.KeyDown(VK_NUMPAD0, 2000);
LLPlayer.KeyUp(VK_NUMPAD0, 2000);
// Enters "A" in Notepad's main window
LLPlayer.KeyDown(VK_SHIFT, 2000); // Presses Shift
LLPlayer.KeyDown(97, 2000); // Presses "a"
LLPlayer.KeyUp(97, 2000); // Releases "a"
LLPlayer.KeyUp(VK_SHIFT, 2000); // Releases Shift
end;
C++Script, C#Script
function LLPlayerExample()
{
// Launches Notepad
WshShell["Run"]("notepad.exe", SW_NORMAL);
// Enters "0" in Notepad's main window
LLPlayer["KeyDown"](VK_NUMPAD0, 2000);
LLPlayer["KeyUp"](VK_NUMPAD0, 2000);
// Enters "A" in Notepad's main window
LLPlayer["KeyDown"](VK_SHIFT, 2000); // Presses Shift
LLPlayer["KeyDown"](97, 2000); // Presses "a"
LLPlayer["KeyUp"](97, 2000); // Releases "a"
LLPlayer["KeyUp"](VK_SHIFT, 2000); // Releases Shift
}
See Also
Testing Applications in Low-Level Mode
KeyUp Method
Low-Level Procedure Events
KeyDown Method (Desktop Objects)
Keys Action
Keys Method
Simulating Keystrokes