你只需要注册自己的
EventFilter
上
Scene
。这将允许您收听所需的键盘组合并做出相应反应。
在下面的示例应用程序中,您将传递所需的
Tab
和
KeyCombination
涉及一种处理注册的方法
事件筛选器
.
在这个例子中,我配置了以下快捷方式
CTRL+1
,
CTRL+2
,以及
CTRL+3
选择
选项卡
分别为1、2或3。
请注意,您也可以使用
KeyCombination.CONTROL_DOWN
而不是
KeyCombination.SHORTCUT_DOWN
,但使用
SHORTCUT_DOWN
因为它是独立于平台的。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TabPaneShortcuts extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// **********************************************************************************************
// Create a basic layout
// **********************************************************************************************
VBox root = new VBox(5);
root.setAlignment(Pos.TOP_CENTER);
root.setPadding(new Insets(10));
// **********************************************************************************************
// Create a TabPane
// **********************************************************************************************
TabPane tabPane = new TabPane();
// **********************************************************************************************
// Create the Tabs
// **********************************************************************************************
Tab tab1 = new Tab("Labor");
Tab tab2 = new Tab("Parts");
Tab tab3 = new Tab("Tax");
tabPane.getTabs().addAll(tab1, tab2, tab3);
// **********************************************************************************************
// Add the TabPane to our root layout
// **********************************************************************************************
root.getChildren().add(tabPane);
// **********************************************************************************************
// Set the Scene for the stage
// **********************************************************************************************
Scene scene = new Scene(root);
primaryStage.setScene(scene);
// **********************************************************************************************
// Register keyboard shortcuts to select Tabs with the keyboard. Here, we create the KeyCodeCombination
// to listen for CTRL + 1,2, or 3.
// **********************************************************************************************
registerShortcut(tabPane, tab1, scene,
new KeyCodeCombination(KeyCode.DIGIT1,
KeyCombination.SHORTCUT_DOWN));
registerShortcut(tabPane, tab2, scene,
new KeyCodeCombination(KeyCode.DIGIT2,
KeyCombination.SHORTCUT_DOWN));
registerShortcut(tabPane, tab3, scene,
new KeyCodeCombination(KeyCode.DIGIT3,
KeyCombination.SHORTCUT_DOWN));
// **********************************************************************************************
// Configure the Stage
// **********************************************************************************************
primaryStage.setWidth(300);
primaryStage.setHeight(200);
primaryStage.setTitle("Test Application");
primaryStage.show();
}
/**
* Registers the given KeyCombination to automatically select the given Tab of the TabPane
*
* @param tabPane The TabPane whose selection model we need to manipulate
* @param tab The Tab to be selected when the given KeyCombination is detected
* @param scene The main Scene on which to register this EventFilter
* @param combination The KeyCombination to listen for
*/
private void registerShortcut(TabPane tabPane, Tab tab, Scene scene, KeyCombination combination) {
// **********************************************************************************************
// Add an EventFilter to the Scene to listen for the given KeyCombination
// **********************************************************************************************
scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
// **********************************************************************************************
// If the event matches the KeyCombination passed to this method, select the desired Tab
// **********************************************************************************************
if (combination.match(event)) {
tabPane.getSelectionModel().select(tab);
}
});
}
}
您的问题特别提到使用
ALT+[letter]
对于组合,这也可以很容易地完成:
registerShortcut(tabPane, tab1, scene,
new KeyCodeCombination(KeyCode.L,
KeyCombination.ALT_DOWN));
我选择了一起去
CTRL
仅仅因为使用
ALT
导致我的Windows 10系统每次都“叮”,我懒得弄清楚原因。:)