Reputation: 73
I'm having a problem where the user is unable to change the keyboard layout whilst the application is in focus. It doesn't work when clicking the language preferences in the taskbar nor when using the shortcut Alt+Shift. I'm also not receiving any WM_INPUTLANGCHANGEREQUEST
or WM_INPUTLANGCHANGE
events, which I would have expected.
#include <Windows.h>
#include <stdio.h>
static int close_requested = 0;
static LRESULT CALLBACK wnd_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_CLOSE:
close_requested = 1;
break;
case WM_INPUTLANGCHANGE:
puts("WM_INPUTLANGCHANGE");
break;
case WM_INPUTLANGCHANGEREQUEST:
puts("WM_INPUTLANGCHANGEREQUEST");
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int main(void)
{
const HINSTANCE hInst = GetModuleHandle(NULL);
const WNDCLASS wc = {
.hInstance = hInst,
.hIcon = LoadIcon(NULL, IDI_APPLICATION),
.lpszClassName = TEXT("test-class"),
.lpfnWndProc = wnd_proc,
};
RegisterClass(&wc);
const HWND hWnd = CreateWindowEx(0, TEXT("test-class"), TEXT("Test"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 1920, 1080, NULL, NULL, hInst, NULL);
while (!close_requested) {
MSG msg;
while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
DestroyWindow(hWnd);
return 0;
}
I'm not sure what is causing my application to block users from changing their keyboard layout. I've been testing with an US International
and Microsoft IME
keyboard. I also cannot change language preferences if the keyboard would stay the same, for instance with a English (United States)
and a English (United Kingdom)
with the same keyboard layout (US International
).
Are there any Windows messages that I should be handling or do I need to set any additional flags when creating the window?
Upvotes: 0
Views: 331
Reputation: 3890
I think the message loop you are using is causing the problem. Modify the message loop to:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
It can work for me, I suggest you refer to Using Messages and Message Queues to clarify the difference between the two message loops.
Edit:
Also you can use :
while (!close_requested) {
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
The cause of the error is given by IInspectable: The dangers of filtering window messages
Upvotes: 1