GetKeyboardLayout () does not work properly in some cases

I am writing a console application that should indicate a keyboard layout. I use the GetForegroundWindow() function to get the current active window, the GetWindowThreadProcessId() function to get the thread ID, and finally with the GetKeyboardLayout() function I get a keyboard layout. Everyting works great with any application that I try, except in some cases:

  • When I switch the window to cmd.exe or any other console application, it shows the default layout, changing the layout does not affect
  • In games the same situation.
  • All threads of the same process have the same layout [tested on explorer.exe (it confused me, since I think the layout is a thread)

Please explain to me what is happening. 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; } 
+3
visual-c ++ winapi keyboard-events
source share

No one has answered this question yet.

See similar questions:

7
Switch / change the Windows language, even if the application is not focused
3
WinAPI How to get the keyboard layout of a console application

or similar:

10
How do I get GetModuleFileName () if I only have a window handle (hWnd)?
8
The CoWaitForMultipleHandles API does not behave as documented
8
WM_QUIT only messages for the stream, not windows?
3
Why does GetWindowThreadProcessId return 0 when called from a service?
2
Capturing when a particular laggy application window finishes processing keyboard input
2
Can an application run another application code?
0
In some cases, FillRect does not work
0
GetWindowThreadProcessID () returns different results based on context
-one
Why does wparam change if I use the same message with the same pairs?
-3
ShellExecute command not working properly in win10

All Articles