Reputation: 1467
Whenever SPACE key is pressed, it is registered and processed as a last pressed (focused) button. I cannot capture it as KeyEvent, and it acts as a button.
I want to generate and pass on char from buttons and keyboard keys pressed.
Here is the handler part of my code in the controller class:
public void handleKeys(KeyEvent key) {
char chr = key.getText().charAt(0);
handleSymbols(chr);
key.consume();
}
public void handleBtns(ActionEvent buttonPressed) {
char chr = buttonPressed.getSource().toString().toLowerCase().charAt(buttonPressed.getSource().toString().length() - 2);
handleSymbols(chr);
buttonPressed.consume();
}
Here is the FXML file beginning:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="container" prefHeight="500.0" prefWidth="400.0" style="-fx-background-color: #00e600"
stylesheets="/sample/main.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller"
onKeyPressed="#handleKeys">
<left>
<VBox prefHeight="300.0" prefWidth="50.0">
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
<Button onAction="#handleBtns" prefHeight="135.0" prefWidth="50" text="+">
<VBox.margin>
<Insets bottom="20.0" />
</VBox.margin>
</Button>
<Button onAction="#handleBtns" prefHeight="135.0" prefWidth="50" text="-" />
</VBox>
</left>
There is something similar here, but for SWING, so it does not work:Disabling space bar triggering click for JButton
How can i unbind SPACE as a default key for focused buttons? I just don't want SPACE to act as a button.
Upvotes: 1
Views: 1611
Reputation: 1467
I added an eventFilter to the stage in the main class and it works
addEventFilter(KeyEvent.KEY_PRESSED, k -> {
if ( k.getCode() == KeyCode.SPACE){
k.consume();
}
});
Upvotes: 3