Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winapi GetKeyboardLayout()不使用';在某些情况下不能正常工作_Winapi_Visual C++_Keyboard Events - Fatal编程技术网

Winapi GetKeyboardLayout()不使用';在某些情况下不能正常工作

Winapi GetKeyboardLayout()不使用';在某些情况下不能正常工作,winapi,visual-c++,keyboard-events,Winapi,Visual C++,Keyboard Events,我正在编写一个控制台应用程序,它应该指示键盘布局。我使用GetForegroundWindow()函数获取当前活动窗口,使用GetWindowThreadProcessId()函数获取线程id,最后使用GetKeyboardLayout()函数获取键盘布局。我尝试的任何应用程序都可以正常运行,但某些情况除外: 当我将窗口切换到cmd.exe或任何其他控制台应用程序时,它会显示默认布局,更改布局没有任何效果 在游戏中也是如此 同一进程的所有线程都具有相同的布局[在explorer.exe上测试(这

我正在编写一个控制台应用程序,它应该指示键盘布局。我使用
GetForegroundWindow()
函数获取当前活动窗口,使用
GetWindowThreadProcessId()
函数获取线程id,最后使用
GetKeyboardLayout()
函数获取键盘布局。我尝试的任何应用程序都可以正常运行,但某些情况除外:

  • 当我将窗口切换到cmd.exe或任何其他控制台应用程序时,它会显示默认布局,更改布局没有任何效果
  • 在游戏中也是如此
  • 同一进程的所有线程都具有相同的布局[在explorer.exe上测试(这让我感到困惑,因为我认为布局是线程指定的)
  • 请告诉我发生了什么。下面是我的测试代码

    #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;
    }
    
    #包括
    #包括
    int main()
    {
    而(8)
    {
    HWND_curr_window=GetForeGroundIndow();
    DWORD当前窗口进程id;
    DWORD当前窗口线程id=GetWindowThreadProcessId(\u当前窗口和当前窗口进程id);
    
    std::cout您的代码是正确的,这是Microsoft的问题。我在第三方shell中运行此代码,并获得正确的结果。您可以尝试此shell,我认为它是Windows上最好的shell。

    GetKeyboardLayout
    适用于Windows子系统中运行的应用程序(它是user32.dll导出)。命令行应用程序在控制台子系统中运行。可以更改线程或整个进程的键盘布局。Microsoft CONSOLE存在问题,该问题仍处于打开状态