Bruce
Bruce

Reputation: 37

How to convert a KeyCode into a KeyValue

I am using JavaFX to recieve Key input using .setOnKeyReleased. The problem is that this gives me a keyCode, but to use the robot class (tried fx robot but it is "restricted") I need a keyValue int.

Is there anyway i can convert a KeyCode or (Fx)KeyEvent into a keyValue? Please help, can not figure this out. Thanks

Upvotes: 0

Views: 1029

Answers (1)

Roland
Roland

Reputation: 18415

You could try impl_getCode() of the KeyCode. But be aware, that this is internal and most probably will change with Java 9. From the KeyCode class:

/**
 * @treatAsPrivate implementation detail
 * @deprecated This is an internal API that is not intended for use and will be removed in the next version
 */
// SB-dependency: RT-22749 has been filed to track this
@Deprecated
public int impl_getCode() {
    return code;
}

Why this method isn't a proper one, is a mystery.

Use it e. g. like this:

scene.addEventFilter(KeyEvent.KEY_RELEASED, e -> {
    System.out.println(e.getCode().impl_getCode());
});

If everything fails with the Java implementation changes, you could always just create your own mapping of JavaFX key codes to awt key codes using the Java source.

Upvotes: 1

Related Questions