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.