solaris783
Topic Author
Posts: 5
Joined: 07 Oct 2019, 03:41

C++ Integration - forwarding keyboard inputs

07 Oct 2019, 05:39

I'm a beginner at this so bear with me. I was using one of the projects that come with the Noesis SDK: IntegrationGLUT. I added a keyboard handler through the existing glut.h mechanisms (WndProc, WM_KEYDOWN, WM_KEYUP) and called view->KeyDown and view->KeyUp as inputs came in. I added a TextBox but as I typed nothing showed up except space and backspace. I started calling view->Char in addition to KeyDown and then characters started showed up in the TextBox. The real problem is that if I hit Backspace on the TextBox, then the next alphanumeric key I type doesn't show up. After a backspace I had to call view->Char(inputChar) twice with whatever is typed next for it to show up. Here's my handler:
int lastCharSent = -1;
////////////////////////////////////////////////////////////////////////////////////////////////////
static void KeyFunc(int key, int state)
{
	Noesis::Key nKey = (Noesis::Key) WINAPI_KEYMAPPING[key]; // get the Noesis key code from the WinApi value (like 0x41 = Noesis::Key_A)
	if (state == GLUT_DOWN)
	{
		int ProcChar = TranslateKey(key); // turn the input key into a char (i.e. "a" instead of 0x41)
		if (ProcChar != 0) {
			_view->Char((uint32_t)ProcChar);
			if (lastCharSent == Key_Back) // HACK to overcome Char eating character sent after a backspace
				_view->Char((uint32_t)ProcChar);
		}
		_view->KeyDown(nKey);
		lastCharSent = (int)nKey;
	}
	else
		_view->KeyUp(nKey);
}
The SDK documentation has basically two short sentences with an ambigous explanation of KeyUp/Down/Char in Noesis Integration Tutorial:
When a key is pressed you must use KeyDown() and KeyUp(). To send already processed UTF-32 characters, Char is provided.
Those two sentences don't really explain the behavior I'm seeing. What am I doing wrong?
 
User avatar
jsantos
Site Admin
Posts: 3906
Joined: 20 Jan 2012, 17:18
Contact:

Re: C++ Integration - forwarding keyboard inputs

08 Oct 2019, 06:11

KeyDown(), KeyUp() and Char() functions are expected to be called from WM_KEYDOWN, WM_KEYUP and WM_CHAR respectively in Windows. I recommend just having a look at any Win32 application to see how those messages are being sent. We also provide that integration inside Win32Display.cpp.

Indeed, we need to improve the documentation about this but the expected order of events for each key is: Down, Char and then Up. It is important that Char happens between Down and Up and that you don't forget to call the Up event.

Who is online

Users browsing this forum: Google [Bot] and 42 guests