简体   繁体   English

使用 c++ 和 windows ZDB974238714CA8DE634A7CE1D083A14 更改 windows 上的输入法

[英]Change input method on windows using c++ and windows API

I want to write some code in C++ in combination with windows API in order to change the input method on windows (Keyboard input language). I want to write some code in C++ in combination with windows API in order to change the input method on windows (Keyboard input language).

Here is my first try:这是我的第一次尝试:

#include <iostream>
#include <windows.h>

int main() {
    int arr_size = GetKeyboardLayoutList(0, NULL);

    HKL hkl_array[arr_size];  // The array to hold input locale identifies found in the system(HKL)

    // Initialize the array to all zeros
    for (int i = 0; i <= arr_size - 1; i++) {
        hkl_array[i] = nullptr;
    }

    int lang_found = GetKeyboardLayoutList(arr_size, hkl_array);  // Get all the HKLs in the array

    for (int i = 0; i <= lang_found - 1; ++i) {
        // Print all the HKLs found
        std::cout << i + 1 << ". " << hkl_array[i] << std::endl;
    }

    std::cout << "Function return: " << ActivateKeyboardLayout(hkl_array[0], KLF_SETFORPROCESS);

    return 0;
}

When running the above code with English as the current language I get:当以英语作为当前语言运行上述代码时,我得到:

1. 0x4080408
2. 0x4090409

Function return: 0x4090409

(0x4080408 is Greek and 0x4090409 is English)

According to the documentation for ActivateKeyboardLayout , the return value is of type HKL and If the function succeeds, the return value is the previous input locale identifier.根据ActivateKeyboardLayout文档,返回值为 HKL 类型,如果 function 成功,则返回值为之前输入的区域设置标识符。 Otherwise, it is zero.否则,它为零。 In my case, the function clearly runs with no errors since it returns the input locale identifier for English witch was the language before the hypothetical change.就我而言,function 显然运行没有错误,因为它返回的输入语言环境标识符是英语女巫是假设更改之前的语言。 The language although does not change.语言虽然没有变化。 I get similar results if I start from greek and try to change to English.如果我从希腊语开始并尝试更改为英语,我会得到类似的结果。

I have tried different values in the flag parameter with no luck.我在标志参数中尝试了不同的值,但没有运气。 It is always the same thing.它总是一样的。 I have also tried using the LoadKeyboardLayoutA as follows:我还尝试使用LoadKeyboardLayoutA ,如下所示:

#include <iostream>
#include <windows.h>

int main(){
    LPCSTR language_select = "00000408";  // Code for greek
    std::cout << LoadKeyboardLayoutA(language_select, KLF_ACTIVATE);

    return 0;
}

When running the above code with English as the current language I get:当以英语作为当前语言运行上述代码时,我得到:

0x4080408

According to the documentation for LoadKeyboardLayoutA , If the function succeeds, the return type is HKL and the return value is the input locale identifier corresponding to the name specified in the function's parameter (language_select).根据LoadKeyboardLayoutA文档,如果 function 成功,则返回类型为 HKL,返回值是与函数参数 (language_select) 中指定的名称对应的输入语言环境标识符。 So this seems to run ok as well but no luck with language change.所以这似乎也运行良好,但语言改变没有运气。 I also tried with language_select = "00000809" (UK English) and the result I got was Uk English was just added to the list of languages as I did not have it installed.我还尝试了 language_select = "00000809" (英国英语),我得到的结果是英国英语刚刚添加到语言列表中,因为我没有安装它。

Finally, I tried calling the ActivateKeyboardLayout with LoadKeyboardLayoutA ad the HKL parameter as follows最后,我尝试使用LoadKeyboardLayoutA和 HKL 参数调用ActivateKeyboardLayout ,如下所示

#include <iostream>
#include <windows.h>

int main() {
    LPCSTR language_select = "00000408";  // Code for greek
    std::cout << ActivateKeyboardLayout(LoadKeyboardLayoutA(language_select, KLF_ACTIVATE), KLF_SETFORPROCESS);

    return 0;
}

When running the above code with English as the current language I get:当以英语作为当前语言运行上述代码时,我得到:

0x4080408

Which again seems normal but there is no chance in the language.这再次看起来很正常,但语言没有机会。 I am running this on Windows 10 20H2 OS Build 19042.685 and I am using CLion as an IDE (I don't think it matters but just in case anyone needs the info).我在 Windows 10 20H2 OS Build 19042.685 上运行它,我使用 CLion 作为 IDE(我认为这不重要,但以防万一有人需要信息)。 What am I doing wrong?我究竟做错了什么?

The very first sentence of the ActivateKeyboardLayout function documentation states: "Sets the input locale identifier (formerly called the keyboard layout handle) for the calling thread or the current process.". ActivateKeyboardLayout function 文档的第一句话指出:“为调用线程或当前进程设置输入区域设置标识符(以前称为键盘布局句柄)。”。 Since you never read input in your program, you don't observe its effect.由于您从未在程序中读取输入,因此您不会观察到它的效果。 It does not change the keyboard layout of the system.它不会改变系统的键盘布局。

On Windows, each thread has its own keyboard layout.在 Windows 上,每个线程都有自己的键盘布局。 To change the keyboard layout for a specific window, you need to send the WM_INPUTLANGCHANGEREQUEST message to it:要更改特定 window 的键盘布局,您需要向其发送WM_INPUTLANGCHANGEREQUEST消息:

SendMessageW(<windowHandle>, WM_INPUTLANGCHANGEREQUEST, 0, LoadKeyboardLayoutW(language_select, KLF_ACTIVATE));

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

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