Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Input fun

This topic is 5929 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

I''m using SDL to get input. The way my game works right now is it gets the current key states for all the keys, then it goes and does something like this: if (keys[UP]) Ship->GoUp(); else if (keys[DOWN]) Ship->GoDown(); But now lets say I''m trying to make a console. If I get input that way, then pressing a key makes it type in something like this: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_ even when I press "a" for just an instant. So how should I get around this?

Share this post

Link to post
Share on other sites
so, currently you have a function that loads the "keys" array with true values, with it''s index equivalent to key codes, when you press a key, right?

Do you have a function that changes those true values to false when the key is released? I bet that''s the problem, you''re just forgetting to tell the program the key was released.

However, I would use a slightly different approach. Your current "keys" array could technically tell us when every singal key is pressed. The problem is, it''s very memory intensive, and running through it to check every key is slow. Instead, I would use a stack that stores the keycodes of the keys that are currently depressed. Push the keycode when a key is pressed, and pop the keycode when the key is released. You could limit your stack size to maybe 5 keys, you really don''t need that many.

Share this post

Link to post
Share on other sites
I believe the problem lies in the fact that you check the keyboard buffer every frame, which will be many times a second, so if you hit a key for like 1 second you will print as many characters to your console as you have frames per second.

For your console it would be much better to only print characters after recieving a WM_KEYUP event in the message loop.

Runicsoft -- home of my open source Function Parser and more

Share this post

Link to post
Share on other sites
Input can be a pain, to take the raw information the computer is giving you and making heads or tails out of it.

There''s many approaches. If you just want to know the instant a button is pressed, a flag can help.

if(key[UP] && !upChecked)
upChecked = true;

upChecked = false;

If you want predefined input as long as a button is held down, then just use button presses and releases to toggle a flag

upIsDown = true;
upIsDown = false;

//do some predefined, key repeat independent thing
//like fire a constant laser or something

there''s a lot of other things you can do. Interpreting input just right can take a bit of finessing.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!