I'm trying to make a method of converting a string, such as "HOME" or "BACKSPACE", into it's corresponding keyCode. I know that there has to be some way to do this other than using a series of IF statements or a switch of some sort. I've done research and haven't been able to find anything that is really what I'm looking for. Does anyone know how this can be accomplished?

Anyway, you can get all the values for chars by using the other method I said.All you need to do is work out whether it's a string or a char, then use the appropriate method. It's not clean, but it (should) work.

// Get all of the fields in KeyEventField[] fields = KeyEvent.class.getFields();

for (inti = 0; i < fields.length; i++) {

StringfieldName = fields[i].getName();

// We only care about the field names corresponding to key codesif (fieldName.startsWith("VK")) {// Use reflection to get the actual key code valueintkeyCode = fields[i].getInt(null);StringkeyName = fieldName.substring(3);

// Add the code and name values to the mapskeyCodesToNames.put(keyCode, keyName);keyNamesToCodes.put(keyName,keyCode); } }} catch (Exceptione) {e.printStackTrace();}

// Try it out System.out.println(keyCodesToNames.get(KeyEvent.VK_5));System.out.println(keyCodesToNames.get(KeyEvent.VK_ESCAPE));System.out.println(keyNamesToCodes.get("ESCAPE"));

Bad advice. Key chars and key codes are completely separate concepts. The same virtual key can create many different possible chars or none at all. Setting one variable probably does not have an effect on the others. KeyEvent objects just hold those values. They are calculated somewhere else.

The map advice, on the other hand, is perfectly fine.

@cubemaster21

The answer depends on your level of experience.

Pro: Do you want the Java constant names? (Which could be read from a file or obtained using reflection.) Or a locale specific String? (Which can be solved by iterating through every possible keycode and possibly caching the result of

getKeyText(i)

in a map. Or reading from a file, which would not force you to use one to one mappings. "Meta", "Super", "Windows", and "Apple" all refer to the same key, but getKeyText will only return one.)

Where do the Strings come from and what are they meant to be used for?

Noob: You are not supposed to use key codes and Strings that way. A key code is more valuable than a String. I would avoid using Strings. They aren't much help for programming logic. Mainly just user interfaces like key binding editors, if even that, where you could show the user which key they pressed. If you have a good reason for wanting Strings, then explain what that is because you were too vague. There are uses to justify such a reverse look up, but they would not necessarily use the same mapping that getKeyText would. If it were actually more helpful to use Strings than ints, then the String values you use depend on your application.

Edit: Too slow to post. Looking at your code, you are definitely a noob. (Magic constants are a dead give away.) The person you copied from is probably not much more experienced. It's pretty bad.

I doubt a

getKeyCode(String)

function will help in the long run. It's better to use key codes. If you did need the function, you might call it once for each string used at the start of your program. You better explain your scenario.

Well, I've been trying to make my own library that I can use with my own games and make things a lot simpler. I figured that adding a keyboard would be a good idea. I'm using strings because when I do my update logic for the game, it would look something like this:

1 2 3 4

publicvoidtick(intdelta){if(Keyboard.isKeyDown("SPACE")jump();}

I didn't want to have to deal with KeyEvent. This altogether is starting to sound like a bad idea on my part....

This way if you change your jump key from space to up arrow (or the player changes it), only the keymap changes. You don't have to change it every where in your code. Now the above code isn't perfect as Jump is only defined by one key (For instance "JUMP RIGHT" might require both space and right arrow) but it should help.

Also, you should probably use an Enum instead of string names. They are faster and the compiler will flag errors if you accidentaly use JUPM instead of JUMP.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org