How to disable/release the F1 key for nifty-gui console? (or change it?)

Hi!



Ive been looking for some time now on how to do this, I made a few tries some time ago but they didnt work.

Do you know an efective way to disable the F1 key? I am currently using F2 key, but F1 is still working… I would like to use F1 key for a generic help display!



thx!!

teique said:
Hi!

Ive been looking for some time now on how to do this, I made a few tries some time ago but they didnt work.
Do you know an efective way to disable the F1 key? I am currently using F2 key, but F1 is still working... I would like to use F1 key for a generic help display!

thx!!


Did you try re-register the f1 key with an another action?

Hi @glaucomardano!



Yup,

if I register F1, my code will only receive the F1 Key Release event, the key pressed is used/grabbed at nifty-gui.



so I can only make use of

if(!isPressed)showHelp();

what would be enough to work, but as the F1 is still recognized by nifty-gui, it still uses it and also popups the console. (I think I could handle it if I resent a F1 keypress only event, but I still have no idea on how to do that; anyway it would be better to un-register it initially)

If you register a RawInputListener before you start up nifty then you will be able to catch the key events before nifty gets them. If you detect F1 then you can forward it to your own code and mark it consumed.

1 Like

I haven’t checked the Console control but I’m pretty sure the default key can be changed. It’s not like void to omit this. Sadly I don’t use the console and I’m out for the night so, dig down into Nifty’s code or visit void’s blog. There’s a mention of the console both on Nifty’s wiki and on void’s blog.



I’m sure you’ll find the info you need there. Google Nifty GUI to find the relevant sites.

@pspeed

this is working, still on the key release event, but I was able to prevent the keypress from activating the console, lotsa thx!!

[patch]

@Override

public void onKeyEvent(KeyInputEvent evt) {

if(evt.getKeyCode() == KeyInput.KEY_F1){

if(evt.isPressed()){

evt.setConsumed(); //consume the keypress event preventing it from being used by console

}

}

}

[/patch]

.

@madjack

I googled this: nifty gui “KeyInput.KEY_F1”

what would be a good guess, but found nothing…

I will check that later, to unbind it is still my goal, thx :slight_smile:

EDIT: Btw, I updated the console alone example on this thread http://hub.jmonkeyengine.org/groups/gui/forum/topic/nifty-gui-console-with-history-for-commands-xml-or-javabuilder-something-premade/?_wpnonce=0f28209ac0#post-128907

Got curious… Found a solution.



the console control uses: inputMapping=“de.lessvoid.nifty.input.mapping.MenuInputMapping” to capture the F1 key to bring the console up… Now, all you have to do is change it to your own class and voila!



This is a paste of the above default class. Switch the key for the one you need.



[java]

package de.lessvoid.nifty.input.mapping;



import de.lessvoid.nifty.input.NiftyInputEvent;

import de.lessvoid.nifty.input.NiftyInputMapping;

import de.lessvoid.nifty.input.keyboard.KeyboardInputEvent;



public class MenuInputMapping implements NiftyInputMapping {



public NiftyInputEvent convert(final KeyboardInputEvent inputEvent) {

if (inputEvent.isKeyDown()) {

if (inputEvent.getKey() == KeyboardInputEvent.KEY_F1) {

return NiftyInputEvent.ConsoleToggle;

} else if (inputEvent.getKey() == KeyboardInputEvent.KEY_RETURN) {

return NiftyInputEvent.Activate;

} else if (inputEvent.getKey() == KeyboardInputEvent.KEY_SPACE) {

return NiftyInputEvent.Activate;

} else if (inputEvent.getKey() == KeyboardInputEvent.KEY_TAB) {

if (inputEvent.isShiftDown()) {

return NiftyInputEvent.PrevInputElement;

} else {

return NiftyInputEvent.NextInputElement;

}

} else if (inputEvent.getKey() == KeyboardInputEvent.KEY_UP) {

return NiftyInputEvent.MoveCursorUp;

} else if (inputEvent.getKey() == KeyboardInputEvent.KEY_DOWN) {

return NiftyInputEvent.MoveCursorDown;

}

}

return null;

}

}

[/java]

Hey @madjack!



You mean I can disable/remove/overwrite an already existing NiftyInputMapping with this new one?

I was trying to find a way how to do that but couldnt :confused:



Btw, it would be a good replacer, but would be interesting to extends the original class, overhide and in case of F1, just return null; otherwise, use the super.conver() so I could keep updated compatibility with new releases of that class.



Do you know how I can “change it to “+my+” own class” ? :slight_smile:

Or you are speaking about getting the nifty sources and replace there? I am using the libs that comes with jme3, and most classes have no sources :/, I still need to try its SVN, I would like to change the console auto-completion to match case insensitively commands also!!

Lucky you I got side-tracked before going to bed. You owe me! :stuck_out_tongue:



In the screen where you have your console, BEFORE the screen tag, insert this:



[xml]

<controlDefinition name=“nifty-console” controller=“de.lessvoid.nifty.controls.console.ConsoleControl”>

