2 keyinput at the same time in jme3?

Hi,

I followed tut from zathras and read the TestControls but I still dont know how to have 2 keyinput at the same time

in testcontrols I found this code

inputManager.addMapping("My Action",

                new KeyTrigger(KeyInput.KEY_SPACE),

                new MouseAxisTrigger(MouseInput.AXIS_WHEEL, false));

so I tried another one like

inputManager.addMapping("a",new KeyTrigger(KeyInput.KEY_LEFT),new KeyTrigger(KeyInput.KEY_RIGHT));

but when I press left or right the action will run immediately.

are there any way to make the action run when I press both left and right key?

Set up booleans for each action. Instead of running the action when a key is pressed, have it set the boolean to true or false. So, if the keylistener detects a press or release, set it to the keyPressed variable. Then, run the action based on the boolean.

kidneytrader said:

Set up booleans for each action. Instead of running the action when a key is pressed, have it set the boolean to true or false. So, if the keylistener detects a press or release, set it to the keyPressed variable. Then, run the action based on the boolean.


I also thought about that before I opened this topic and tried this

class abc extends SimpleApplication implements AnalogListener

initKeys(){
inputManager.addMapping("left",new KeyTrigger(KeyInput.KEY_LEFT));
inputManager.addMapping("right",new KeyTrigger(KeyInput.KEY_RIGHT));
inputManager.addListener(this, new String[]{"left","right"});
}


@Override
public void onAnalog(String name, float value, float tpf) {
  if(name.equals("left") && name.equals("right")){
   animChannel1.setAnim("action1");
  }
}


this didnt work

and I also tried this


if(name.equals("left")){
         left=true;
}

if(name.equals("right")){
              right=true;
}
if(!name.equals("left")){                   //!left
                left=false;
}      
if(!name.equals("right")){                //!right
         right=false;
}
if(right && left){
         animChannel1.setAnim("action1");
}



with !left and !right then it didnt work
without them it works, but not as I expected : when I press left or right, then after some seconds I press the other one
and action1 will still run
I wanted it to run when I press both key, and after that it should return both false boolean (dont know how, like you can see in my code ^^ )

could you show me an example ?

You should probably be using ActionListener instead of AnalogListener, then you can use the boolean passed to onAction to determine if the button is being pressed, held or released. The boolean is false the first time, and true every time after that including the time onAction is called as it's released. So to react only on the first time, just use && !keyPressed

xieu90 said:

could you show me an example ?


Actually, I can do better. I had literally exactly the same issue you did while working on my project; you can find the code for it in this thread.

If you don't want to read through all of it, this is what's relevant:

private ActionListener actionListener = new ActionListener()
   {
      public void onAction(String name, boolean keyPressed, float tpf)
      {
         if (name.equals("yourActionName"))
         {
            yourActionBoolean = keyPressed;
         }
      }
   };



public void simpleUpdate(float tpf)
   {
      if (yourActionBoolean)
      {
         something.doAction();
      }



I didn't explain very well in my first post, but what I was trying to say was that you should use an ActionListener and set the boolean like this:

yourActionBoolean = keyPressed;

So when you press the button, the boolean becomes true because keyPressed is true. When you let go, the boolean becomes false because keyPressed is false. Then, you just do something based on the boolean in your update. Also, if you have problems with the action not stopping even after you set the boolean correctly, it's because in your update method the action never resets to the "don't do" state. Hope this is more clear, let me know if it still doesn't work!

Edit:

and I also tried this


if(name.equals("left")){
         left=true;
}
if left){
         animChannel1.setAnim("action1");
}



I don't understand exactly how the listener works, but I do know why your code didn't, so I'll try to explain: the listener action happens whenever the key's state is changed. Meaning, if you let go of the key, it still tells the listener about it even though the key isn't pressed. To actually know if the key is up or down, you have to use the keyPressed boolean like I demonstrated above.

thank you both of you ^^

it works a little bit now thought I still have problem



p1pl=new ActionListener(){
   public void onAction(String name, boolean keyPressed, float tpf) {// TODO onAction player 1
      if (name.equals("punchl"))
      {
         p1plb = keyPressed;
      }
}};
p1pr=new ActionListener(){
   public void onAction(String name, boolean keyPressed, float tpf) {
      if (name.equals("punchr"))
      {
         p1prb = keyPressed;
      }
}};


simpleupdate
if(p1pl b&& !p1prb){ //p1 punchleft boolean
   animChannel1.setAnim("attackhighhit");
        animChannel1.setLoopMode(LoopMode.DontLoop);
}
if(p1plb && p1prb){
   p1.setLocalTranslation(p1.getLocalTranslation().add(2, 0, 0));
      //animChannel1.setAnim("attacklowhit");animChannel1.setLoopMode(LoopMode.DontLoop);
}




punchl = left punch = pl
punchr = right punch = pr

so I wanted when I press pl the character will punch left and pr = punch right
and when I press both of them then it will make for example something like kamehameha ^^ here in the code i just made it move left.
the problem is when I press pl the animation should run immediately(doesnt matter if I hold pl or release it immediately) but the animation will always reset , until I release pl. I think it is because of p1plb = keyPressed; (somehow actionListener changes back to analog ^^ )

and here it also do something strange
pl (hold) -> reset anim  then pr together with pl -->punch then move  if I still hold them then it will punch move punch move
pr (hold) -> do nothing (cause I didnt tell it to do anything yet) then pl -> it will move until I release one or both (like in the code)
so the order will effect the animation too

so far I found these two problems.
@kidneytrader did you also have similar problems in your project ?