PDA

View Full Version : how would i do something a key is lifted in SDL?



jimi_hendrix
November 24th, 2008, 12:56 AM
tell me if you want my source but lets say i read the key input and then go to a switch statement where i read the event and then do something per key...how would i do something until that key is lifted...

Lux Perpetua
November 24th, 2008, 01:42 AM
The relevant information is here:

http://www.libsdl.org/cgi/docwiki.cgi/Introduction_to_Events
http://www.libsdl.org/cgi/docwiki.cgi/SDL_KeyboardEvent

You can check for SDL_KEYUP to figure out if the key has been lifted.

By the way, SDL has great documentation:

http://www.libsdl.org/cgi/docwiki.cgi/SDL_API

jimi_hendrix
November 24th, 2008, 01:46 AM
thanks

jimi_hendrix
November 24th, 2008, 02:00 AM
ok that stuff didnt really help but why is this code wrong
void handleInput()
{
bool keepGoing = true;
while (keepGoing == true)
{
keepGoing = false;

//get the keystate and begin checking for keydowns
Uint8* Keys = SDL_GetKeyState(NULL);

if(Keys[SDLK_LEFT])
{
x -= velX;
keepGoing = true;
}

if(Keys[SDLK_RIGHT])
{
x += velX;
keepGoing = true;
}
}
}

jimi_hendrix
November 24th, 2008, 02:01 AM
it just hangs

jimi_hendrix
November 24th, 2008, 01:08 PM
bump

raf_kig
November 24th, 2008, 01:50 PM
SDL_GetKeyState:
Gets a snapshot of the current keyboard state. The current state is returned as a pointer to an array. The size of this array is stored in numkeys. The array is indexed by the SDLK_* symbols (see SDLKey). A value of 1 means that the key is pressed and a value of 0 means that it is not. The pointer returned is a pointer to an internal SDL array. It will be valid for the whole lifetime of the application and should not be freed by the caller.

Note: Use SDL_PumpEvents to update the state array.
I suggest reading http://www.libsdl.org/cgi/docwiki.cgi/SDL_GetKeyState before using the function ;-)

Regards

raf

jimi_hendrix
November 24th, 2008, 09:17 PM
ill try it later and tell if it works

slavik
November 25th, 2008, 12:46 AM
oldie but goodie? those were the days ...

http://ubuntuforums.org/showthread.php?t=490914

jimi_hendrix
November 25th, 2008, 03:42 AM
looked at the code where you handled input and couldnt figure out how you did it...but i have a break coming up so i will have plenty of time to figure this out

slavik
November 25th, 2008, 04:31 AM
yeah, that's the bad part of the code :(

the idea is that the keysym returns an array where 1 is the key is pressed and 0 the key being unpressed (the key is an index in the array). what you want to do is act while the key is pressed. grabbing the keysym happens every iteration.

jimi_hendrix
November 25th, 2008, 05:13 AM
right...i can make my ship move when the player hits a key...but if they hold the key down it stands still until they pick their finger up and press the key again...i want it to keep on moving

slavik
November 25th, 2008, 05:16 AM
my code does that, compile it ;)
look carefully at the bodies of the if statements.

jimi_hendrix
November 25th, 2008, 05:17 AM
i know...im trying to figure out how it does that ;)