Jump to content
  • Advertisement
Sign in to follow this  
triqer

Holding a key down in SDL

This topic is 3259 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Currently I'm handling key presses using event polling. However, when a user holds the fire button I want the bullets to fire at a constant rate, i.e. I want the fire button to have a "holding down" functionality, but not the other keys. I've tried using a keystate, SDL_EnableKeyRepeat, and setting a flag when the key is pressed but I can't get any of them to quite work the way I want. Does anyone have any strategies they use when trying to accomplish something like this? Thanks. P.S. 1. The keystate problem was that while holding the key kind of worked, it was very inconsistent and bullets didn't continue to fire unless the user was moving at the same time (and this happened at an inconsistent rate.) 2. The SDL_EnableKeyRepeat problem was that I couldn't just specify it for the fire key. 3. In my flag idea I set a flag to true if the key was pressed, and then as long as that flag was true through every game loop a bullet would be created (obviously this would be changed for timing, but it was just a test). Once the key was released the flag would be put to false. However, this simply didn't work.

Share this post


Link to post
Share on other sites
Advertisement
SDL_GetKeyState.

Quote:
Original post by triqer
3. In my flag idea I set a flag to true if the key was pressed, and then as long as that flag was true through every game loop a bullet would be created (obviously this would be changed for timing, but it was just a test). Once the key was released the flag would be put to false. However, this simply didn't work.


That sounds like it should work. What was the problem?

Share this post


Link to post
Share on other sites
Quote:
1. The keystate problem was that while holding the key kind of worked, it was very inconsistent and bullets didn't continue to fire unless the user was moving at the same time (and this happened at an inconsistent rate.)

2. The SDL_EnableKeyRepeat problem was that I couldn't just specify it for the fire key.


1. This almost sounds like something in your firing logic updates only once every time through the main game loop. Do you have 1) a main loop that updates as often as possible, or something that updates only upon input (i.e., the input that causes your character to move?

2. SDL does not allow that kind of precision. You'll have to enable key repeat for all keys, and then use additional hand-coded logic to take action only when a particular key is pressed or released.

3. This is similar to what I suggested in #2, but did you only program a single bullet to be fired? Try this approach: find the amount of time that's passed sine your last update() call (or the last time you fired) and divide by your firing interval. Have your logic fire that many bullets.

Share this post


Link to post
Share on other sites
@The OP: You should definitely be able to do what you're describing (that is, use event polling for input and still track when a key is being 'held down').

This will mostly be a repeat of what's already been said, but here are a couple of tips.

First, SDL's 'key repeat' functionality isn't what you're looking for here. Key repeat is useful when entering text (e.g. in a console), but you'll most likely want to turn it off during gameplay.

Second, number 3 in your list is probably what you want: set a flag when a 'key down' event is received, and clear it when a 'key up' event is received. If this didn't work for you, it was probably just due to a flaw in your code (this applies to number 1 in your list as well).

I would suggest trying method 3 again, and doing some debugging to try and figure out why it isn't working as expected. (It sounds like you may have some fundamental problems with your main loop and/or method of updating game entities, which may have something to do with it.)

Share this post


Link to post
Share on other sites
I re-implemented the 3rd method, because I kind of just threw it together as a test to see if it would work. Now I have a boolean flag in the actual Ship class for whether the ship is firing and set the flag to true or false from the handle input method and use a separate method for the bullet creation.

I didn't handle the bullet creation in the actual handle input method because it is only called when an event happens.

In short, I got it to work. Thanks for the insight.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!