Reading text from keyboard

Hi

I’m looking for a comfortable possibility to read text from the keyboard. At
the Moment I’m checking for a lot key combinations for special characters,
but that’s really bad, specially because I got a swiss keyboard…
Is there a better way to do this job, I already tried the SDL input events
but it seems they also don’t give you key combinations (like shift+1, …).

greetings
Milo Spirig–
+++ GMX - Mail, Messaging & more http://www.gmx.net +++
NEU: Mit GMX ins Internet. Rund um die Uhr f?r 1 ct/ Min. surfen!

They sure do, but all physical keys are treated as such, regardless
of keymaps and stuff. Think of the keyboard as a gigantic gamepad. :slight_smile:

As to your problem, you’re not supposed to do this yourself in an
application, ever. That won’t work with different layouts (like
Dvorak… heh), and it hardcodes your application to a specific
language for no good reason.

Doesn’t UNICODE provide the information you need?

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`---------------------------> http://olofson.net/audiality -’
http://olofson.nethttp://www.reologica.se —On Friday 17 January 2003 15.20, toytoy at gmx.ch wrote:

Hi

I’m looking for a comfortable possibility to read text from the
keyboard. At the Moment I’m checking for a lot key combinations for
special characters, but that’s really bad, specially because I got
a swiss keyboard… Is there a better way to do this job, I already
tried the SDL input events but it seems they also don’t give you
key combinations (like shift+1, …).

Thanks for your tip, I will try to use unicode.

-----Urspr?ngliche Nachricht-----Von: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org] Im Auftrag von David
Olofson
Gesendet: Freitag, 17. Januar 2003 18:46
An: sdl at libsdl.org
Betreff: Re: [SDL] Reading text from keyboard

On Friday 17 January 2003 15.20, @Milo_Spirig wrote:

Hi

I’m looking for a comfortable possibility to read text from the
keyboard. At the Moment I’m checking for a lot key combinations for
special characters, but that’s really bad, specially because I got a
swiss keyboard… Is there a better way to do this job, I already
tried the SDL input events but it seems they also don’t give you key
combinations (like shift+1, …).

They sure do, but all physical keys are treated as such, regardless
of keymaps and stuff. Think of the keyboard as a gigantic gamepad. :slight_smile:

As to your problem, you’re not supposed to do this yourself in an
application, ever. That won’t work with different layouts (like
Dvorak… heh), and it hardcodes your application to a specific
language for no good reason.

Doesn’t UNICODE provide the information you need?

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. | RT and
| off-line synth. Scripting. Sample accurate timing. |
`---------------------------> http://olofson.net/audiality -’
http://olofson.nethttp://www.reologica.se


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi

I now added unicode support with SDL_EnableUNICODE(1) and try to process the
events like this:

while(SDL_PollEvent(&Event))
{
	switch(Event.type)
	{
		case SDL_KEYDOWN:
			if((Event.key.keysym.unicode & 0xFF80) == 0)
			{
				char ch = Event.key.keysym.unicode &

0x7F;
if(ch)
m_strText += ch;
}
}
}

The problem is that the variable ch is always zero whatever key I press. Do
you have any idea what I’m doing wrong? (I’ve got WinXP)

greetings
Milo Spirig

-----Urspr?ngliche Nachricht-----Von: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org] Im Auftrag von David
Olofson
Gesendet: Freitag, 17. Januar 2003 18:46
An: sdl at libsdl.org
Betreff: Re: [SDL] Reading text from keyboard

On Friday 17 January 2003 15.20, @Milo_Spirig wrote:

Hi

I’m looking for a comfortable possibility to read text from the
keyboard. At the Moment I’m checking for a lot key combinations for
special characters, but that’s really bad, specially because I got a
swiss keyboard… Is there a better way to do this job, I already
tried the SDL input events but it seems they also don’t give you key
combinations (like shift+1, …).

They sure do, but all physical keys are treated as such, regardless
of keymaps and stuff. Think of the keyboard as a gigantic gamepad. :slight_smile:

