GetKeyboardLayout() doesn't work properly in some cases

走远了吗. 提交于 2019-12-19 20:01:22

问题


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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/29217501/getkeyboardlayout-doesnt-work-properly-in-some-cases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!