I have successfully built and run an example Win Api application. Next I have added handling for WM_CHAR
and WM_SYSCHAR messages
:
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
case WM_CHAR:
case WM_SYSCHAR:
{
UINT32 codepoint = (UINT32)wParam;
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
The only purpose of the above code is to see during debugging a codepoint for the pressed key. According to my language settings I should get the codepoint 261 after pressing AltGr + a but I get the codepoint 185. I have also built and run an example application which uses GLFW
library and set a char callback using glfwSetCharCallback
fucntion. That callback is called with codepoint 261 after pressing AltGr + a. It looks like that my Win Api code has some lacks but after looking at GLFW source code I have no idea why I get a different codepoint in my application. It is not related to handling WM_KEYDOWN,WM_SYSKEYDOWN,WM_KEUP and WM_SYSKEYUP
messages - I have commented out that code in GLFW and the char callback works fine. Where is the problem ?
Using the Unicode version of the RegisterClass
function(RegisterClassW
) works.
The
WM_CHAR
message uses UTF-16 (16-bit Unicode Transformation Format) code units in itswParam
if the Unicode version of theRegisterClass
function was used to register the window class.
UTF-16 will complement code point correctly.