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; }
visual-c ++ winapi keyboard-events
Binary mind
source share