JavaFX:禁用使用键盘控制单选按钮
我刚开始使用JavaFX,我已经开发了一个小俄罗斯方块游戏。现在一切都很顺利,但最后我决定添加一组单选按钮来选择难度-在这里我遇到了一个问题: 突然,左/右/上/下键只切换单选按钮,不再控制游戏 为了处理游戏性,我在场景中添加了一个按键事件监听器:JavaFX:禁用使用键盘控制单选按钮,java,javafx,radio-button,focus,Java,Javafx,Radio Button,Focus,我刚开始使用JavaFX,我已经开发了一个小俄罗斯方块游戏。现在一切都很顺利,但最后我决定添加一组单选按钮来选择难度-在这里我遇到了一个问题: 突然,左/右/上/下键只切换单选按钮,不再控制游戏 为了处理游戏性,我在场景中添加了一个按键事件监听器: public void setKeyEventHandler() { game.getScene().setOnKeyPressed(Field::handleKeyEvent); } private static void handleK
public void setKeyEventHandler() {
game.getScene().setOnKeyPressed(Field::handleKeyEvent);
}
private static void handleKeyEvent(KeyEvent event) {
// ... handle the event to move the figure
}
但正如我所说的,自从我添加了单选按钮后,这就不再执行了。
有没有办法禁用使用键盘键更改单选按钮,并使其仅可使用鼠标点击进行更改?
我是否需要以某种方式将焦点从他们身上移开?但是怎么做呢
编辑:
也许我应该补充一点,我只是使用setOnAction()
函数来处理单选按钮的事件,例如:
classicModeButton.setOnAction((e) -> interestingMode = false);
interestingModeButton.setOnAction((e) -> interestingMode = true);
我以前从未处理过这种情况,所以我在这里只是随便说说一个想法,但也许您可以为
javafx.scene.input.KeyEvent
实现一个处理程序
在您的处理程序中,您可能会说:
if(event.getCode().isNavigationKey()){
// Some navigation key was pressed
event.consume(); // prevent further propagation
yourGamesControlElement.requestFocus(); // transfer focus
handleKeyMovementInGame(event.getCode()); // handle the action
}
这种检测方法的一个好处是,isNavigationKey()
将为您处理所有键码检查,包括检查常规箭头键和数字键盘箭头键
我还建议查看哪些讨论使用KeyEvents来防止JavaFX在按下Tab键时转移焦点,以及其他一些想法和实现细节。第一种方法:
// Init the variables
BooleanProperty interestingMode = new SimpleBooleanProperty(false);
RadioButton classicModeButton = new RadioButton("Classic");
RadioButton interestingModeButton = new RadioButton("Interesting");
ToggleGroup tg = new ToggleGroup();
classicModeButton.setToggleGroup(tg);
interestingModeButton.setToggleGroup(tg);
tg.selectToggle(classicModeButton);
// The radios should be not focusable
classicModeButton.setFocusTraversable(false);
interestingModeButton.setFocusTraversable(false);
// On toggle-change, the mode will be changed
interestingMode.bind(tg.selectedToggleProperty().isEqualTo(interestingModeButton));
// Just print the changes
tg.selectedToggleProperty().addListener((observable, oldValue, newValue) ->
System.out.println((newValue == interestingModeButton) ? "Hmm, interesting" : "Classic .. boring"));
scene.setOnKeyPressed(e ->
System.out.println((e.getCode().isArrowKey()) ? "Arrow pressed!" : "Other pressed, I don't care!"));
您可以使用单选按钮
通过此更改,箭头键的默认键侦听器将不再更改收音机的状态,因为收音机将无法聚焦(即使被鼠标选中):
只有在场景
上没有其他可聚焦的节点
s的情况下,才会起作用,否则它们会在屏幕的事件处理程序处理关键事件之前处理关键事件。如果有其他节点,请检查第二种方法
示例片段:
// Init the variables
BooleanProperty interestingMode = new SimpleBooleanProperty(false);
RadioButton classicModeButton = new RadioButton("Classic");
RadioButton interestingModeButton = new RadioButton("Interesting");
ToggleGroup tg = new ToggleGroup();
classicModeButton.setToggleGroup(tg);
interestingModeButton.setToggleGroup(tg);
tg.selectToggle(classicModeButton);
// The radios should be not focusable
classicModeButton.setFocusTraversable(false);
interestingModeButton.setFocusTraversable(false);
// On toggle-change, the mode will be changed
interestingMode.bind(tg.selectedToggleProperty().isEqualTo(interestingModeButton));
// Just print the changes
tg.selectedToggleProperty().addListener((observable, oldValue, newValue) ->
System.out.println((newValue == interestingModeButton) ? "Hmm, interesting" : "Classic .. boring"));
scene.setOnKeyPressed(e ->
System.out.println((e.getCode().isArrowKey()) ? "Arrow pressed!" : "Other pressed, I don't care!"));
第二种方法:
// Init the variables
BooleanProperty interestingMode = new SimpleBooleanProperty(false);
RadioButton classicModeButton = new RadioButton("Classic");
RadioButton interestingModeButton = new RadioButton("Interesting");
ToggleGroup tg = new ToggleGroup();
classicModeButton.setToggleGroup(tg);
interestingModeButton.setToggleGroup(tg);
tg.selectToggle(classicModeButton);
// The radios should be not focusable
classicModeButton.setFocusTraversable(false);
interestingModeButton.setFocusTraversable(false);
// On toggle-change, the mode will be changed
interestingMode.bind(tg.selectedToggleProperty().isEqualTo(interestingModeButton));
// Just print the changes
tg.selectedToggleProperty().addListener((observable, oldValue, newValue) ->
System.out.println((newValue == interestingModeButton) ? "Hmm, interesting" : "Classic .. boring"));
scene.setOnKeyPressed(e ->
System.out.println((e.getCode().isArrowKey()) ? "Arrow pressed!" : "Other pressed, I don't care!"));
您可以通过向场景添加事件处理程序而不是事件处理程序来处理关键事件,然后。这将捕获已经处于捕获阶段(而不是冒泡阶段)的事件,因此事件甚至不会到达(可聚焦)节点
s:
scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
System.out.println((event.getCode().isArrowKey()) ? "Arrow pressed!" : "Other pressed, I don't care!");
event.consume();
});
通过这个按钮,所有按下的事件都被捕获,但当然也可以“让一些事件”通过
可以找到有关事件传递方式的更多信息(例如)。对于我来说,当微调器捕捉箭头时,按我不希望它捕捉的箭头键,执行xxxx.requestFocus()
对于其他操作,除了错误的微调器之外,场景中的组件帮助了我。它可能也适用于单选按钮。