16

I'm writing a program in C# that runs in the background and allows users to use a hotkey to switch keyboard layouts in the active window. (Windows only supports CTRL+SHIFT and ALT+SHIFT)

I'm using RegisterHotKey to catch the hotkey, and it's working fine.

The problem is that I can't find any API to change the keyboard layout for the focused window.

ActivateKeyboardLayout and LoadKeyboardLayout can only change the keyboard layout for the calling thread.

Does anyone know how to change the keyboard layout for a different thread (the way the Language Bar does)?

Laurel
  • 5,522
  • 11
  • 26
  • 49
SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
  • When you switch languages using a windows-builtin hotkey, it doesn't affect different threads, does it? – JXG Nov 05 '08 at 15:12
  • per thread keyboard layout switching? As opposed to system-wide layout switching? Doing something with chording? – jcolebrand Nov 01 '10 at 17:13
  • @drachenstern: There's no such thing as system-wide keyboard layout switching; the active keyboard layout is a per-UI-thread state. I'm trying to replace Alt+Shift so that I don't press it by accident, so chording is not an option (if I understand what you meant). – SLaks Nov 01 '10 at 17:21
  • I imagine you knew what I meant on the chording. I just figured the keyboard layout was something that inherited from system and was curious why you would want to override it per thread. Why would you not use the same layout on the thread as on the system? That's what I was curious about. Just looking to learn more ways of seeing the world, I suppose. – jcolebrand Nov 01 '10 at 19:53

4 Answers4

8
PostMessage(handle, 
    WM_INPUTLANGCHANGEREQUEST, 
    0, 
    LoadKeyboardLayout( StrCopy(Layout,'00000419'), KLF_ACTIVATE)
);
default locale
  • 11,849
  • 13
  • 52
  • 59
Ramil Mammadov
  • 306
  • 4
  • 10
  • 1
    This works perfectly using `GetForegroundWindow` for the `handle`. One exception is WPF programs, which seem to lock up in response to this message. – Roman Starkov Nov 01 '15 at 14:42
  • @RomanStarkov How to find which version of WPF affected and what is the reason? – Dims Apr 11 '19 at 22:01
  • @Dims I don't know either of those. I wonder if the reason is somehow connected to [this](https://stackoverflow.com/questions/10176150/very-simple-wpf-program-locks-up-hangs-on-keyboard-layout-change) (but it won't help you solve the problem) – Roman Starkov Apr 12 '19 at 13:53
2

I think the trick is to get your code to execute in the context of the thread whose keyboard layout you wish to change. You'll need to do some win32 interop here and learn about DLL Injection to get your code to execute in the remote thread.

A keyboard hook handler looks like a good option for you here.

Take a look at http://www.codeproject.com/KB/threads/winspy.aspx

Sijin
  • 4,450
  • 19
  • 22
1

Another way that may be acceptable if you are writing something just for yourself: define a separate key combination for every layout (such as Alt+Shift+1, etc), and use SendInput to switch between them.

The circumstances in which this is usable are limited of course.

Roman Starkov
  • 52,420
  • 33
  • 225
  • 300
0
  function ChangeRemoteWndKeyboardLayoutToRussian(
    const RemoteHandle: THandle): Boolean;
  var
    Dumme: DWORD;
    Layout: HKL;
  begin
    Layout := LoadKeyboardLayout('00000419', KLF_ACTIVATE);
    Result := SendMessageTimeOut(RemoteHandle, WM_INPUTLANGCHANGEREQUEST,
      0, Layout, SMTO_ABORTIFHUNG, 200, Dumme) <> 0;
    if Result then    
      Result := SendMessageTimeOut(RemoteHandle, WM_INPUTLANGCHANGEREQUEST,
        RUSSIAN_CHARSET, Layout, SMTO_ABORTIFHUNG, 200, Dumme) <> 0;
  end;
SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
Ramil Mammadov
  • 306
  • 4
  • 10