The typical use case when the developer assigns an event handler for the F1 key to show help in some way.
However, if the TextField has focus, it will consume all function key events without doing anything.
If it actually does nothing with function keys, I suggest adding a simple if statement to avoid that.
The following sample application demonstrates the issue. Try pressing/releasing the F1 key while the TextField has focus; nothing happens.
Let the button has focus and try pressing/releasing F1 key again. It works!
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextFieldConsumesFunctionKeyEvents extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
final TextField textField = new TextField();
final Label label = new Label();
primaryStage.setScene(new Scene(new VBox(textField, new Button("Button"), label)));
primaryStage.getScene().getAccelerators().put(KeyCombination.valueOf("F1"), () -> label.setText("Help!"));
primaryStage.setTitle("TextField Consumes Function KeyEvents");
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}
However, if the TextField has focus, it will consume all function key events without doing anything.
If it actually does nothing with function keys, I suggest adding a simple if statement to avoid that.
The following sample application demonstrates the issue. Try pressing/releasing the F1 key while the TextField has focus; nothing happens.
Let the button has focus and try pressing/releasing F1 key again. It works!
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextFieldConsumesFunctionKeyEvents extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
final TextField textField = new TextField();
final Label label = new Label();
primaryStage.setScene(new Scene(new VBox(textField, new Button("Button"), label)));
primaryStage.getScene().getAccelerators().put(KeyCombination.valueOf("F1"), () -> label.setText("Help!"));
primaryStage.setTitle("TextField Consumes Function KeyEvents");
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}
- duplicates
-
JDK-8089884 TextInputControls capturing function key events
- Resolved