aqEnvironment.SetKeyboardLayout Method

Applies to TestComplete 15.62, last modified on March 19, 2024

Description

Use the SetKeyboardLayout method to set the desired keyboard layout for any process running in the operating system.

Declaration

aqEnvironment.SetKeyboardLayout(ProcessId, Locale)

ProcessId [in]    Required    Integer    
Locale [in]    Required    String    
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

ProcessId

Specifies the identifier of the process for which you are going to set the keyboard layout. You can obtain the process id using the Id property of the Process object that corresponds to the desired process. If you use the process identifier shown in the Windows Task Manager, a call to SetKeyboardLayout will fail after you stop the process and run it again, since the next time you run the process, its identifier changes.

Locale

A string that contains the layout identifier of the specified window in a hexadecimal form. The full layout identifier string looks like “00000409” (US English), “00000407” (German), “0000040C” (French) and so on.

The last four characters of the string specify the language identifier, for example, 0409 (US English), 0407 (German), 040C (French). The first four characters of the string specify the device handler of the physical keyboard layout, for example, “00100409” (US English Dvorak layout). To learn more about layout identifiers, see the description of the LoadKeyboardLayout function in Win32 API documentation.

In order for the function to be able to change the layout successfully, the appropriate keyboard layout must be installed in the operating system. If the specified layout is not installed, the function will fail. To check if support for the desired language is installed, use the aqEnvironment.IsLanguageSupported function.

Result Value

If the keyboard layout has been changed successfully, the method returns True; else - False.

Remarks

This method sets a specified keyboard layout for all windows of the tested application that exist in the system at the moment of the call. If a new window appears, you may need to set the layout for it since there is no guarantee that it will have the keyboard layout you specified.

Example

The following code demonstrates how you can check whether the desired keyboard layout is set for a process. If it is not set, the routine sets the layout for the process automatically.

JavaScript, JScript

function KeyboardLayoutExample()
 {
   WshShell.Run("notepad.exe", SW_NORMAL);
   var wNotepad = Sys.Process("notepad");
   var EditForm = wNotepad.Window("Notepad", "Untitled - Notepad", 1).Window("Edit", "", 1);
   var USEnglish = "00000409";
   if (aqEnvironment.GetKeyboardLayout(EditForm.Handle) != USEnglish)
   {
    if (aqEnvironment.SetKeyboardLayout(wNotepad.Id, USEnglish) == false)
    {
      Log.Error("The specified language is not supported.");
    }
   }
   EditForm.Keys("Hello!");
 }

Python

def KeyboardLayoutExample():
  WshShell.Run("notepad.exe", SW_NORMAL)
  wNotepad = Sys.Process("notepad")
  EditForm = wNotepad.Window("Notepad", "Untitled - Notepad", 1).Window("Edit", "", 1)
  USEnglish = "00000409"
  if aqEnvironment.GetKeyboardLayout(EditForm.Handle) != USEnglish:
    if aqEnvironment.SetKeyboardLayout(wNotepad.Id, USEnglish) == False:
      Log.Error("The specified language is not supported.")
  EditForm.Keys("Hello!")

VBScript

Sub KeyboardLayoutExample
   Dim wNotepad, EditForm, USEnglish
   Call WshShell.Run("notepad.exe", SW_NORMAL)
   Set wNotepad = Sys.Process("notepad")
   Set EditForm = wNotepad.Window("Notepad", "Untitled - Notepad", 1).Window("Edit", "", 1)
   USEnglish = "00000409"
   If Not (aqEnvironment.GetKeyboardLayout(EditForm.Handle) = USEnglish) Then
    If aqEnvironment.SetKeyboardLayout(wNotepad.Id, USEnglish) = False Then
      Log.Error("The specified language is not supported.")
    End If
   End If 
   EditForm.Keys("Hello!")
End Sub

DelphiScript

function KeyboardLayoutExample;
   var wNotepad, EditForm, USEnglish;
begin
   WshShell.Run('notepad.exe', SW_NORMAL);
   wNotepad := Sys.Process('notepad');
   EditForm := wNotepad.Window('Notepad', 'Untitled - Notepad', 1).Window('Edit', '', 1);
   USEnglish := '00000409';
   if aqEnvironment.GetKeyboardLayout(EditForm.Handle) <> USEnglish then
   begin
    if aqEnvironment.SetKeyboardLayout(wNotepad.Id, USEnglish) = False then
    begin
      Log.Error('The specified language is not supported.')
    end;
   end;
   EditForm.Keys('Hello!');
end;

C++Script, C#Script

function KeyboardLayoutExample()
 {
   WshShell["Run"]("notepad.exe", SW_NORMAL);
   var wNotepad = Sys["Process"]("notepad");
   var EditForm = wNotepad.Window("Notepad", "Untitled - Notepad", 1).Window("Edit", "", 1);
   var USEnglish = "00000409";
   if (aqEnvironment["GetKeyboardLayout"](EditForm.Handle) != USEnglish)
   {
    if (aqEnvironment["SetKeyboardLayout"](wNotepad["Id"]) == false)
    {
      Log["Error"]("The specified language is not supported.");
    }
   }
  EditForm["Keys"]("Hello!")
  }

See Also

Keys Method
Keys Action
Simulating Keystrokes
GetKeyboardLayout Method
IsLanguageSupported Method

Highlight search results