Simulating Keypresses

Applies to TestComplete 15.73, last modified on March 04, 2025

The following code illustrates the use of simulated keypresses. TestProc first obtains the MS Word process. Then it brings up the active document window and inserts "Hello, world!" at the start of it. Note that the first keypress is Ctrl+Home. This shortcut moves the insertion point to the beginning of the document.

JavaScript, JScript

function TestProc()
{
  var p, w;
  p = Sys.Process("winword");
  w = p.Window("OpusApp", "*");
  if (w.Exists)
  {
    w.Window("_WwF", "", 0).Window("_WwB", "*", 0).Window("_WwG", "", 0).MSAAObject("client").Click();
    Sys.Keys("^[Home]Hello, wolr[Left][Left][Del][Right]ld!!");
  }
}

Python

def TestProc():
  p = Sys.Process("winword") 
  w = p.Window("OpusApp", "*") 
  if w.Exists:
    w.Window("_WwF", "", 0).Window("_WwB", "*", 0).Window("_WwG", "", 0).MSAAObject("client").Click()
    Sys.Keys("^[Home]Hello, wolr[Left][Left][Del][Right]ld!!")

VBScript

Sub TestProc
  Set p = Sys.Process("winword")
  Set w = p.Window("OpusApp", "*")
  If w.Exists Then
    w.Window("_WwF", "", 0).Window("_WwB", "*", 0).Window("_WwG", "", 0).MSAAObject("client").Click
    Sys.Keys "^[Home]Hello, wolr[Left][Left][Del][Right]ld!!"
  End If
End Sub

DelphiScript

procedure TestProc;
var
  p, w : OleVariant;
begin
  p := Sys.Process('winword');
  w := p.Window('OpusApp', '*');
  if w.Exists then
  begin
    w.Window('_WwF', '', 0).Window('_WwB', '*', 0).Window('_WwG', '', 0).MSAAObject('client').Click();
    Sys.Keys('^[Home]Hello, wolr[Left][Left][Del][Right]ld!!');
  end;
end;

C++Script, C#Script

function TestProc()
{
  var p, w;
  p = Sys["Process"]("winword");
  w = p["Window"]("OpusApp", "*");
  if (w["Exists"])
  {
    w["Window"]("_WwF", "", 0)["Window"]("_WwB", "*", 0)["Window"]("_WwG", "", 0)["MSAAObject"]("client")["Click"]();
    Sys["Keys"]("^[Home]Hello, wolr[Left][Left][Del][Right]ld!!");
  }
}

Highlight search results