Question

I want to increment value of keyevent.VK parameter using program. I tried below code which print one "r.keyPress(KeyEvent.VK_1);" upon execution of this code it prints 1. I want program should automatically increase the values using any loop. please help

Was it helpful?

Solution

In this case the reflection API may be useful:

for (int i = 0; i < 9; i++) {
        try {
            final Field field = KeyEvent.class.getDeclaredField("VK_" + i);
            r.keyPress((int) field.get(KeyEvent.class));

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top