Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / General Programming / Simple input question Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

May 24, 2005, 06:08 PM

Since i love flipcode, i'd like to ask this question here.

NOTE: I am using SDL, yet i think this is a general question.

Well, obviously i need to get input from the user (keyboard in particular), and i don't like my current implementation. Im not sure whether it relates to SDL in particular either.

Anyway, I have about 100 or more booleans, each one is set to either true or false upon the use of keyboard (set using ascii values). Now, i have a couple of issues. First of all, is if I have a console, and i want to type something in, my engine has 60fps, and so holding a key for just a fraction of a second yields about 20 keys. Thats not good. But it doesnt just relate to console, imagine a player jump, or shoot (mouse click)... With each jump, i add a certain force through my physics engine to the player entity, causing him to nicely jump up, but if i hold down space, my player just flies up as though he's got a needle shoved up his ass. Not good.

Same thing in shooting with a pistol, i cant allow my simple pistol to shoot as though it was AK-47 with a simple mouse-click, but still if the person holds down a mouse button, i want to be able to set a certain interval, could someone basically explain to me how to set it? (the interval that is?).




May 24, 2005, 11:19 PM



May 24, 2005, 11:51 PM

I respect some (most even) of your answers, but this isnt the one :(

Good try though.



May 25, 2005, 01:11 AM

Well, for things like the jump and the pistol, you basically just hang onto a collection of variables that record the time of the last jump, time of the last fire, etc. Don't permit any new jumps until a certain interval has elapsed since the last one, and the same for weapon firings and other similiar events.

For firing a weapon, this gives you a fixed rate of fire if the user holds down the button. This is appropriate for, say, automatic weapons like SMGs, but if it's a pistol you may want to use a different input model where the weapon fires each time the player clicks the mouse (and doesn't fire repeatedly if they hold down the button). This also applies to the typing system, in which you want to add a character each time the player presses a key. So you must detect actual key presses; in order to do this, just maintain two arrays of booleans, one for the last frame and one for the current frame, and compare them to determine if the mouse button or key has been pressed (or released).

If you want to get fancy you can even track the time at which each key was pressed, enabling you to repeat keys that have been held down when typing.

Rui Martins

May 25, 2005, 04:34 AM

The simplest way, in my view, is to have a boolean to keep the state of each control!
By control I mean, "left","right", "jump", "fire", etc...
Then you can bind the behaviour of your controls to a specific key, making it configurable and independent of the physical control, this is, it can be a keyboard, a mouse click, a network packet, or whatever.

To use the keyboard, you just need to have a callback, that will be called whenever a key is pressed or released. In some APIs, you can even disable the repeat feature of the keyboard, hence diminishing the processing of the keyboard input.

And I'm affraid BRIAN was right, if you want to control how you handle the behaviour you have to wait until the key is released, i.e. until the "control" is actuated in the oposite sense, activate/deactivate => press/release.
This is how you control the repeat or not of any control/action.

Maybe you are using a callback or similar that tells you which ASCII character is being pressed, which is completly different from just calling you when the key is hit.

Erik Faye-Lund

May 25, 2005, 04:44 AM

For a jump, you shouldn't just wait a certain amount of time (like reedbeta said), since this will make your player being able to jump while falling off a cliff etc. you should also take weither the player in the air or not into the account.


May 25, 2005, 05:35 AM

or, in sdl, you could just poll the messages and react on the SDL_KEYDOWN messages...

oh, and, for typing, you can even tell sdl to send repeating messages after a while if you want, so that you can just press for a while and it starts repeating the key.


May 25, 2005, 09:09 AM

"oh, and, for typing, you can even tell sdl to send repeating messages after a while if you want, so that you can just press for a while and it starts repeating the key. "

Could you tell me how to do that please? Thanks!

Also, thank you for all of your replies. So just holding an array of booleans to keep track of the keys is enough? And, the way i understand this now, is that the way the keyboard input is handled it's part of the game and not of the engine?

