Help on getting the KeyInput KeyCode when you have the key's name

Hello, names David. I became interested in JMonkey about a year ago but was discouraged and abandoned it after it was a pain to get working right with maven. JMonkey3 was still a bit of a pain (HOPEFULLY one of these days the JMonkey Engine can get into come central repository) but after I tagged all the dependencies with a custom version number and uploaded them to my private Nexus repository, everything works easy now the way a maven project should ^_^. SO I am back interested in developing with JMonkey Now.

Currently I am running JME3 Alpha2

Here is the first main issue I ran into. I have a config file that maps actions to keyboard keys. It has many lines looking like “jump:KEY_J”. The issue is I need to map that key name (‘KEY_J’) to the right KeyInput KeyCode (so I can then loop through and call all the appropriate inputManager.addMapping(…) calls). APPARENTLY in JMonkey2 there is a method on KeyInput that does EXACTLY this called getKeyIndex.(jmonkey javadoc) I do not see this functionality in JMonkey3. Is there another way to do this or is that method now somewhere else? Thank you for any help.


You can use reflection? Just look up the field with that name, then fetch the constant value.

eww… that’s kinda ugly, not to mention an exception handling nightmare. The config file is basically user input; using that for reflection seems dangerous as they could put anything in there. I will think it over, consult my Effective Java book to see if that’s viable, and consult with my colleagues and get back to you. But at that point I would almost rather do things right and create my own enum with my own set of keys mapping the JMonkey key codes with the name, and just use those everywhere instead of the provided JMokey ones. That just feels like a lot of code duplicating functionality in JMonkey though.

Is that the only way to do it? Are there any plans for this method to be brought over from JMonkey2? (I know JME3 is still in alpha and I expect certain functionality not complete yet and am willing to wait on it). If not is there a reason? It just seems like essential functionality to me and I can see many cases where it is needed .

Thanks for the quick response though,


Have you ever looked at how jME2 does it? It uses reflection as well, if the given name doesn’t match any of the fields, the exception thrown just says you provided an incorrect string.

It is simple and effective, and does not require maintaining a hash table and what not.

Eh, no I did not know that JME2 used reflection. Using reflection in that way just goes against the the normal java design I am used to, I just hadn’t considered it. I really should have check though, sorry

Well There are really only two options I guess. Implement my own functionality with an enum (which with an enum it doesn’t require maintaining a hash table, as yes that would be ugly) and basically re-implement part of the functionality IN JMonkey. (I am sure there are good reasons why it wasn’t implemented that way IN Jmonkey3, like being java1.4.2 comparable or something of the like. I always hating those kind of requirements lol). OR to do it as you said, via reflection. I guess if I put it in a separate JMonkey Input utility class, and build up a hash statically on the startup of the program, it wont be too bad. I will probably take that route.

Thanks for the input and the fast replies man. Look forward to working with the JMonkey library with my team, and I am excited about some of the cool things it looks like I is able to do.