JavaFX:禁用使用键盘控制单选按钮

JavaFX: Disable controlling RadioButtons with keyboard

我正在开始使用 JavaFX,并且已经构建了一个小俄罗斯方块游戏。现在一切正常,但最后我决定将一组单选按钮添加到 select 难度 - 在这里我遇到了一个问题: 突然 LEFT/RIGHT/UP/DOWN 键只切换单选按钮,不再控制游戏。

为了处理游戏玩法,我在场景中添加了一个按键事件侦听器:

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() 将为您处理所有键码检查,包括检查常规箭头键和数字键盘箭头键。

我还建议您查看 this GitHub Gist which discusses using KeyEvents to prevent JavaFX from transferring focus when the Tab key is pressed and the JavaFX KeyCode documentation 以获得一些额外的想法和实施细节。

第一种方法:

你可以让你的 RadioButtons not focusable.

通过此更改,箭头键的默认键侦听器将不再更改收音机的状态,因为它们不会获得焦点(即使被鼠标选中):

classicModeButton.setFocusTraversable(false);
interestingModeButton.setFocusTraversable(false);

只有当您的 Scene 上没有其他可聚焦的 Nodes 时,这才会起作用 ,否则它们将在之前处理按键事件它可以由屏幕的事件处理程序处理。如果你有其他节点,请检查第二种方法。

示例片段:

// 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!"));

第二种方法:

您可以通过添加 event filter rather than an event handler to the Scene, and consume the event 来处理按键事件。这将捕获已经处于捕获阶段(而不是冒泡阶段)的事件,因此该事件甚至不会到达您的(可聚焦的)Nodes:

scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
    System.out.println((event.getCode().isArrowKey()) ? "Arrow pressed!" : "Other pressed, I don't care!");
    event.consume();
});

有了这个,所有的按键事件都会被捕获,当然也有可能"let some events"通过。

可以找到更多关于如何传递事件的信息(例如)here