简体   繁体   English

LoadKeyboardLayout不会更改键盘布局

[英]LoadKeyboardLayout doesn't change keyboard layout

I'm trying to change the keyboard layout to English, but the changes don't reflect in Windows. 我正在尝试将键盘布局更改为英语,但更改未反映在Windows中。

The layout is installed, I have checked with GetKeyboardLayoutList. 布局已安装,我已经使用GetKeyboardLayoutList进行了检查。

屏幕截图

Code: 码:

#include <windows.h>

int main()
{
    LoadKeyboardLayout("00000409", KLF_ACTIVATE);
    return 0;
}

GetKeyboardLayoutName shows that the language has changed, but I don't see that in Windows GetKeyboardLayoutName显示语言已更改,但在Windows中看不到

Test code: 测试代码:

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    TCHAR keyboard[KL_NAMELENGTH];
    GetKeyboardLayoutName(keyboard);
    cout << keyboard << endl;

    LoadKeyboardLayout("00000409", KLF_ACTIVATE);

    GetKeyboardLayoutName(keyboard);
    cout << keyboard << endl;

    return 0;
}

Outputs: 输出:

00000405
00000409
[Finished in 2.2s]

UPDATE: Tried runnin the .exe on different windows computer, same result 更新:尝试在不同的Windows计算机上运行.exe,结果相同

Working with keyboard layout is rather tricky and it is different for Console and GUI applications. 使用键盘布局非常棘手,对于控制台和GUI应用程序则有所不同。 And LoadKeyboardLayout just make the layout "available" for activation unfortunately. 而且, LoadKeyboardLayout是, LoadKeyboardLayout仅使布局“可用”以进行激活。

GUI 图形用户界面

  1. Application must have GetMessage , TranslateMessage , DispatchMessage cycle. 应用程序必须具有GetMessageTranslateMessageDispatchMessage周期。 (Sublime has GetMessage cycle) (崇高具有GetMessage周期)
  2. You may switch layout with two sequental calls 您可以通过两次顺序调用来切换布局

     DWORD dwNewKeybLayout = 0x00000409; // Layout must be already loaded! PostMessage(hWnd, WM_INPUTLANGCHANGEREQUEST, 0, (LPARAM)dwNewKeybLayout); PostMessage(hWnd, WM_INPUTLANGCHANGE, 0, (LPARAM)dwNewKeybLayout); 

Console 安慰

Your application with int main() is console application, it does not have GetMessage cycle. 具有int main()的应用程序是控制台应用程序,它没有GetMessage循环。 All messages are processed by conhost itself. 所有消息都由conhost本身处理。 That's why your GetKeyboardLayoutName will not return correct result. 这就是为什么您的GetKeyboardLayoutName将不会返回正确的结果。 Never! 决不! No way! 没门!

However, you still may change layout for all processes are running in this console window. 但是,您仍然可以更改在此控制台窗口中运行的所有进程的布局。 But take in mind, that conhost processes messages asynchronously and the actual layout (which you can't determine or check) may be changed after some lag. 但是请记住,托管服务器会异步处理消息,并且实际布局(您无法确定或检查)可能会在一段时间后更改。

    HWND hCon = GetConsoleWindow();
    DWORD dwNewKeybLayout = 0x00000409; // Layout must be already loaded!
    PostMessage(hCon, WM_INPUTLANGCHANGEREQUEST, 0, (LPARAM)dwNewKeybLayout);
    PostMessage(hCon, WM_INPUTLANGCHANGE, 0, (LPARAM)dwNewKeybLayout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM