Hello im using KeyBinding system for my events commands of keys in my states,
But now i need use a ANY KEY event, and cannot found a solution for a valid key constant,
What is the good way?
Your prompt could be like "Press ENTER to continue"?
really the work is any key, its impossible without setup ALL keys ?
well, its not that bad:
int[] allKeyCodes = new int[223];;
for(int i = 0; i < allKeyCodes.length; i++)
allKeyCodes[i] = i+1;
KeyBindingManager.getKeyBindingManager().set("ANY_KEY_EVENT", allKeyCodes);
something like that, and the array has to be filled only once (there are 223 keys defined in KeyInput)
nice one dhdd
Coool! good snippet!
UPDATE: Today i test it, without success, dont work.
I know this is old but it goes unanswered so here is my answer for anyone who stumbles on this topic create a RawInputListener and put your logic in the onKeyEvent
method.
here is some pseudo code…
public class MyRawInputAppState BaseAppState implements RawInputListener {
@Override
public void initialize(Application application) {
InputManager inputManager = application.getInputManager();
inputManager.addRawInputListener(this);
}
@Override
public void onEnable() { }
@Override
public void onDisable() { }
@Override
public void cleanup(Application application) {
InputManager inputManager = application.getInputManager();
inputManager.removeRawInputListener();
}
public void beginInput() { }
public void endInput() { }
public void onJoyAxisEvent(JoyAxisEvent event) { }
public void onJoyButtonEvent(JoyButtonEvent event) { }
public void onMouseMotionEvent(MouseMotionEvent event) { }
public void onMouseButtonEvent(MouseButtonEvent event) { }
public void onKeyEvent(KeyInputEvent event) {
if (event.isRepeating() || !isEnabled()) {
return;
}
if(event.isReleased()) {
//Do your action here
}
}
public void onTouchEvent(TouchEvent event) { }
}
the KeyInputEvent has methods getKeyChar()
and getKeyCode()
so you can get information about the exact key that was pressed. This could be useful if you were planning on letting your users remap you keys.
If I missed something pleas let me know
Sorry to bump this thread I just thought it should have an answer.
I’m currently looking for something similar. So thanks for digging out that one.
My question: How can I poll the state of, say, the SHIFT key (no events, but polling).
Another question: How can I find out the state of CAPS LOCK and NUM LOCK? (they are not up/down only, but also “activated” and “deactivated” (unlike other keys).
Thanks,
Capture the events and keep a flag that you poll.
Capture the events and keep a flag that you poll. Even the OS is doing this as it’s entirely possible for the little keyboard lights to be set 100% independently of caps lock or num lock actually being set.
That’s what I’m currently doing.
I had a memory of jME code asking key.isDown() - but that could have been jME 2 (it’s a pretty old memory).
Oh cool. I did not know that.
Kool I thought people would be mad I bumped a 7 year old topic. But I wanted to do some user driven input mapping.
For user driven input mapping, you might want to check out Lemur’s InputMapper class.
Here was a demo example:
It has a nicer separation of input and function than InputManager alone does. The latest (soon to be released) version even has the ability to query for all of the current mappings and such. It supports key comobs, treating key-pairs like an axis, etc…
Lemur’s github repo is here:
If you use maven or gradle to build then you can pick up the jar this way:
com.simsilica:lemur:1.6.1
InputMapper’s javadoc:
http://jmonkeyengine-contributions.github.io/Lemur/javadoc/Lemur/com/simsilica/lemur/input/InputMapper.html
There have even been requests to move this class into JME core someday.
I will check it out maybe barrow some code but my UI is full 3D as part of the rootNode so I don’t need the full library, but thank you.
Pspeed once told us that his InputMapper is separate from the UI part of Lemur.
You should be able to only use the input mapping together with your own UI.
Lemur is not very big. You can use just InputMapper if you like.
Lemur is also 3D, by the way. It has separate scene picking, etc. that works with any scene graph object. The GUI elements just combine the separate parts together. But the separate parts are usable on their own.
I all also release my device system soon and it includes the possibility to check a key state. It does NOT include trigger things, as the basic idea behind that is to shut down all the “event chain” mechanism. This mechanism is maybe great when it comes to “sleeping” software like Word (or editor, in general) but when you have a main loop where all game modification have to be done the chain event become a wall where you crash the car (the chain event) and check the blood (the boolean) during the main loop. It’s not very elegant and it involves a lot of redundant code and crossing reference (the event emitter references listener and listeners reference the emitter, to remove themselves from it when needed, leading to concurrent modification exception if done with java basic collections etc).
I’ll maybe release the device part as an independent library (so sooner than the rest). But the implementation is very easy anyway (you can imagine it: a map for “pressed” and a map for “pressedThisFrame”/“releasedThisFrame” (actually: number of change this frame. This plus the “pressed” let you answer the pressedThisFrame and releasedThisFrame).