As to your problem, you’re not supposed to do this yourself in an
application, ever. That won’t work with different layouts (like
Dvorak… heh), and it hardcodes your application to a specific
language for no good reason.

Doesn’t UNICODE provide the information you need?

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. | RT and
| off-line synth. Scripting. Sample accurate timing. |
`---------------------------> http://olofson.net/audiality -’
http://olofson.nethttp://www.reologica.se


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

dang, man, you’re making this waaaay more hard than it has to be… this is
what i use…

if(event.type is KEYDOWN)
{
if(event.key.keysym.sym == SDLK_BACKSPACE)
// take the last letter off, if there are any letters
else if(event.key.keysym.sym == SDLK_RETURN)
// process it, send it, whatever
else
{
// i’m using c type strings (char vectors) so my code is kinda complex
if(strlen(saying) < MAX_LEN)
saying[strlen(saying)] = (char)event.key.keysym.unicode;

// it looks like you could just do
m_strText += (char)event.key.keysym.unicode;

}
}_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail

Err? He’s not overcomplicating anything; your code is incorrect. You can’t
blindly cast 16-bit Unicode values to a (typically) 8-bit char; if the
value is above ASCII (ISO-8859-1, actually: U+00FF), you’ll get garbage.
It’d be more straightforward for him to write
"Event.key.keysym.unicode < 0x80", though.

Milo (please set your real name in your From line, so we don’t have
to search for it), back up a bit: see if you’re getting any non-zero
values in keysym.unicode.On Sat, Jan 18, 2003 at 06:55:50AM +0000, Matt Monson wrote:

dang, man, you’re making this waaaay more hard than it has to be… this is
what i use…

// i’m using c type strings (char vectors) so my code is kinda
complex
if(strlen(saying) < MAX_LEN)
saying[strlen(saying)] = (char)event.key.keysym.unicode;

// it looks like you could just do
m_strText += (char)event.key.keysym.unicode;
}
}


Glenn Maynard

dang, man, you’re making this waaaay more hard than it has to be… this
is
what i use…

The code I’m using is from http://sdldoc.csn.ul.ie/sdlkeysym.php, so I think
it can’t be that bad :slight_smile:
But I replaced it now with:

if(Event.key.keysym.unicode < 0x80 && Event.key.keysym.unicode > 0)

But the unicode member of keysym is still always zero. I already tried to
use SDL_GetKeyName() and it gives me the correct key name.

Any ideas?

greetings
Milo Spirig

Hi Milo,

[…]

The code I’m using is from http://sdldoc.csn.ul.ie/sdlkeysym.php, so I think
it can’t be that bad :slight_smile:
But I replaced it now with:

if(Event.key.keysym.unicode < 0x80 && Event.key.keysym.unicode > 0)

But the unicode member of keysym is still always zero. I already tried to
use SDL_GetKeyName() and it gives me the correct key name.

Your code works perfectly at my system.
Are you really sure that you have added unicode support?

Please send a complete minimal example for further testing.On Saturday 18 January 2003 11:57, Milo Spirig wrote:


Johannes Schmidt

< http://libufo.sourceforge.net > Your widget set for OpenGL

Hi

Thanks for everybody’s help, the code works now but it’s pretty weird. If a
call SDL_EnableUNICODE(1) when I initialize my programm it doesen’t work,
but if I call the function every frame it does. I don’t know what’s the
problem, but I’m happy that it finally works :slight_smile:

greetings
Milo Spirig

You’ll have to ensure that you call SDL_EnableUNICODE(1) after SDL_Init.

SDL_Init overides the unicode flag with the default value (which means
disabling it).On Saturday 18 January 2003 14:06, Milo Spirig wrote:

Thanks for everybody’s help, the code works now but it’s pretty weird. If a
call SDL_EnableUNICODE(1) when I initialize my programm it doesen’t work,
but if I call the function every frame it does. I don’t know what’s the
problem, but I’m happy that it finally works :slight_smile:


Johannes Schmidt

< http://libufo.sourceforge.net > Your widget set for OpenGL

Hello SDL-users, I have an odd situation at hand.

I’m writing an application that allows users to press combinations of
keys simultaneously…I’m running into an error where SDL won’t catch
all the keys: for example

I pressed the backquote, get keysym=96 sent to SDL_KEYDOWN.
I let go.

I pressed the letter a, get keysym=97 sent to SDL_KEYDOWN
I let go.

I pressed the letter w, get keysym=119 sent to SDL_KEYDOWN.
I let go.

Next, when I press and hold A, then W and then I try to pressed the
backquote, either SDL_KEYDOWN doesn’t get an event - or - the event
occurs with the keysym 0.

Does anyone know why this happens? Yet even better, how do I fix this?
Or…maybe it’s an SDL “quality.”

Chuck

It’s a hardware restriction and there is nothing you can do to fix it.On Saturday 18 January 2003 17:46, Chuck Mason wrote:

Hello SDL-users, I have an odd situation at hand.

I’m writing an application that allows users to press combinations of
keys simultaneously…I’m running into an error where SDL won’t catch
all the keys: for example

I pressed the backquote, get keysym=96 sent to SDL_KEYDOWN.
I let go.

I pressed the letter a, get keysym=97 sent to SDL_KEYDOWN
I let go.

I pressed the letter w, get keysym=119 sent to SDL_KEYDOWN.
I let go.

Next, when I press and hold A, then W and then I try to pressed the
backquote, either SDL_KEYDOWN doesn’t get an event - or - the event
occurs with the keysym 0.

Does anyone know why this happens? Yet even better, how do I fix
this? Or…maybe it’s an SDL “quality.”

Chuck Mason wrote:

Next, when I press and hold A, then W and then I try to pressed the
backquote, either SDL_KEYDOWN doesn’t get an event - or - the event
occurs with the keysym 0.

Does anyone know why this happens?

It’s a trait of most keyboards, and therefore SDL can’t do much about
it.

http://www.sjbaker.org/steve/omniv/keyboards_are_evil.html--
Kylotan
http://pages.eidosnet.co.uk/kylotan

Hi

Thanks for everybody’s help, the code works now but it’s pretty weird. If a
call SDL_EnableUNICODE(1) when I initialize my programm it doesen’t work,
but if I call the function every frame it does. I don’t know what’s the
problem, but I’m happy that it finally works :slight_smile:

You need to call the function after you set the video mode, that’s all.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Try making ch (unsigned) instead of (char).

Regards,

DanielOn Friday 17 January 2003 23:04, ToyToy wrote:

I now added unicode support with SDL_EnableUNICODE(1) and try to process
the events like this:

while(SDL_PollEvent(&Event))
{
switch(Event.type)
{
case SDL_KEYDOWN:
if((Event.key.keysym.unicode & 0xFF80) == 0)
{
char ch = Event.key.keysym.unicode & 0x7F;
if(ch)
m_strText += ch;
}
}
}

The problem is that the variable ch is always zero whatever key I press. Do
you have any idea what I’m doing wrong? (I’ve got WinXP)

He’s only looking at characters that don’t have any bits above 7 set.
if((Event.key.keysym.unicode & 0xFF80) == 0) is a slightly obfuscated
version of if((Event.key.keysym.unicode < 0x80).

I’m guessing he was just trying to get ASCII working with Unicode
enabled before worrying about other Unicode values; I believe I suggested
(or at least meant to suggest) he look at the Event.key.keysym.unicode
value directly (in a debugger).On Wed, Jan 22, 2003 at 04:31:39AM +0100, Daniel Phillips wrote:

  		if((Event.key.keysym.unicode & 0xFF80) == 0)
  		{
  			char ch = Event.key.keysym.unicode & 0x7F;
  			if(ch)
  				m_strText += ch;
  		}
  }
}

The problem is that the variable ch is always zero whatever key I press. Do
you have any idea what I’m doing wrong? (I’ve got WinXP)

Try making ch (unsigned) instead of (char).


Glenn Maynard