A little KeyTrigger question

In my code I’d like to trigger “key events” when something happen where I can catch it with a custom listener.



I’ve setup a class that extends KeyInput containing 2 “special characters” 0xFF and 0xFD so it’s can’t be triggered by the keyboard. Then I made a GameTrigger class implementing Trigger. Finally I set up an action listener.



All seems well. The keys are accepted by GameInput, GameTrigger classes and the keys are mapped into the AddMapping and addListener methods.



Now, I don’t know if that’s the proper way to do it, but I trigger the special key using:



new GameTrigger(0xFF);



as it’s usually done in SimpleApp, but for some reason these are not detected… I wonder why. Anything I should look for in particular?



Here’s some code.

[java]public class GameInput implements KeyInput {



public static final int KEY_HINT = 0xFF;

public static final int KEY_PROG = 0xFD;

… (snipped the @override)

[/java]



[java]public class GameTrigger implements Trigger {



private int key;



public GameTrigger(){

}



public GameTrigger(int keyCode){

this.key = keyCode;

}



@Override

public String getName() {

return "KeyCode " + key;

}

}

[/java]



And lastly.

[java]public class MenuLoader extends Game implements ActionListener {



private NiftyJmeDisplay niftyDisplay;

private Nifty nifty;



public MenuLoader() {}

public void loadMenu(Game app) {



niftyDisplay = new NiftyJmeDisplay(app.getAssetManager(),

app.getInputManager(),

app.getAudioRenderer(),

app.getGuiViewPort());

this.nifty = niftyDisplay.getNifty();

this.nifty.fromXml(“Interface/loadingScreen.xml”, “start”);

app.getGuiViewPort().addProcessor(niftyDisplay);



app.getInputManager().addMapping(“UpdateHint”, new GameTrigger(GameInput.KEY_HINT));

app.getInputManager().addMapping(“UpdateProg”, new GameTrigger(GameInput.KEY_PROG));

app.getInputManager().addListener(menuListener, “UpdateHint”, “UpdateProg”);



}



private ActionListener menuListener = new ActionListener(){



@Override

public void onAction(String name, boolean isPressed, float tpf) {

if (name.equals(“UpdateHint”) && !isPressed) {

updateHintText(name);

} else if (name.equals(“UpdateProg”) && !isPressed)

updateProBar(name.toString());

}



};



private void updateProBar(String add){

System.out.println(add);

}



private void updateHintText(String message){

System.out.print(message);

}



@Override

public void onAction(String name, boolean isPressed, float tpf) {

throw new UnsupportedOperationException(“Not supported yet.”);

}



}[/java]



Hopefully it’s just a little thing I forgot. Thanks! :slight_smile:

That doesn’t seem to work. Or rather, I can’t seem to get it to work.



Anyone could give me a pointer how to programmatically spawn/trigger a fake key with a message attached to it?

Hi,

i don’t understand why you are using the key event, if you don’t want the event to be triggered by the keyboard.



What are you trying to do exactly?

I want to setup a listener that will listen for events that I will trigger inside the game so that it can respond to it.



For example, I will eventually have a HUD display, and in there I’ll update the player with some information. Like, Race X attacked their base. That’s off the top of my head. But I want something with which I can dispatch messages.



As it is, I’ve set up a listener on a menu that I’ve made but I can’t fire up an event (like a fake keystroke for example) that would be caught by that listener. The listener works (afaik) but it doesn’t catch the fake stuff I send.



I want to know if I can take what’s in jME3 and tweak it (like extending some interfaces or classes). But I can’t seem to find out how.



I hope I’m making some sense. :confused:

It seems to me that you should just be using regular ol’ Java listeners to pull this off. My knowledge is a bit rusty on listeners, but I’m fairly certain they would treat you better in this case. The key input listeners you’re using are for just that, key input. The vibe of what I’m getting is that you want messages based off conditions.



There’s likely many more ways to do this as well, that just sprang to mind.



Cheers!

~FlaH

It makes sense, but you can create your own listener, since YOU are going to trigger the event.

You don’t have to distort another listener to make it fit your needs, do your own.



for example.

create a MyEventListener interface with an onEvent(String message) method.

Your hud (or some kind of hud controler) should implement this interface.

Of course in the onEvent method display the message or what ever on your hud.



Create some kind of EventBorker that have a list of MyEventListener. and of course register your HUD to the broker as a listener

Each time and event occur, notify the broker.

Then the broker just have to dispatch the event to it’s listener by iterating over them and calling the onEvent method.



This is game logic, and JME has no game logic implementation for now. So you’d better do your own classes for that kind of purpose.



hope that helps

The idea I had was to trigger an event into which a message would be attached for maximum efficiency. But yeah, you’re right. Using a message dispatcher would also be good.



At this point anything I could use would help I guess. :wink:



I can also confirm that both addMapping and addListener are indeed grabbing those new keys I’ve sent up.



But. The way I send those keys are by using:



[java]new KeyInputEvent(0xFF, ‘9’, false, false);[/java]

and that doesn’t seems to work. That’s the part that doesn’t seem to want to play nice.



As usual, it’s probably something I’m doing, or not doing, that is causing this to happen.

Drop the KeyEvent idea, this is not a key event, you are over complicating the problem.

Me? Overcomplicating anything? :stuck_out_tongue: I would never do such a thing! :wink: lol



Well, I just found a little something on the web about what I want to do, so I’ll try to make it work.



Wish me luck! :wink: