Non Blocking Way To Grab Keyboard Input?


TrevorBradley

Active Member
Joined
Nov 6, 2007
Messages
732
I've started to add chat to my game, and started to hit a major headache.

EDIT: Should have mentioned I'm using C++/SDL, if anyone wasn't following along.

When I was checking for individual keys, using SDL Keydown events worked just fine. But now that I'm looking to use the entire keyboard for text chat, it looks like a nightmare.

I actually went through and did it the dumb way, checking for individual keys, combinations of shifts, and translated the event.key.keysym.mod into a char. But some of my users use international keyboards and all my hack work seems like twice the waste now.

I'm wondering if there's a platform independent way of pulling in keyboard input to get characters that doesn't block.

Alternately if there's no easy non-blocking solution and I decide to run "cin" to pull keyboard input in a different thread (probably should have done this in the first place), how well does it work for foreign characters?

Thanks in advance!
 
If nothing works you could still see how glut does it.
Thats what I do, the only problems are the modifier keys (ctrl, alt, shift) which I plan to use for game controls in the pc version of my game. But there are some ways explained at other sources.
 
OK, this has gotten worse. Apparently German keyboards are identical to US keyboard, just with different letters painted on them. So "Sharp S" (ß) to the right of 0 is intepreted as SDLK_MINUS or '-', and Shift-"Sharp S", which should be ? (for Germans) is really _. Germans need to type Shift - (_) to get ?.

On the pandora, this won't be an issue. Well it might be, depending on if the keyboards are different in different countries.

I'll start moving towards that cin separate thread solution.. :(
 
Trevor Bradley said:
OK, this has gotten worse. Apparently German keyboards are identical to US keyboard, just with different letters painted on them. So "Sharp S" (ß) to the right of 0 is intepreted as SDLK_MINUS or '-', and Shift-"Sharp S", which should be ? (for Germans) is really _. Germans need to type Shift - (_) to get ?.

On the pandora, this won't be an issue. Well it might be, depending on if the keyboards are different in different countries.

I'll start moving towards that cin separate thread solution.. :(
did you miss the first reply in this thread or did you try it out and it didn't work?

The keysym struct has a "unicode" field that contains the translated unicode character. But you need to do SDL_EnableUNICODE(1) to enable unicode translation. Presumably the unicode field should contain the correct character for you to output/send.
 
Last edited by a moderator:
bencoder said:
did you miss the first reply in this thread or did you try it out and it didn't work?

The keysym struct has a "unicode" field that contains the translated unicode character. But you need to do SDL_EnableUNICODE(1) to enable unicode translation. Presumably the unicode field should contain the correct character for you to output/send.



Your post seemed to be contradicted by my tests. No unicode was being sent.

I wasn't aware of SDL_EnableUNICODE(1) though. That might be perfect.

*reads manual*

Yup, that should be perfect. I'll have my crack squad of German testers get right on that... :)
 
Last edited by a moderator:
Trevor Bradley said:
Your post seemed to be contradicted by my tests. No unicode was being sent.
It wasn't my post, I just noticed it and thought you might have missed what looked like exactly what you wanted. Also, rereading my last post sounds like I came across as quite argumentative. It wasn't the tone I was going for so my apologies. It's a great game, good luck :)
 
Last edited by a moderator:
bencoder said:
Trevor Bradley said:
Your post seemed to be contradicted by my tests. No unicode was being sent.
It wasn't my post, I just noticed it and thought you might have missed what looked like exactly what you wanted. Also, rereading my last post sounds like I came across as quite argumentative. It wasn't the tone I was going for so my apologies. It's a great game, good luck :)


No worries...

Sadly, turning Unicode on appears to have no effect. German keyboards really do appear to be just like US keyboards with different letters painted on them. (Though I think someone had the sanity to properly switch Z and Y)

I'll research this a bit more. Maybe someone has figured out the subtlety.

Either that or I need a CHEAP German keyboard delivered to Canada.. Even one that was broken with a few keys missing would be fine. :)
 
Last edited by a moderator:
Trevor Bradley said:
Sadly, turning Unicode on appears to have no effect. German keyboards really do appear to be just like US keyboards with different letters painted on them. (Though I think someone had the sanity to properly switch Z and Y)

I'll research this a bit more. Maybe someone has figured out the subtlety.

Either that or I need a CHEAP German keyboard delivered to Canada.. Even one that was broken with a few keys missing would be fine. :)
I was under the impression that keyboards generally do send out the same signals for the same keys(with different letters/characters painted on them) and it's the keyboard driver used by your operating system that changes things, like if I changed to a german keyboard layout, when I hit the keys they wouldn't be what's on the keyboard, they'd be what's on a german keyboard. Of course I might be wrong about this but I'm pretty sure that's the general operation :S
 
Last edited by a moderator:
In a small test the unicode-component seems to carry the correct unicode-values (Linux, Locale: en_US.UTF-8, keymap: de-latin-1).

This small program reads the keys via SDL and prints them to stdout. It even works with dead keys.
CODE

#include "stdlib.h"
#include "SDL/SDL.h"
#include "locale.h"

int main(int argc, char *argv[])
{
SDL_Surface *screen;
SDL_Event event;
int running = 1;

if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
printf("SDL Error: %s\n", SDL_GetError());
return 1;
}

atexit(SDL_Quit);

screen = SDL_SetVideoMode(320, 240, 0, SDL_ANYFORMAT);
if (screen == NULL)
{
printf("Video Error: %s\n", SDL_GetError());
return 1;
}

SDL_EnableUNICODE(1);
setlocale(LC_ALL, "");

while (running)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
if (event.key.keysym.unicode != 0)
{
printf( "Unicode: %i, Char: %C\n", event.key.keysym.unicode, event.key.keysym.unicode);
}
break;
case SDL_QUIT:
running = 0;
break;
}
}
}
return 0;
}
 
Small nit:
For headers not in the same directory (i.e., system headers) you should use <>, not "", i.e.
#include <stdlib.h>
and so on.
 
Klaus said:
In a small test the unicode-component seems to carry the correct unicode-values (Linux, Locale: en_US.UTF-8, keymap: de-latin-1).

This small program reads the keys via SDL and prints them to stdout. It even works with dead keys.
Klaus, that's fantastic. Thank you!!!

EDIT: Quick note... I was still checking event.key.keysym.sym and not event.key.keysym.unicode
 
Last edited by a moderator:
Back
Top