I appreciate you clearing this up for me =)



May 25, 2005, 09:59 AM

but as i said. this is for a MESSAGE BASED SYSTEM. means, you directly get and react on SDL_KEYDOWN and SDL_KEYUP.. without any array.

Rui Martins

May 25, 2005, 10:30 AM

davepermen wrote: ... this is for a MESSAGE BASED SYSTEM. means, you directly get and react on SDL_KEYDOWN and SDL_KEYUP.. without any array.

You still need an array of booleans just for your controls typically 4 or 5, not for every key.
You need to keep the state of each control, active/inactive.

If the control is progressive, like an analog joystick, than use a float or an int. For example to control the stearing of a car.

Victor Widell

May 25, 2005, 11:10 AM

" I respect some (most even) of your answers, but this isnt the one :("

Think about it... If you only trigger the event when the button state changes from down to up, there will be only one shot/jump. I believe you can implement that yourself. And the principle is the same to trigger at up to down.


May 25, 2005, 12:35 PM

But how many games have u seen, that utilize shooting, or jumping upon the release of a key (mouse button)?

I think it will be quite uncomfortable for the gamers...



May 25, 2005, 12:55 PM

Can't think of a title offhand, but I have seen this type of input before. Usually you hold the button down to "build up power" and release it to trigger the action. =)

Victor Widell

May 25, 2005, 01:35 PM

"how many games have u seen, that utilize shooting, or jumping upon the release of a key (mouse button)?"

Some. Sonic The Hedgehog does this for the spinning start.


May 25, 2005, 01:36 PM

hm no, i never had a need for global key storage.. the things that hold the state are the game-objects.

instead of a

  2. idle
  3. {
  4.    if(key[LeftArrow] == DOWN)
  5.    {
  6.       Actor->TurnLeft();
  7.    }
  8. }


  2. onKeyLeftArrow()
  3. {
  4.    Actor->StartLeftTurning();
  5. }

thus, completely removing the need of any lowlevel state variables. possibly actor has a bool IAmLeftTurning, wich stores its state, yes, but possibly not.

oh, and normally, there is a middle layer, too..

means KeyLeftArrow gets bound to some Controller->MoveLeftTrigger, that, by itself, if in game, triggers the Actor->StartLeftTurning().

the controller has all the possible things a "brain" can do (user, computer), to play the game.

and the keyboard then can get bound differently to the controller, thus allowing for user defined keyboard settings.

or so..

oh, and.. no global states, still.. all event based :D


May 26, 2005, 01:05 AM

It's just as easy to trigger something on the press of a key as well as the release, and this makes the controls feel more responsive. Even typing is done this way - the letter appears on the screen when you press the key, not when you release it.


May 26, 2005, 04:43 AM

I think BRIAN was half right, but you should also stand on something to be able to jump.

This is my attempt at pseudocode. :P

  2. if ( keys[JUMPKEY] == UP ) jumpenabled = true;
  4. if ( keys[JUMPKEY] == DOWN && jumpenabled && playertouchestheground ) {
  5.     jump();
  6.     jumpenabled = false;
  7. }


May 26, 2005, 09:23 AM

That way a person would be able to jump while constantly holding down the space key.

Thanks for your responses, i guess ill have the objects in game keep timers for this kind of stuff.



May 26, 2005, 12:41 PM

No he can't. Come on, the pseudo code wasn't that hard to read? :D

Check it again. You need to release the jump key before you can jump again.


May 26, 2005, 02:27 PM

i meant he can't... LOL sorry, typo. Happens a lot =) *ashamed*


May 26, 2005, 02:50 PM

Oh, you need him to be able to do that?

Then just let him jump only if touching the ground. :)


May 26, 2005, 08:49 PM

heh, i realize that, i just wanted to give a simple example of the dillema, and get a more/less general resolution =) But thanks for the suggestion ;)


This thread contains 22 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.