Mouse/Key Listener version of FengGUIInputHandler

I’ve always been more comfortable assigning listeners to KeyInput and MouseInput than using InputHandlers.

To facilitate that, I rewrote the FengGUIInputHandler (available here, on the wiki: http://www.jmonkeyengine.com/wiki/doku.php?id=fengjmeinputhandler) as a Listener that can be subscribed to KeyInput and MouseInput.



This is almost identical to the InputHandler version, but I just find it more convenient to use this way:



import org.fenggui.Display;
import org.fenggui.event.Key;
import org.fenggui.event.mouse.MouseButton;
import org.lwjgl.input.Keyboard;

import com.jme.input.KeyInputListener;
import com.jme.input.MouseInputListener;

/**
 * Listener version of the FengGUIInputHandler.
 * Subscribe this to KeyInput and MouseInput.
 * @author Joshua Keplinger
 */
public class FengGUIListener implements MouseInputListener, KeyInputListener {


   private Display disp;
   
   private boolean keyHandled;
   private boolean mouseHandled;
   
   public FengGUIListener(Display disp)
   {
      this.disp = disp;
   }
      
   public boolean wasKeyHandled()
   {
      return keyHandled;
   }
   
   public boolean wasMouseHandled()
   {
      return mouseHandled;
   }
      

   private boolean down;
   private int lastButton;
   
   public void onButton(int button, boolean pressed, int x, int y)
   {
      mouseHandled = false;
      down = pressed;
      lastButton = button;
      if(pressed)
         mouseHandled = disp.fireMousePressedEvent(x, y, getMouseButton(button), 1);
      else
         mouseHandled = disp.fireMouseReleasedEvent(x, y, getMouseButton(button), 1);
   }

   public void onMove(int xDelta, int yDelta, int newX, int newY)
   {
      mouseHandled = false;
      // If the button is down, the mouse is being dragged
      if(down)
         mouseHandled = disp.fireMouseDraggedEvent(newX, newY, getMouseButton(lastButton));
      else
         mouseHandled = disp.fireMouseMovedEvent(newX, newY);
   }

   public void onWheel(int wheelDelta, int x, int y)
   {
      mouseHandled = false;
      // wheelDelta is positive if the mouse wheel rolls up
      if(wheelDelta > 0)
         mouseHandled = disp.fireMouseWheel(x, y, true, wheelDelta);
      else
         mouseHandled = disp.fireMouseWheel(x, y, false, wheelDelta);


   }
   
   /**
    * Helper method that maps the mouse button to the equivalent
    * FengGUI MouseButton enumeration.
    * @param button The button pressed or released.
    * @return The FengGUI MouseButton enumeration matching the
    * button.
    */
   private MouseButton getMouseButton(int button)
   {
      switch(button)
      {
         case 0:
            return MouseButton.LEFT;
         case 1:
            return MouseButton.RIGHT;
         case 2:
            return MouseButton.MIDDLE;
         default:
            return MouseButton.LEFT;
      }
   }

   @Override
   public void onKey(char character, int keyCode, boolean pressed) {
         keyHandled = false;      
         Key key = mapKeyEvent();
         if(pressed)
         {
            keyHandled = disp.fireKeyPressedEvent(character, key);
            // Bug workaround see note after code
            if (key == Key.LETTER || key == Key.DIGIT)
               keyHandled = disp.fireKeyTypedEvent(character);
         } else
            keyHandled = disp.fireKeyReleasedEvent(character, key);
   }
   
   /**
    * Helper method that maps LWJGL key events to FengGUI.
    * @return The Key enumeration of the last key pressed.
    */
   private Key mapKeyEvent()
   {
      Key keyClass;
      
        switch(Keyboard.getEventKey())
        {
           case Keyboard.KEY_BACK:
              keyClass = Key.BACKSPACE;
               break;
           case Keyboard.KEY_RETURN:
              keyClass = Key.ENTER;
               break;
           case Keyboard.KEY_DELETE:
              keyClass = Key.DELETE;
               break;
           case Keyboard.KEY_UP:
              keyClass = Key.UP;
              break;
           case Keyboard.KEY_RIGHT:
              keyClass = Key.RIGHT;
               break;
           case Keyboard.KEY_LEFT:
              keyClass = Key.LEFT;
               break;
           case Keyboard.KEY_DOWN:
              keyClass = Key.DOWN;
               break;
           case Keyboard.KEY_SCROLL:
              keyClass = Key.SHIFT;
               break;
           case Keyboard.KEY_LMENU:
              keyClass = Key.ALT;
               break;
           case Keyboard.KEY_RMENU:
              keyClass = Key.ALT;
               break;
           case Keyboard.KEY_LCONTROL:
              keyClass = Key.CTRL;
               break;
           case Keyboard.KEY_RSHIFT:
              keyClass = Key.SHIFT;
               break;    
           case Keyboard.KEY_LSHIFT:
              keyClass = Key.SHIFT;
               break;             
           case Keyboard.KEY_RCONTROL:
              keyClass = Key.CTRL;
               break;
           case Keyboard.KEY_INSERT:
              keyClass = Key.INSERT;
               break;
           case Keyboard.KEY_F12:
              keyClass = Key.F12;
               break;
           case Keyboard.KEY_F11:
              keyClass = Key.F11;
               break;
           case Keyboard.KEY_F10:
              keyClass = Key.F10;
               break;
           case Keyboard.KEY_F9:
              keyClass = Key.F9;
               break;
           case Keyboard.KEY_F8:
              keyClass = Key.F8;
               break;
           case Keyboard.KEY_F7:
              keyClass = Key.F7;
               break;
           case Keyboard.KEY_F6:
              keyClass = Key.F6;
               break;
           case Keyboard.KEY_F5:
              keyClass = Key.F5;
               break;
           case Keyboard.KEY_F4:
              keyClass = Key.F4;
               break;
           case Keyboard.KEY_F3:
              keyClass = Key.F3;
               break;
           case Keyboard.KEY_F2:
              keyClass = Key.F2;
               break;
           case Keyboard.KEY_F1:
              keyClass = Key.F1;
               break;
           default:
              if("1234567890".indexOf(Keyboard.getEventCharacter()) != -1) {
                 keyClass = Key.DIGIT;
              } else {
                 // @todo must not necessarily be a letter!! #
                 keyClass = Key.LETTER;
              }
              break;
       }
      
        return keyClass;
   }
   
}

I'd be nice if you could put this version on another wiki page, and add links to the FengGUI on jME main wiki page to it (with a small text explaning the differeces). That way, this won't get lost in the forum.

Cool, thanks for sharing. I am having problems with the regular InputHandler… will probably give this a try

I'd be nice if you could put this version on another wiki page, and add links to the FengGUI on jME main wiki page to it (with a small text explaning the differeces). That way, this won't get lost in the forum.


Thanks, that hadn't occurred to me. Consider it done.

Thanks again :slight_smile:

Thank you SO much, I've found this much more convenient to use than the other version. The other version should be removed from the wiki IMHO because it is problematic when switching between GameStates (the InputHandlers are very hard to disable and require modifications to the other version before they can be disabled properly).

Actually, if you are using the latest FengGUI from SVN, the FengGUIListener needs some adaptations to work properly.

The InputHandler version has already been updated (see: http://www.jmonkeyengine.com/wiki/doku.php?id=new_integrating_fenggui_with_jme).



I just updated the listener to work with this version as well, see: http://www.jmonkeyengine.com/wiki/doku.php?id=new_integrating_fenggui_with_jme&s[]=fenggui