KEY_UP and KEY_DOWN Issue -- SOLVED

Today I noticed that previously working behavior in a “pre-alpha” game I’m working on has stopped working. My mapping/AnalogListener combination for KEY_UP and KEY_DOWN is no longer being called. I had it set up like this:



[java]

// replace default handling of mouse movement (i.e. rotation)

inputManager.deleteMapping(“FLYCAM_Left”);

inputManager.addMapping(“FLYCAM_Left”, new KeyTrigger(KeyInput.KEY_LEFT));

inputManager.deleteMapping(“FLYCAM_Right”);

inputManager.addMapping(“FLYCAM_Right”, new KeyTrigger(KeyInput.KEY_RIGHT));

inputManager.deleteMapping(“FLYCAM_Up”);

inputManager.addMapping(“FLYCAM_Up”, new KeyTrigger(KeyInput.KEY_UP));

inputManager.deleteMapping(“FLYCAM_Down”);

inputManager.addMapping(“FLYCAM_Down”, new KeyTrigger(KeyInput.KEY_DOWN));

inputManager.addListener(flyCam, “FLYCAM_Left”, “FLYCAM_Right”, “FLYCAM_Up”, “FLYCAM_Down”);

[/java]



Which basically just re-mapped things so only the keyboard (and not the mouse and keyboard) controls rotation. Rotating left and right works as expected. Rotating up and down used to work, but no longer does. I’m not sure what changed.



After it stopped working I tried some different things. I created my own AnalogListener (instead of re-using flyCam). Still nothing: I pressed the keys and the execution never hit the listener at all. I tried different key mappings. That worked. For example, replacing KeyInput.KEY_DOWN with KeyInput.KEY_LBRACKET makes down rotation work as expected.



So it seems to be isolated to the KEY_UP and KEY_DOWN key triggers.



As an experiment, I tried adding an ActionListener for the KEY_UP and KEY_DOWN mappings. Curiously, the ActionListener sees the key events, but it only sees the isPressed==false events. Pressing the up or down arrows never generates an isPressed==true call to the ActionListener. Pressing other keys generates both true and false calls to the ActionListener.



Any advice?

Just a quick note on the fix.



The easiest fix is to set focusable=“false” for the Nifty controls in the HUD. With focusable=“false” set, the button that was causing the problem still responds to mouse-clicks, but no longer consumes the KEY_UP and KEY_DOWN events. Alternatively, I think you could override the button’s default input mapping, which is de.lessvoid.nifty.input.mapping.MenuInputMapping. I didn’t bother, though, since making the button non-focusable solved the problem.

1 Like

Do you use nifty?

Yes

If you have some nifty screen up then it is probably gobbling up your key down events. I don’t know how to resolve it but I’ve seen other posts about that.



…actually, I had experienced some of that myself I think but an update fixed it for my use-case. I haven’t checked the cursor up and down, though… and in my case no nifty screens were visible.

1 Like

Thanks; that gives me a good place to start looking. I do have a Nifty screen up (an in-game H.U.D.).



I had set the logging level for NiftyInputEventHandlingLog to WARNING because I found the INFO level to be incredibly verbose (some of those INFO level log statements from Nifty should have used FINE or FINER). I just put it back to INFO, and sure enough I see a pair of logged events for each keypress. This is interesting:



pressing left (KEY_LEFT) rotates, and generates the following log statements:

11/10/11 18:51:52 INFO NiftyInputEventHandlingLog : [processKeyboardEvent] de.lessvoid.nifty.input.keyboard.KeyboardInputEvent@5f27a49c processed [false]

11/10/11 18:51:52 INFO NiftyInputEventHandlingLog : [processKeyboardEvent] de.lessvoid.nifty.input.keyboard.KeyboardInputEvent@23a82e92 processed [false]



pressing (KEY_UP) does not rotate, and generates the following log statements:

11/10/11 18:52:00 INFO NiftyInputEventHandlingLog : [processKeyboardEvent] de.lessvoid.nifty.input.keyboard.KeyboardInputEvent@708d8f67 processed [true]

11/10/11 18:52:00 INFO NiftyInputEventHandlingLog : [processKeyboardEvent] de.lessvoid.nifty.input.keyboard.KeyboardInputEvent@66a96863 processed [false]



Notice the false/false vs. true/false pairs. I haven’t dug into it yet, but I bet that’s the return value from the Nifty “processKeyboardEvent” call, and I bet it’s reporting whether the event was handled/consumed or not. That “true” for KEY_UP sure looks like exactly what you were talking about.



Source for Nifty isn’t included in jMonkeyPlatform, is it? I’ll see if I can grab the Nifty source and figure out what’s going on.

I don’t think source is included. I always just go to the nifty site to browse the code.

I added the Nifty src jar to the IDE so I could use the debugger.



Nifty is consuming the keypress. It goes from Nifty$NiftyInputConsumerImpl.processKeyEvent() → Screen.keyEvent() → FocusHandler.keyEvent(). That method tries to handle the event with the focused Nifty element. In my case, there is a focused Nifty button that is silently consuming the event. KEY_UP and KEY_DOWN must be used for Nifty button navigation with the default configuration.



Now that I know what’s going on, I should be able to fix it. Thanks for the pointer!

Hi !

I have a similar issue. Except, instead of eating my up/down’s key press, Nifty shouldn’t have those bindings at all.

I suppose it comes from it’s own default keymap. I simply would like to remove some of those default mapping.

i.e : app.getInputManager().deleteMapping(“LeftMoveFocus”);

Is there a way to get the list (i.e. app.getInputManager().getMapList() ) or some documentation about the exhaustive list of those default values ?
I saw some of those on the website (like the escape key, …etc).

I’m not using the jplatform (eclipse IDE) and I’m a little bit lost with the files…

Nifty gets its events through a RawInputListener… there are no mappings in InputManager. So even if there was a way to get this information it would not be helpful in this case.

ok, i’ll check some workaround then. Thanks.