在某些情况下,GetKeyboardLayout()无法正常工作 [英] GetKeyboardLayout() doesn't work properly in some cases
问题描述
我正在编写应指示键盘布局的控制台应用程序.我正在使用GetForegroundWindow()
函数获取当前的活动窗口,使用函数GetWindowThreadProcessId()
获取线程ID,最后使用函数GetKeyboardLayout()
获取键盘布局.在某些情况下,Everingting可以在我尝试的任何应用程序上正常工作
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:
- 当我将窗口切换到cmd.exe或任何其他控制台应用程序时,它显示默认布局,更改布局无效
- 在游戏中情况相同
- 同一进程的所有线程都具有相同的布局[在explorer.exe上进行了测试(这使我感到困惑,因为我认为布局是指定的线程)
请解释一下发生了什么.下面是我的测试代码
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;
}
推荐答案
您的代码正确,这是Microsoft的问题.我在第三方外壳程序 babun 中运行此代码,并获得正确的结果.您可以尝试使用此外壳程序,我认为它是Windows上最好的外壳程序.
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.
这篇关于在某些情况下,GetKeyboardLayout()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!