Process several key events simultaneously

Hi,

how can I respond to several key events simultaneously, so I can press several keys at the same time?

Do I need to set a value to true?

in your event listener you just need a lot of if statements (not else if, else). So that they are all executed per frame.

They are never called simultaneously but always sequentially.

I included several if statements, but if I press one key, the other keys are ignored.

you using an analog listener?

Does https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:combo_moves help?

No, I still have the same problem.

Here is my analog listener:

[java]

private AnalogListener analogListener1 = new AnalogListener() {

public void onAnalog(String name, float value, float tpf) {

if(isRunning) {

if(name.equals(KeySettings.actions[3])) {

activePlane.left(plane_phy);

}

if(name.equals(KeySettings.actions[4])) {

activePlane.right(plane_phy);

}

if(name.equals(KeySettings.actions[5])) {

activePlane.up(plane_phy);

}

if(name.equals(KeySettings.actions[6])) {

activePlane.down(plane_phy);

}

if(name.equals(KeySettings.actions[7])) {

activePlane.rollLeft(plane_phy);

}

if(name.equals(KeySettings.actions[8])) {

activePlane.rollRight(plane_phy);

}

}

else

System.out.println("Press Pause to unpause.");

}

};

[/java]



KeySettings[x] is a String constant.

Look at the onAnalog methods arguements each time it is called you just get one string in the variable name, it is not an array, therefore each time onAnalog is called you will only get one statement executed so you may as well use else if it may save you some processing.



I believe that like normen says, they are called sequentially each frame, probably using some kind of queuing. However I am not sure exactly what is happening it may be only one per frame but I don’t think that this would be the case.



What are you ultimately trying to achieve?

I have an object that you can move using WASD and rotated with the arrow keys and it would be nice if it could be moved in two directions at the same time.

I set this up…



[java]

public void onAnalog(String name, float value, float tpf) {

if(name.equals("W")) {

System.out.println("W");

}

if(name.equals("A")) {

System.out.println("A");

}

System.out.println(i + "


");
i++;
}
[/java]

And in update just print a line for "new frame"

I get this output...

[java]
W
22474
A
22475
New Frame
New Frame
New Frame
New Frame
New Frame
New Frame
W
22476
A
22477
New Frame
New Frame
New Frame
New Frame
New Frame
New Frame
W
22478
A
22479
[/java]

Which shows that the input is all managed in a single frame but it is not managed in every frame. Not once did I get a "new frame" in the middle of the input. I assume that then the tpf for update() and onAction()/onAnalog() are different! lol. Therefore you do get pseudo-simultaneous input but you need to manage it properly, and like zathras says I think that the combo moves page should help.

Run HelloTerrain.java which uses the FlyByCamera class, you can move in two directions there, however I think that when moving diagonally your speed is sqrt(2) and not 1, but it may not be I haven’t looked in to it. If it is then that isn’t hard to fix using vectors and matrices.

Allright, tank you. I think I will try the combo move method.

Well, If you don’t require the keys to be pressed simultaneously then you can just leave it as it is, I don’t see a problem using what you have without worrying about the combo moves, I think that you should look at the FlyByCamera class to see how that handles the input, and I had a brief look over it and it seems that it might not take into account that a velocity for combinations of W, A, S, D you can test this yourself but like I say it isn’t hard to fix.

The pseudo-simultaneity of the onAnalog method should suffice for you… well it certainly does for the FlyByCamera class.