mardi 23 octobre 2018

How to get a field using a String and obtaining the value of said field?

So I've got a game that requires I get the value inputted as text into the console, and perform an action with a org.lwjgl.input.Keyboard field value that it corresponds to. For example, if the user types in 'F', I get the value of Keyboard.Key_F and work from there. The problem is that this approach isn't working.

Here's the current code that I have:

public void getKey(String input) throws NoSuchFieldException, IllegalAccessException {
    if (input != null) {
        Class<Keyboard> keys = Keyboard.class;
        Field key = keys.getDeclaredField("Key_" + input.toUpperCase());
        performAction(key.getInt(null));
}

private void performAction(int keyCode) {
    // do something
}

This is not correctly working, it's not recieving a value that can be used in performAction, and I'm unsure how to proceed. I'd appreciate any help given.





Aucun commentaire:

Enregistrer un commentaire