5

I'm writting console app that should indicate keyboard layout. I'm using GetForegroundWindow() function to get current active window, function GetWindowThreadProcessId() to get thread id, and finally, with function GetKeyboardLayout() I get keyboard layout. Everyting works fine with any application I try except some cases:

  1. When I switch window to cmd.exe or any other console app it shows default layout, changing layout have no effect
  2. In games is the same situation
  3. All threads of the same process have the same layout [tested on explorer.exe (this confused me as I think layout is thread specified)

Please, explain me what is going on. Below is my test code

#include <Windows.h>
#include <iostream>

int main()
{
    while(8)
    {
        HWND  _curr_window = GetForegroundWindow();
        DWORD _curr_window_procces_id;
        DWORD _curr_window_thread_id = GetWindowThreadProcessId(_curr_window, &_curr_window_procces_id);
        std::cout << "Process ID is " << _curr_window_procces_id << " and Thread ID is " << _curr_window_thread_id << std::endl;
        HKL _key_locale = GetKeyboardLayout(_curr_window_thread_id);
        std::cout << "Keyboard layout is " << _key_locale << std::endl;
        Sleep(1000);
    }
    return 0;
}
Binary Mind
  • 309
  • 3
  • 13
  • 2
    `GetKeyboardLayout` works with applications running in the WINDOWS subsystem (it's a user32.dll export). Command line applications run in the CONSOLE subsystem. – IInspectable Mar 23 '15 at 18:22
  • Keyboard layout can be changed for either a thread or the whole process. – Jonathan Potter Mar 23 '15 at 19:35
  • There is an issue with Microsoft console which is still open https://github.com/Microsoft/console/issues/83 – Mayur Oct 05 '18 at 16:33

1 Answers1

0

Your code is right, It's Microsoft's problem. I run this code in a third-party shell, babun and get the right result. You can try this shell, I think it's the best shell on Windows.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
DaiPei
  • 31
  • 2