public void start(Stage primaryStage) throws Exception {
Label l = new Label();
Pane p = new Pane(l);
Scene s = new Scene(p, 200, 200);
KeyCodeCombination kComb = new KeyCodeCombination(KeyCode.A, KeyCombination.ALT_ANY);
s.setOnKeyPressed(e -> {
if (kComb.match(e))
l.setText("Hello");
});
primaryStage.setScene(s);
primaryStage.show();
}
试试看
我自己补充一下:
javafx.scene.input 包有一个方便的 KeyCodeCombination 类,其构造函数接受键和修饰符的组合作为属性。这个类实现了 match(KeyEvent e) 方法
使用示例: