Mouse buttons

Can someone point me to a tutorial for using the mouse button event? or does anyone know how to check if a button is pressed down? Java has a method for the mouseinput called isButtonPressed() but jMonkey doesnt implement it.

Thank you. In that it has action listener just having an onAction method. When I am trying to implement it it says I should implement the abstract method actionPerformed. What is the difference between these? is actionPerformed just the new version of it or what is it used for? The documentation only says it is invoked when the action occurs which doesnt help much.

The tutorial is self-explanatory. Take the time to read the example in their entirety. Don’t just skim over it.

You add a mapping


inputManager.addMapping(“WhateverNameSuitsYou”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));


Then you add this to the proper listener (action or analog)


inputManager.addListener(actionListener, new String[]{“WhateverNameSuitsYou”});


Finally in the actionListener you tell jME3 what to do when that key is triggered.


private ActionListener actionListener = new ActionListener() {

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals(“WhateverNameSuitsYou”) && !keyPressed) {

isRunning = !isRunning;





I read it. I wanted to know about the actionPerformed method which is shown here

When I try to implement the ActionListener, it tells me I must implement this actionPerformed method because it is an abstract method. I wanted to know what this method is used for. This is not shown in the tutorial you linked to. I completely understood the actionListener and onAction method.

awt.event.ActionListener is an AWT API class. You want to use com.jme3.input.controls.ActionListener for jme3. You have imported the wrong class. And for listening to the mouse, you want com.jme3.input.controls.AnalogListener

Ok thank you. I have been trying to use analog but i need to be able to see if the mouse button is being held down, so I need to use the Action Listener since it has the keypressed boolean. Unless there is a way to do that using the analog listener.

You use the ActionListener for mouse clicks, AnalogListener for mouse motion events.

so if i want to click and hold on an object and move it along with the mouse, I would need to use both?

Yes. You will want to keep track that the mouse has been pressed using the ActionListener, and then track where it moves with the AnalogListener.

Ok. Ill try that. Thank you.

I was using a ray cast to determine where the box is i am trying to move. Should i still use this with the mouseaxis triggers or should i try to move the box in a ratio to the screen pixel size.

(sorry for all the questions. i have been stressing over this project for school)

yes, MouseAxisTrigger.

TestMousePick shows how to pick an object. Just modify it to pick when the button is pressed instead of picking every frame.

Is this a 3d scene you are working in?

You can move the box in 3d, say on a plane, by creating a large quad as a surface. Then whenever the mouse is moved, and you have the mouse button down and an object is selected, do a ray cast against the quad. That point it returns in the world is where you will move the object to.

Yes it is a 3d scene. Thats how i am trying to do it. What do you mean by a large quad as a surface? I just made a large, flat box to use as a plane.

Your large flat box as a plane will work as well. Just a surface that you can cast a ray against so you know where your selected item will move to. Like moving an item on the top of a table: you cast a ray against the top of the table from your mouse position to see where it intersects, and that is where the item you are moving moves to.

One problem I am having is that when i cleft click, the mouse cursor is disappearing and then when i release it is in the same place it was originally (the cursor). No matter where i try to move to.

Something is wrong in your code, you’re going to have to debug through it to see what is happening.

phantomliger11 said:
One problem I am having is that when i cleft click, the mouse cursor is disappearing and then when i release it is in the same place it was originally (the cursor). No matter where i try to move to.

There's nothing wrong with the code. :) That's the default behavior for the mouse input's binding.

The default binding will make the cursor disappear when the left/right mouse button is clicked (that's why you see the cursor disappear when rotating, but barely visible when doing a simple click because that goes faster).

Now, what I personally do is to remove the default binding are rebind it but only on the right mouse button.

// in my AppState class I have an unbinding method that removes several default mapping.
// in a different method I rebind it.
gMgrs.getInputManager().addMapping("ChaseCamToggleRotate", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));

The default binding uses both buttons.

Ah I never took notice of that. Learn something new every day :slight_smile:

So since I dont need the camera to rotate, I can just delete the binding. do the & and quot and ; mean a quotation mark?