Intermediate Lesson 7 - Better keyboard queue

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
icehot
Posts: 7
Joined: November 27th, 2012, 11:47 am

Intermediate Lesson 7 - Better keyboard queue

Post by icehot » December 7th, 2012, 10:31 pm

I noticed that watching this lesson chili was trying to convert keycodes into upper and lowercase characters and it seemed like a long winded way of going about it as he said you'd have to code for every locale and possible combination of shift keys etc so I came up with the following changes to the keyboard class which seems to work well...

First change in windows.cpp for the keydown event:

Code: Select all

case WM_KEYDOWN:
			kServ.OnKeyPressed(wParam, HIBYTE(LOWORD(lParam)));
			break;
This is to also send the scancode of the key to the server.

Then in the keyboard server onkeypressed method:

Code: Select all

void KeyboardServer::OnKeyPressed(unsigned char keycode, unsigned char scancode)
{
	keystates[keycode] = true;
	WORD buffer[5]; // arbitrary length i used for testing
	BYTE kbs[256];
	GetKeyboardState(kbs);
	ToAscii(keycode, scancode,kbs,buffer,0);
	keybuffer.push(buffer[0]);
}
Unfortunately the ToAscii function shown here uses a different format for the keyboard state so it's easiest just to return the it using the windows function.

Finally in the game.cpp go method:

Code: Select all

while(kbd.PeekKey())
	{
		unsigned char keypress = kbd.ReadKey();
		if (isprint(keypress))
		{
			int length = strlen(textBuffer);
			if (length < 79)
			{
				textBuffer[length] = keypress;
				textBuffer[length+1] = '\0';
			}
		}
	}
Just the one line is changed to ensure it's a printable character otherwise you end up getting spaces in the text buffer for any keypress... It seems to work well, but I'm sure it can be further improved however it does do what chili wanted by having the other characters and the correct ascii codes all in the same buffer.

NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Re: Intermediate Lesson 7 - Better keyboard queue

Post by NaturalDemon » December 14th, 2012, 10:34 pm

it seams the pro handle all the mouse, keyboard stuff inside the Game::Go()/Render function and frame by frame.

i think chili´s aim with the mouse.., keyboard server is to mimics the directinput "class" stuff.

i´m not 100% sure .. but i see most guys on youtube just use std::string type, my gues chili´s aim was to teach the basic bit stuff 0-255 and what it can resemble.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Intermediate Lesson 7 - Better keyboard queue

Post by chili » December 16th, 2012, 2:04 pm

Yeah, I didn't start off with std::string because I wanted to show the difference between C and C++ and I wanted to teach about pointers and c strings because it is important to understand. I am goingto be using stl string more and more as we go on.
Chili

Post Reply