I am trying to replicate a “Push to talk” style button system in JavaFX. When you hold down a key on the stage, the button is activated and deactivated when you let go. As you can see from my example below, when you hold down the button and let go it prints out the correct action.
StackPane stackPane = new StackPane();
Button button = new Button("Push To Talk");
stackPane.getChildren().add(button);
Scene scene = new Scene(stackPane, 400, 200);
stage.setScene(scene);
stage.show();
scene.setOnKeyPressed(e -> {
String codeString = e.getCode().toString();
if(codeString.equals("A")) {
button.arm();
};
});
scene.setOnKeyReleased(e -> {
String codeString = e.getCode().toString();
if(codeString.equals("A")) {
button.disarm();
}
});
button.setOnMousePressed(e -> {
System.out.println("Push to Talk Activated!!");
});
button.setOnMouseReleased(e -> {
System.out.println("Push to Talk Deactivated!!");
});
However when I press the A key, the button graphic is being activated and deactivated but the button functions are not being called. What am i doing wrong?