0

I want to change the keyboard layout in Windows with Delphi. I successfully could do this with following code but it seems it only changes for one process/thread and not for every process.

// Array with 4 keyboard layout codes (in decimal)
const KLS: array [0 .. 3] of integer = (2055, 4108, 2064, 2057);
KLF_SETFORPROCESS = $00000100;

// The TForm1 contains a RadioGroup1 with 4 Radiobuttons 
procedure TForm1.btn_activateLayoutClick(Sender: TObject);

begin
  SetKeyboardLayout(RadioGroup1.ItemIndex);
end;

// set the new keyboard layout according to the ItemIndex of the RadioGroup1
procedure TForm1.SetKeyboardLayout(const klsIndex: integer);
var
  klId: array [0 .. 9] of char;
  keyboardCode: integer;
begin
  keyboardCode := KLS[klsIndex];

  try
     ActivateKeyboardLayout(keyboardCode, KLF_SETFORPROCESS or KLF_ACTIVATE, KLF_SETFORPROCESS)
  finally
     raise Exception.Create('Error while changing keyboard layout');
  end;
end;
end.

Does anyone know how I could change the keyboard layout for every process in Windows? Do I need to make some changes in Registry?

user1885888
  • 135
  • 5
  • 13
  • Are you using Windows 7 or prior? As far as I understood, the way windows handles keyboard layouts changed from Windows 7 to 8: Windows 7 and prior handled layouts **per process** which I personally found uncomfortable and painful. Windows 8+ has one keyboard layout for the entire user session. – Günther the Beautiful Feb 20 '15 at 08:32
  • @GünthertheBeautiful: You're right. Microsoft updated it in their [link]documentation(https://msdn.microsoft.com/en-us/library/windows/desktop/ms646305(v=vs.85).aspx). – user1885888 Feb 20 '15 at 08:38
  • 1
    @DavidHeffernan: We have to support French and German speaking users in our Remote system. With this system, an user can connect to Windows 7 hosts via RDP. RDP automatically changes the keyboard layout for a French speaking user but this fails when the Windows 7 RDP host is already logged in. It only works when you do a logoff on Windows 7 RDP host before. This is also a solution but it is not what we want because of the high loading time. Therefore, we need a workaraound to change the keyboard layout while an active RDP session. This is why I tried to change the layout with a Delphi tool. – user1885888 Feb 20 '15 at 09:03
  • 1
    Ok, that helps us understand – David Heffernan Feb 20 '15 at 09:08

2 Answers2

0

Based on your comments it seems that what you want is to prevent the RDP connection from changing your local keyboard layout settins.

Answer to this can be found here: https://superuser.com/questions/426356/how-can-i-stop-the-remote-computer-from-changing-my-keyboard-layout

Community
  • 1
  • 1
SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • Not exactly. We have a pool of Windows 7 hosts which users can connect to one of these hosts. An RDP connection could be very idly if it has to login first and load every program. This is why every host is already logged in and loaded. Default keyboard layout is German. A French speaking user wants to have a keyboard layout French. RDP automatically changes this but **only** when a RDP host is **not logged in**. Since ours are already logged in, we want to change it with Delphi or something else when a French speaking user is connected. Is there a possibility to change it system-wide? – user1885888 Feb 23 '15 at 08:34
0
//Winapi.windows
LoadKeyBoardLayout('0000040A',1); //Spanish keyboard
LoadKeyBoardLayout('00000409',1); //English (US) keyboard
//https://learn.microsoft.com/en-us/windows/iot-core/develop-your-
app/onscreenkeyboardlayouts  windows page for keyboards
{The keyboard has to be loaded already under "Region & Language" -->
English (United States) --> Options --> +Add a keyboard --> US QWERTY
and Spanish QWERTY}