Change nifty keybindings

Hi,

I’m trying to use the arrow keys for movement in the game but nifty is consuming those events. So I’m wondering how to change the inputmappings. I tried the following:

gui:

< screen id=“hud” visibleToMouse=“false” inputMapping=“dae.repest.gui.controllers.EmptyInputMapping” preInputMapping=“dae.repest.gui.controllers.EmptyInputMapping” controller=“dae.repest.gui.controllers.EmptyController” focusable=“false”>

The empty input mapping returns null for every input event:

[java] */
public class EmptyInputMapping implements NiftyInputMapping{

@Override
public NiftyInputEvent convert(KeyboardInputEvent inputEvent) {
   return null;
}

}[/java]

The empty controller looks like this :

[java]
public class EmptyController implements ScreenController, KeyInputHandler {

@Override
public void bind(Nifty nifty, Screen screen) {
   
}

@Override
public void onStartScreen() {
  
}

@Override
public void onEndScreen() {
  
}

@Override
public boolean keyEvent(NiftyInputEvent inputEvent) {
  return true;
}

}
[/java]

But the arrow key events are still swallowed by the nifty input management system. Does anybody have an idea how to solve this ?

Hi,

Nifty Doc:

keyEvent

boolean keyEvent(NiftyInputEvent inputEvent)
handle a key event.
Parameters:
inputEvent - key event to handle
Returns:
true when the event has been consumend and false if not

So you should return false in keyEvent Method.

Same issue, but I was hoping to take it a step farther.

While this solution prevented nifty from consuming the input, it does not prevent nifty from making use of the input event. Is there a way to prevent this?

That is to say, while pressing the up key a character on the screen will move forward but nifty will also be switching focus on any buttons. And if the enter key is bound to an action it will also trigger whatever nifty button happens to have focus.

I am not sure what you want.

if you don’t want nifty to execute any code don’t bind any key or implement an KeyInputHandler with no behaviour.
[java]
public class EmptyNiftyKeyInputHandler implements KeyInputHandler{

public boolean keyEvent(NiftyInputEvent inputEvent) {
   return false;
}

}
[/java]

If you want instead that both get the event, do what you need in keyEvent() and still return false.

Hope that helps.

With your code, both my input manager & nifty are receiving the key event.
Without your code, only nifty receives the key event

Nifty binds the following keys automatically:

  1. The arrow keys & tab key will cycle focus on nifty controls
  2. And the enter/return key will execute a nifty control, such as a button, if it has focus.

I was looking for a way to disable this behavior.

I may have found a solution,
which was to remove the “focusable” flag in the .xml for all nifty controls on my screen. I’m not sure if nifty is making use of any other keys that I don’t know about, but for the moment this seems to be a good workaround.