对于您的用例,我认为您实际上想使用加速器而不是助记符。
button.getScene().getAccelerators().put(
new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN),
new Runnable() {
@Override public void run() {
button.fire();
}
}
);
在大多数情况下,建议您使用 KeyCombination.SHORTCUT_DOWN 作为修饰符说明符,如上面的代码所示。对此的一个很好的解释是组合键文档:
快捷键修饰符用于表示修饰键,即
常用于主机平台上的键盘快捷键。这是为了
Windows 上的示例控件和 Mac 上的元(命令键)。通过使用
快捷键修饰符开发者可以创建平台无关的
快捷方式。所以“快捷键+C”组合键是在内部处理的
在 Windows 上为“Ctrl+C”,在 Mac 上为“Meta+C”。
如果您想专门编写代码来仅处理 Ctrl+S 组合键,您可以使用:
new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN)
这是一个可执行的示例:
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class SaveMe extends Application {
@Override public void start(final Stage stage) throws Exception {
final Label response = new Label();
final ImageView imageView = new ImageView(
new Image("http://icons.iconarchive.com/icons/gianni-polito/colobrush/128/software-emule-icon.png")
);
final Button button = new Button("Save Me", imageView);
button.setStyle("-fx-base: burlywood;");
button.setContentDisplay(ContentDisplay.TOP);
displayFlashMessageOnAction(button, response, "You have been saved!");
layoutScene(button, response, stage);
stage.show();
setSaveAccelerator(button);
}
// sets the save accelerator for a button to the Ctrl+S key combination.
private void setSaveAccelerator(final Button button) {
Scene scene = button.getScene();
if (scene == null) {
throw new IllegalArgumentException("setSaveAccelerator must be called when a button is attached to a scene");
}
scene.getAccelerators().put(
new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN),
new Runnable() {
@Override public void run() {
fireButton(button);
}
}
);
}
// fires a button from code, providing visual feedback that the button is firing.
private void fireButton(final Button button) {
button.arm();
PauseTransition pt = new PauseTransition(Duration.millis(300));
pt.setOnFinished(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
button.fire();
button.disarm();
}
});
pt.play();
}
// displays a temporary message in a label when a button is pressed,
// and gradually fades the label away after the message has been displayed.
private void displayFlashMessageOnAction(final Button button, final Label label, final String message) {
final FadeTransition ft = new FadeTransition(Duration.seconds(3), label);
ft.setInterpolator(Interpolator.EASE_BOTH);
ft.setFromValue(1);
ft.setToValue(0);
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
label.setText(message);
label.setStyle("-fx-text-fill: forestgreen;");
ft.playFromStart();
}
});
}
private void layoutScene(final Button button, final Label response, final Stage stage) {
final VBox layout = new VBox(10);
layout.setPrefWidth(300);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(button, response);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 20; -fx-font-size: 20;");
stage.setScene(new Scene(layout));
}
public static void main(String[] args) { launch(args); }
}
// icon license: (creative commons with attribution) http://creativecommons.org/licenses/by-nc-nd/3.0/
// icon artist attribution page: (eponas-deeway) http://eponas-deeway.deviantart.com/gallery/#/d1s7uih
示例输出:
2020 年 1 月更新,对多个控件使用相同的加速器
当前和以前的实现(JavaFX 13 及更早版本)中的加速器的一个警告是,您无法开箱即用地定义相同的加速键组合以在单个应用程序中的多个菜单或控件上使用。
欲了解更多信息,请参阅:
- JavaFX ContextMenu 加速器从错误的选项卡触发
- 以及相关的JDK-8088068 问题报告.
链接的问题报告包括一个解决方法,您可以使用它来允许您在应用程序的多个位置定义和使用相同的加速器(例如,在不同上下文菜单中的两个不同菜单项上)。
请注意,这仅适用于尝试在应用程序内的多个位置使用相同的加速器,如果您不需要尝试这样做,那么您可以忽略此信息。