Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,605 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 923 people online right now. Registration is fast and FREE... Join Now!




Detecting when a key is being held in SDL

2 Pages V  1 2 >  
Reply to this topicStart new topic

Detecting when a key is being held in SDL, Clueless :(

gabehabe
post 27 Jun, 2008 - 02:28 PM
Post #1


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


I started learning SDL recently, and now I'm working on my first project. Problem is, I've come to a bit of a dead end...

I'm making a copy of pong, and I'm stuck on how to make the paddle continue to move when the key is being held.

Basically, it will only move once when the key is pressed, and won't continue to move while the key is being held.

This is what I have so far:
cpp
            while (SDL_PollEvent (&this->event))
{
if (event.type == SDL_QUIT)
SDL_Quit ();
if (event.type == SDL_KEYDOWN)
{
Uint8 *KeyStates = SDL_GetKeyState (NULL);
if (KeyStates [SDLK_UP])
this->offset.y -= 10;
if (KeyStates [SDLK_DOWN])
this->offset.y += 10;

SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00 ));
apply_surface(offset.x, offset.y, surface, screen);
SDL_Flip (surface);
}
}

Thanks in advance smile.gif
User is online!Profile CardPM

Go to the top of the page

Cerolobo
post 27 Jun, 2008 - 04:00 PM
Post #2


D.I.C Regular

Group Icon
Joined: 5 Apr, 2008
Posts: 440



Thanked 31 times
My Contributions


I haven't done any work with SDL, so I'm really just grasping at the air.

From what I can tell, SDL_GetKeyState() does not have to be called when event.type == SDL_KEYDOWN. You should actually do the checks you did in that if statement, in your actual main game loop. The SDL_KEYDOWN will only be generated when ever the key is originally pressed.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 29 Jun, 2008 - 10:16 AM
Post #3


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Thanks Cerolobo, I can see what you're saying, but a friend of mine explained it...

The best way to do it is to create a bool.
Then, make it true when event.type == SDL_KEYDOWN
and false when event.type == SDL_KEYUP

Thanks anyway smile.gif

This post has been edited by gabehabe: 29 Jun, 2008 - 10:16 AM
User is online!Profile CardPM

Go to the top of the page

gabehabe
post 16 Jul, 2008 - 08:30 AM
Post #4


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


I know it's a bump, but I thought I'd add it because it's pretty useful to know.

There's a one-liner way to do this, and it's a call to a standard SDL function, like so:
cpp
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);


And actually, this would have been better off in the game programming forum. But oh well, I've got a solution now anyway smile.gif
User is online!Profile CardPM

Go to the top of the page

polymath
post 16 Jul, 2008 - 08:45 AM
Post #5


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


Actually, it probably wouldn't, no offence to any of those game programmers, but when I look at that forum answers don't come nearly as fast as they do here.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 16 Jul, 2008 - 08:49 AM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


It would, keystates can be applied to any language, not just C++

Like I said though, I thought I'd offer that extra solution, it's pretty useful wink2.gif
User is online!Profile CardPM

Go to the top of the page

polymath
post 16 Jul, 2008 - 08:58 AM
Post #7


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


I think you missed the point: You could post it in game programming, and it can be used in more than one language, but the time it would take you to get an answer there would be double that of the C++ forum.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 16 Jul, 2008 - 09:16 AM
Post #8


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Do I need to post about you in the game programming forum and get ma game programming homies to come shoot yo' up?
User is online!Profile CardPM

Go to the top of the page

polymath
post 16 Jul, 2008 - 09:23 AM
Post #9


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


That would probably be pretty bad... almost as bad as posting in the Java forum with my sig as it is... smile.gif
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 16 Jul, 2008 - 11:24 AM
Post #10


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


LMAO! I just read your sig.

icon_up.gif
User is online!Profile CardPM

Go to the top of the page

kapax
post 16 Jul, 2008 - 11:34 AM
Post #11


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 39



Thanked 1 times
My Contributions


I have made one project with SDL and honestly, even it worked fine, I still haven't completely understood SDL event handler. I find it often not working as it is supposed to work, according to me and takes so long time to find out the reason.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 16 Jul, 2008 - 12:02 PM
Post #12


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


I'm just getting used to SDL event handling, it doesn't seem too bad...

Take a look at my blog later, I'll have uploaded my first game by then smile.gif
User is online!Profile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/23/08 02:23AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month