无法获取当前的键盘布局 [英] can't get current keyboard layout
问题描述
我尝试使用 GetKeyboardLayoutName()和 GetKeyboardLayout()来获取当前的键盘布局,但是它们都为我提供了默认布局,并且更改布局不会影响输出!
I have tried GetKeyboardLayoutName() and GetKeyboardLayout() for getting the current keyboard layout, but they both give me the default layout and changing the layout doesn't affect the output!
while(1)
{
Sleep(5);
for(int i = 8; i < 191; i++)
{
if(GetAsyncKeyState(i)&1 ==1)
{
TCHAR szKeyboard[KL_NAMELENGTH];
GetKeyboardLayoutName(szKeyboard);
if(GetAsyncKeyState(i)&1 ==1)
{
TCHAR szKeyboard[KL_NAMELENGTH];
GetKeyboardLayoutName(szKeyboard);
cout << szKeyboard << endl ;
}
}
}
}
当默认布局设置为英语时,它总是给我"00000409",而当我将布局更改为波斯语时,我希望它是"00000429".
It always gives me "00000409" when the default layout is set to English, while I expect it to be "00000429" when I change the layout to Farsi.
我的第一个问题是,我过去经常通过搜索找到所有答案.但是现在,经过数小时的搜索却一无所获之后,我现在开始发疯了.
My first question here, I used to find all my answers by just searching. But right now I'm driving crazy after hours of searching around and getting nothing...
推荐答案
您需要注意的一件事是:: GetKeyboardLayout(..)将传递的线程标识符的lang作为参数.
one thing that you need to notice is that ::GetKeyboardLayout (..) gets the lang for the passed thread identifer as a param.
每个输入线程可以具有不同的输入语言环境lang. 例如,如果您将IE放在前台,然后按Alt + Shift,则将lang更改为UK. (您可以在任务栏中看到它)
each input thread can have different input locale lang. for instance if you put lets IE in the foreground and press Alt+Shift the lang changes to UK. ( you can see it in the taskbar )
现在,如果您将Alt + Tab转到另一个窗口(该窗口将处于foregorund中),您将看到lang不必留在英国.
now if you will Alt+Tab to another window ( which will be in foregorund ) you will see that lang dont have to stay UK.
所以您需要检查的是您要传递的线程ID.
so what you need to check is what is the thread id you are passing.
看看下面的代码,它将为您提供当前活动窗口的语言:
look at this code it will get you the lang for the current active window:
GUITHREADINFO Gti;
::ZeroMemory ( &Gti,sizeof(GUITHREADINFO));
Gti.cbSize = sizeof( GUITHREADINFO );
::GetGUIThreadInfo(0,&Gti);
DWORD dwThread = ::GetWindowThreadProcessId(Gti.hwndActive,0);
HKL lang = ::GetKeyboardLayout(dwThread);
要使用GUITHREADINFO,您需要定义WINVER 0x500. 在所有包含之前将其放在stdafx.h中.
to use GUITHREADINFO you need to define WINVER 0x500. put this in the stdafx.h before all the include.
#ifdef WINVER
#undef WINVER
#endif
#define WINVER 0x500
来源: GetKeyboardLayout没有返回正确的语言ID(WINXP)
这篇关于无法获取当前的键盘布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!