How to handle input based on keyboard language ?

closed account (GbRGwA7f)
Hello, I have a Win32 Direct2D program which allows me write text on screen. I found a way to print characters based on selected Keyboard-Language but I get some extra characters now.

1
2
3
4
5
6
7
8
9
10
11
12
13
RESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
	switch (umsg)
	{
	case WM_CHAR:
	{

		wchar_t text2 = LOWORD(wparam); //GETS pressed characters

		MessageBox(hwnd, &text2, L"test", MB_OK);
	}

...


I AM ABLE TO READ chars like A,B,C,D,E, +,@ .. etc but I get some unreadable extra characters also. WHat is the reason of that and what should I do ?
Last edited on
I don't get your problem. Is the output wrong?

Before you use the virtual key you might want to convert it to an actual UNICODE character using MapVirtualKeyW(...) with MAPVK_VK_TO_CHAR, See:

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mapvirtualkeyw
closed account (GbRGwA7f)
I updated the issue I found to print characters but I have a new issue.
MessageBox(...) expects the string to be 0 temianted. You can do it like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
RESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
	switch (umsg)
	{
	case WM_CHAR:
	{

		wchar_t text2[] = { LOWORD(wparam), 0 }; //GETS pressed characters

		MessageBox(hwnd, text2, L"test", MB_OK); // Note: NO & in front of text2
	}

...
Or is the character itself wrong? Consider MapVirtualKeyW(...)
closed account (GbRGwA7f)
Thanks, that works now. In messagebox I display any character from any language. However, I have a new issue. I use Dİrect2D rendering to render text on the screen. When I try to render any character from different language like "йцукенгшщзф" that throws following error on image:

https://ibb.co/mTm44zG


I will be mad.
For non ascii letters you cannot use functions like isalpha(...) from <ctype>. Use the locale version:

http://www.cplusplus.com/reference/locale/isalpha/

If you want to use it set std::setlocale(LC_ALL, ""); somewhere near the beginning of the program.
closed account (GbRGwA7f)
WCHAR wc1[] = L"RU J LETTER: \u0436"; This fixed the issue on direct2D. Now the last problem is I get pressed keys on win32api:

1
2
3
4
5
6
7
8
9
10
LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
	switch (umsg)
	{
	case WM_CHAR: {
			m_Input->AnyCharPressed = true;
			wchar_t myChar[] = { LOWORD((unsigned int)wparam), 0 };

//Here I pass the pressed key to wchar_t variable which will be used during direct2d drawing.
...


I REMOVED:
if (isprint((unsigned int)wparam)) WHICH THROW ERROR
I INSTEAD ADDED iswprint:
if (iswprint((unsigned int)wparam))

The point is, I should handle keycode in "UNICODE" form, like L"\u0436" since only that will help to draw it on Direct2D api. I do not know how to do this. Any ideas ???
Last edited on
Topic archived. No new replies allowed.