Homemade SDL getkey function - please help!

I was happy to early my homemade getkey routine still has problems.

  1. Multiple calls - say 3 - don’t mean that the rountine will wait for multiple (3) key presses.
    I know why but I couldn’t come up with a better solution.
  2. Using my current broken design I would need to write a new “else if ( keys[…” line (see code below) for every key the function should recognize. That’s not exactly cool. I tried passing keysym directly but that didn’t work for some reason.

I’m working on this problem for hours now and it’s really frustrating so please help! (and to the SDL developers: Please fix the WaitEvent/KeyRepeater combo so that it can deal with speedy input)

int getkey(void)
{
SDL_Event event;
Uint8 *keys;
Uint32 t, t2;
int ret = 0;
static int hit = 0;

SDL_PollEvent(&event);
if (event.type == SDL_KEYDOWN) { hit = 1; }  
else if ( event.type == SDL_QUIT )  {  exit(1);  } 

back:

if (hit == 1) {
	keys = SDL_GetKeyState(NULL);

	if ( keys[SDLK_UP] ) { ret = KEY_UP; }
	else if ( keys[SDLK_DOWN] ) { ret = KEY_DOWN; }
	else if ( keys[SDLK_LEFT] ) { ret = KEY_LEFT; }
	else if ( keys[SDLK_RIGHT] ) { ret = KEY_RIGHT; }
	else if ( keys[SDLK_ESCAPE] ) { ret = KEY_ESCAPE; }
	
	hit = 2; 
} else if (hit == 2) {
	t = SDL_GetTicks();
	t2 = t + 200;

	do {
		t = SDL_GetTicks();			
		SDL_PollEvent(&event);
		if (event.type == SDL_KEYDOWN) { hit = 1; goto back; }			
	} while (t < t2);
	hit = 3;


} else if (hit == 3) {
		SDL_Delay(20);
		keys = SDL_GetKeyState(NULL); 

		if ( keys[SDLK_UP] ) { ret = KEY_UP; }	
		else if ( keys[SDLK_DOWN] ) { ret = KEY_DOWN; }
		else if ( keys[SDLK_LEFT] ) { ret = KEY_LEFT; }
		else if ( keys[SDLK_RIGHT] ) { ret = KEY_RIGHT; }
		else if ( keys[SDLK_ESCAPE] ) { ret = KEY_ESCAPE; }
		
		
} 


if (event.type != SDL_KEYDOWN && hit != 3) {

	do {
		SDL_WaitEvent(&event);
	} while (event.type != SDL_KEYDOWN);

	hit = 1;
	goto back;

}


return ret;

}

whiteindian at arcor.de wrote:

SDL_PollEvent(&event);

try changing SDL_PollEvent to SDL_WaitEvent
also always check the return status for errors…
and use the event to see which key is being pressed/released
not SDL_GetKeyState

this whole thing seems overcomplicated for what it is trying to do.–
-==-
Jon Atkins
http://jonatkins.org/

----- Original Nachricht ----Von: Jonathan Atkins
An: sdl at libsdl.org
Datum: 26.04.03 05:59
Betreff: Re: [SDL] Homemade SDL getkey function - please help!

@whiteindian_at_arcor wrote:

SDL_PollEvent(&event);

try changing SDL_PollEvent to SDL_WaitEvent
also always check the return status for errors…
and use the event to see which key is being pressed/released
not SDL_GetKeyState

this whole thing seems overcomplicated for what it is trying to do.

Read my previous post (SDL Keyrepeater trouble). I can’t use SDL_WaitEvent.
One nasty thing I’ve learned is that only the actual pressing down of a key
is an event for SDL. A key that is hold down isn’t.

I want something like getch() , damn normal keyboard input like it is found everywhere except in real-time video games. SDL’s way to get this would be SDL_WaitEvent + Keyrepeat.
But as I said in my previous post this combo is buggy: it can’t deal with speedy input.
To repeat my example (from my game): Running in one direction (by holding down the arrow key) and then quickly switching to running in another direction (by releasing the arrow key and pressing another one fast) doesn’t work: the player figure only moves one step in the new direction and then stops. It seems to be a bug in SDLs Keyrepeater so I’ve to try to write a workaround.

you can get SDL to give you multiple keydown messages if a key is held down

just do this somewhere after your SDL_Init

SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

then just do your normal message getting stuff. you’re making it way too
complicated…

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

I want something like getch() , damn normal keyboard input like it is
found everywhere except in real-time video games. SDL’s way to get this
would be SDL_WaitEvent + Keyrepeat.
But as I said in my previous post this combo is buggy: it can’t deal
with speedy input. To repeat my example (from my game): Running in one
direction (by holding down the arrow key) and then quickly switching to
running in another direction (by releasing the arrow key and pressing
another one fast) doesn’t work: the player figure only moves one step
in the new direction and then stops. It seems to be a bug in SDLs
Keyrepeater so I’ve to try to write a workaround.

This is in the documentation.

Go to http://www.libsdl.org and then click on “Doc Project” and then look
in section 3 “Input Handling” especially “Handling the Keyboard”.

Of interest to you is the sections at the end under the sub-section title
"Game-type Input".

Hope that helps,
ChrisOn Sat, 26 Apr 2003 whiteindian at arcor.de wrote:

Try Shaklee Vitamins: Free Sample! http://www.tryshaklee.com/newstream

dang, sorry, i didnt read all of your post, so ingore my last one…

the problem isnt with SDL, you’d get the same effect in notepad if you held
down one arrow key then switched to the other. right after the switch, it’d
wait a little bit to see if you were acutally holding down the key or if it
was just one press. actually, you can change how long it waits (in windows)
in the keyboard properties -> repeating options -> repeat delay or somethin
like that. therefore, you’re not gonna get this to work with key presses, i
dont think.

why not just get the state of the button?

Uint8 *keyboard = SDL_GetKeyState(0);
if(keyboard[SDLK_RIGHT])
MoveRight();
else if(keyboard[SDLK_LEFT])
MoveLeft();

why do you need to do it with “regular” input??

M@_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail

----- Original Nachricht ----
Datum: 26.04.03 05:59

whiteindian at arcor.de wrote:

SDL_PollEvent(&event);

try changing SDL_PollEvent to SDL_WaitEvent
also always check the return status for errors…
and use the event to see which key is being pressed/released
not SDL_GetKeyState

this whole thing seems overcomplicated for what it is trying to do.

Read my previous post (SDL Keyrepeater trouble). I can’t use SDL_WaitEvent.
One nasty thing I’ve learned is that only the actual pressing down of a key
is an event for SDL. A key that is hold down isn’t.

I want something like getch() , damn normal keyboard input like it is found everywhere except in real-time video games. SDL’s way to get this would be SDL_WaitEvent + Keyrepeat.
But as I said in my previous post this combo is buggy: it can’t deal with speedy input.
To repeat my example (from my game): Running in one direction (by holding down the arrow key) and then quickly switching to running in another direction (by releasing the arrow key and pressing another one fast) doesn’t work: the player figure only moves one step in the new direction and then stops. It seems to be a bug in SDLs Keyrepeater so I’ve to try to write a workaround.

The releasing of a key also also generates an SDL event.
So what I do (and what I’ve seen in numerous other programs)
is create an array of flags for the keys I’m interested in
and set the flag for a keydown event and clear it for a keyup
event. If a flag is set the key has been pressed and not released
(is being held down) and your program can react accordingly
– hope this helps

RJP
<@Roy_Pluschke>On Sat, 26 Apr 2003 06:29:47 +0200 (CEST) whiteindian at arcor.de wrote:

Von: Jonathan Atkins
An: sdl at libsdl.org
Betreff: Re: [SDL] Homemade SDL getkey function - please help!

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The releasing of a key also also generates an SDL event.
So what I do (and what I’ve seen in numerous other programs)
is create an array of flags for the keys I’m interested in
and set the flag for a keydown event and clear it for a keyup
event. If a flag is set the key has been pressed and not released
(is being held down) and your program can react accordingly
– hope this helps

Have a look at the SDL_GetKeyState() function. It does exactly this for you,
so you don’t have to do it yourself.

cu,
Nicolai

RJP

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+q5A8sxPozBga0lwRAoOSAKDQRKotiAlMgrIow45InJRb8zm6bACgpqDz
OFbftUR7CRrm1InslMMekGE=
=B4V/
-----END PGP SIGNATURE-----On Sunday 27 April 2003 09:34, Roy Pluschke wrote: