I’m learning Win32 API. I’ve created an empty window and added an Edit box in it to type some text. I can type a text in it, but can’t switch the keyboard language.
Shortcuts like Alt+Shift don’t work. If I click the language icon in the tray, it will pause for a few seconds, then it will open the list finally, but my window will become inactive, so the change won’t affect it.
I can make the language change happen if I do this:
- press Alt+Shift (or whatever the combination is)
- press Alt, Space
Step 2 will result, as usual, in the popping of the window menu at the left top, but simultaneously the step 1 will become actual too.
I guess, I’m not processing some kind of message, or creating the window wrong, but I can’t find what is it that is wrong. Here is the full code:
#include <windows.h>
// handle of the edit box
HWND testEditBox;
// shows a message
void showMessage( LPSTR message ){
MessageBox( GetActiveWindow(), message, "", MB_OK );
}
// a simple window procedure
LRESULT CALLBACK windowProc( HWND window, UINT message, WPARAM wparam, LPARAM lparam )
{
switch( message )
{
case WM_CLOSE:
DestroyWindow( window );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( window, message, wparam, lparam );
}
return 0;
}
int WINAPI WinMain( HINSTANCE instance, HINSTANCE prevInstance, LPSTR commandLine, int showMode )
{
// create window class
WNDCLASS windowClass;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.style = 0;
windowClass.hInstance = instance;
windowClass.hCursor = LoadCursor( NULL, IDC_ARROW );
windowClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
windowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
windowClass.lpszClassName = "testWindow";
windowClass.lpfnWndProc = windowProc;
if( !RegisterClass( &windowClass ) )
{
showMessage( "Some trouble, sir" );
return GetLastError();
}
HWND window = CreateWindow
(
"testWindow",
"Test Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
400, 300,
NULL, // no parent
NULL, // no menu
instance,
NULL // no 'window-creation data'
);
if( !window ){
showMessage( "Some trouble, sir" );
return GetLastError();
}
ShowWindow( window, showMode );
// add an EDIT box to our window
testEditBox = CreateWindow
(
"EDIT",
"input here",
WS_CHILD,
10, 10,
200, 20,
window,
NULL,
instance,
NULL
);
ShowWindow( testEditBox, SW_SHOW );
// start message loop
MSG message;
while( GetMessage( &message, window, 0, 0 ) > 0 )
{
TranslateMessage( &message );
DispatchMessage( &message );
}
return message.wParam;
}
The problem lays in the line
GetMessage( &message, window, 0, 0 ). You are receiving messages only for thewindow, nottestEditBox, so some messages aren’t dispatched. The line should be likeGetMessage(&message, NULL, 0, 0).These are the messages received by
testEditBoxin your application when I press and release Ctrl + Shift:Compare them to the messages after the fix: