Multiple key pressed

Hi… surely this has been asked a millon times, but how is the code to detect 2 (or more) keys pressed at the same time?

Looked in many posts and they said something like action.setKey(an array) but i dont fully understand…



the way i am writing mi code is:



  KeyBindingManager.getKeyBindingManager().set(Player.JUMPING, KeyInput.KEY_I);
  KeyBindingManager.getKeyBindingManager().set(Player.WALKINGFORWARD, KeyInput.KEY_J);


  //the idea
  if (KeyBindingManager.getKeyBindingManager().isValidCommand(Player.JUMPING) && KeyBindingManager.getKeyBindingManager().isValidCommand(Player.WALKINGFORWARD)) { do something }



obviously tried this but dont work..
for what i want this?:

if forward pressed
walk
if up pressed
jump
if forward and up pressed
(gues!!) jump forward

thanks!

you want the set(String command, int[] keyCode) method.



You set the command with an array of keys. All keys in the array must be pressed to execute the command.

Hmmm, I’ll look into it.

Mojo… i am starting to need it… sorry… ://

Sorry vladl, was busy this week with life stuff. I’ll take a look at it as soon as possible.

Ok, please describe you problem in detail. In my test I created “I”, “Shift-I”, and others.



The results when pressing Shift-I is isValidCommand for “I” and “Shift-I” are true. Which is correct. The application will have to decide what to do in this case (if one overrides the other).



If that’s not your problem, let me know.

Ah, I think I follow, I was looking to low level (Key bindings themselves, which work fine), the input handler is not handling multiple key presses very well…

Ok, this is not as straightforward as you might think, as this isn’t a bug. Basically, if you add key bindings to an action, then add actions to the handler, the handler will call the perform action method on all actions that have their key criteria met. This means if you have an action with Shift-I as it’s binding and one with I, when Shift-I is pressed, both will have it’s criteria met.



What are people’s thoughts on this? Is this at the application level that it should determine what it should do? It would be hard for the Handler to do this intelligently, as there are multiple cases here. It would have to look at all the actions that are possible candidates to be called, see if any share a key, and of those determine which has the most keys pressed. And will this always be the case? Do you never want to have two actions called at the same time that share keys? What if there is a Shift-I action and a CTRL-I action and Shift + CTRL + I is pressed? They share I, but have the same number of keys.



While many of the examples have obvious answers, my point is the solution is not so obvious for generic cases.



Any input would be appreciated.

It gets more complicated… lets say the currently pressed buttons were:

[shift, i, a]



and you had actions defined for the following four combos:

  1. shift+i, 2. shift+a, 3. a, 4. i



    What would get fired off?



    In my own development, I would never use key combos per se. Hitting a key like shift for example would tell my game that a certain state was in place… like “run state” in a FPS. Or like cntrl might mean “alternate key interpretations state” Then when you hit the ‘a’ key, it would go apply state logic like “a = walk left” well, run state == true, so walk left at a rate of 2x normal. etc. Windows does this for the alt key… pressing alt puts you in a “command” mode where a single key press has a different meaning than normal.



    My 2cents anyhow.

I think the demands of different game types are - different.



In action/fps games, you typically combine various keypresses like (duck and walk) or (jump and walk) or even (jump and reload). The way the game reacts may depend on the game logic itself.



In tekken-like games it may be more complicated, for key combos may be not only a combination of parallel key presses but a sequence of them.



In a strategy game on the other hand keys like shift/ctrl/alt may alter an internal mode, like discussed before.



Currently I doubt there is a single high level abstraction which can easily handle all requirements. Therefore I have written my own input handler to meet my demands. What I was personally missing was the possibility inside of an action to find out, which other actions will be called in that step, too. Actions are too isolated from each other probably.



Just my 2 cents, I have not proceeded in my game for a while, too busy :frowning:

Perhaps, if in performAction it takes an event class that contains all the keys pressed? That would at least let you know if some of your other actions were called. Or I could wait until all actions are determined to be valid, build an event that contains all the names of the actions and then call the performAction methods on all the actions. Any of that sound like it would be worth using?

What about having access to the KeyBindingManager from inside an AbstractInputAction, perhaps by passing some sort of readonly-reference to performAction?



What I mean is creating an interface containing isValidCommand, make KeyBindingManager implement this, then extend performAction of the AbstractInputAction by such a parameter and pass the KeyBindingManager to the performUpdate method each time it gets called in an InputHandler.



That way you could request the key states or the validity of other actions from inside the action which is called without enabling the action to perform any changes on the binding manager (i think this pattern is called visitor).



Maybe there is a way to do this without breaking all current actions, like having a non-abstract performAction method with the new additional parameter in AbstractInputAction, which implemented so that it calls the abstract method. That way all current actions will work, but new actions could use the new functionality by overwriting the new method

"batman.ac" wrote:
I think the demands of different game types are - different.
...
In tekken-like games it may be more complicated, for key combos may be not only a combination of parallel key presses but a sequence of them.
...

(

Sorry for this late post. I wasnt notified by email.

This is EXACTLY what i try to do: i want to bind a key (up key) for my fight game character to make him jump. Then a want to bind a key (left key) to make my character walk forward. AND want to bind the two key pressed together up and left to make it jump forward.

For the combos or the specials power i will take care later, but now i want to know if i can make this.

hello… any new about this topic?? can be done??

Wonderful XD

I will check out in a couple of days or maybe in the mid of the next week.