<panel style=“nifty-console-panel”>

<control id="#listBox" name=“listBox” style=“nifty-console-listbox” displayItems="$lines" selectionMode=“Disabled” horizontal=“off” vertical=“optional”>

<text text=“Template” style=“nifty-console-listbox-item” controller=“de.lessvoid.nifty.controls.listbox.ListBoxItemController” inputMapping=“de.lessvoid.nifty.input.mapping.MenuInputMapping”/>

</control>

<control id="#textInput" name=“textfield” style=“nifty-console-textfield” />

</panel>

</controlDefinition>

[/xml]



Now, the following line taken from above:



[xml]

<text text=“Template” style=“nifty-console-listbox-item” controller=“de.lessvoid.nifty.controls.listbox.ListBoxItemController” inputMapping=“de.lessvoid.nifty.input.mapping.MenuInputMapping”/>

[/xml]



instead of inputMapping=“de.lessvoid.nifty.input.mapping.MenuInputMapping” you put the path to the “copy” of the class I pasted above after you have modified it to suit your need and given it a new name.



In case that didn’t work (try it first!), change the name of the controlDefinition above. It -is- possible that it could conflict. You could name it something like controlDefinition name=“myConsole” or whatever. Just make sure you do the same where your console control is defined in your screen XML.



I’ll check later.

mmm…



Yep that shall work indeed! Thx!



But, what I am trying to do is collect the unmodified nifty console xml from jme test data and disable every thing that is not related to the console, so I can make the console directly and exclusively available as a console reference to normal application usage! (at least as a quick starter for ppl needing to issue commands in a easy way from withing the application). (btw I am still trying to disable the fade effect that darkens the whole screen after you open the console a few times :))



So, my approach would be to try to replace the inputMapping with the new one.

I tried to find a way to collect it or replace (set) it but I still couldnt find :), will try more :smiley:

Looks like you’ve got some work ahead of you then. :wink:



Glad I could help.

yep, I know that, more research ahead :smiley:

I will probably use that for a console created “from zero”, thx!!

.

I think such facility is not available (a way we can remove a mapping); and as not knowing if it would be good if such one existed, I will stick to the “on released”, that is working good enough (in the moment) :slight_smile:

.

I think the proper way would be to create my own console from the beggining, and not reutilizing the existing one, but I still get confused with the xml.

[blabering]I need code completion very bad :/… I have trouble with string identifiers like “m_Color”, “AlphaDiscardThreshold” from materials… I try to make enums for them and use the .toString() when needed…[/blabering]

Just so you know, that the way it works with Nifty.



Void can’t customize controls for everyone’s need. But by giving us a framework that is “easily” modifiable (once you know how to), we can customize the already existing controls to do what WE want them to do, in the way we want them to act/react. Take the window control I “made”, that’s exactly what happened.



So, in short, the way I showed you to modify your console is exactly the way it should be done if the basic implementation doesn’t fit your need. :slight_smile:



I wouldn’t be surprised if Void avoided to use the ~ character as it is used by “consoles” in most games so people would make the effort to learn and find out to make it their own.

With all this effort to do things in the “easier” way (reusing ready examples), I am begging to get used to the xml :D.

I think I am naturally going to the “light side” :smiley:

thx again on the tips :slight_smile:



I will cut it all and make a console from zero!

I need many more dialogs, like material selector, etc etc etc…, better start doing them properly :slight_smile:



PS.: yes!! that I will do next, put the console on the ~ :smiley:

Erm, ok, I’ve not read your whole conversation but I think I might contribute the following piece of information:



Processing input in Nifty is a two step process:


  1. the physical input device event (a keyboard key pressed, a game controller button pressed and so on) is translated into a logical NiftyInputEvent
  2. the screencontroller or the control reacts to the logical NiftyInputEvent



    This is done so that you can eventually change the actual input device (use a different mapping) and keep the same implementation for the control or the screencontroller.



    In case of the console key all you need is your own NiftyInputMapping implementation:



    [java]public class MySpecialMapping implements NiftyInputMapping {

    public NiftyInputEvent convert(final KeyboardInputEvent inputEvent) {

    if (inputEvent.isKeyDown()) {

    if (inputEvent.getKey() == KeyboardInputEvent.KEY_ESCAPE) { // or whatever PHYSICAL event you need …

    return NiftyInputEvent.ConsoleToggle; // this is the LOGICAL event

    }

    … // map other keys as well, see all the other NiftyInputMapping implementations for reference

    [/java]



    and set this InputMapping implementation on the screen in question (you should already have an existing inputMapping set to enable handling events for the screen and you will just need to change that to your own implementation:



    [java]Screen screen = new ScreenBuilder("myScreen") {{

    inputMapping("my.package.MySpecialMapping"); // this will enable Keyboard events for the screen controller

    layer(new LayerBuilder("layer") {{

    …[/java]



    no change to the console control necessary at all.
1 Like

@void256,
worked, tho I had to use this screen.addPreKeyboardInputHandler() to make it cope with inputEvent.isKeyDown(), thx!