Preferred Input Method

Hi All,



I'm fairly new to jME and I am trying to figure out if there is a preferred way to handle input.  I've see a variety of methods, including (I am looking at mouse input, but it could be anything…):


  • MouseInput.get().addListener( new MouseInputListener()...

  • Polling the state of the mouse within the simpleUpdate() function.

  • Writing a custom handler, like so:  input.addAction(myCustomHandler, InputHandler.DEVICE_MOUSE, InputHandler.BUTTON_ALL, InputHandler.AXIS_ALL, false );


I'd like to know what the preferred method is (if there is one).  If not (or even if so, if applicable), I'd like to know what the difference between the methods are.  Any insights?

Thanks!

All three methods will query the mouse but at different levels of code abstraction.  The first two methods you mentioned give you access to all mouse state, while the last one is only for button clicks.  Polling the mouse state within the simpleUpdate() function is the lowest level and probably not the best from a design point of view.  The MouseInput().get().addListener method is what you can use if you need mouse position or wheel position info.  An alternative for getting mouse positions is to use RelativeMouse or AbsoluteMouse.  Here is code snippet taken from com.jmetest.TutorialGuide.HelloMousePick:


// Create a new mouse. Restrict its movements to the display screen.
am = new AbsoluteMouse("The Mouse", display.getWidth(), display
            .getHeight());
//....

// Get the mouse input device and assign it to the AbsoluteMouse
// Move the mouse to the middle of the screen to start with
am.setLocalTranslation(new Vector3f(display.getWidth() / 2, display
   .getHeight() / 2, 0));
// Assign the mouse to an input handler
      am.registerWithInputHandler( input );



If all you need to do is listen for button clicks then you can use the InputActionInterface method:

Writing a custom handler, like so:  input.addAction(myCustomHandler, InputHandler.DEVICE_MOUSE, InputHandler.BUTTON_ALL, InputHandler.AXIS_ALL, false );


there is a fourth method, the GameControl system.

But i can't tell you whats the preferred way to do things, i think its a matter of preference.