JavaFX阻止树视图的本机键事件处理

时间:2013-12-24 12:28:03

标签: treeview javafx-2 keyevent

我想更改按键事件的处理,例如当用户按向上/向下箭头时执行其他操作但是当我通过setOnKeyPressed / setOnKeyReleased添加eventHandler时,我无法停止对这些键的本机处理。

示例:

treeView.setOnKeyPressed(new EventHandler<KeyEvent>() {
    private KeyCodeCombination prevNodeKeyCombination = new KeyCodeCombination(KeyCode.UP);
    private KeyCodeCombination nextNodeKeyCombination = new KeyCodeCombination(KeyCode.DOWN);

    public void handle(KeyEvent event)
    {
        if (prevNodeKeyCombination.match(event))
        {
            selectPrevSibling();
        }
        else if (nextNodeKeyCombination.match(event))
        {
            selectNextSibling();
        }

        event.consume(); // i try to block anything that goes by
    }
});

任何想法如何覆盖本机键处理,如果有必要,我可以尝试扩展TreeView,如果这有帮助吗?

1 个答案:

答案 0 :(得分:1)

要覆盖事件的本机处理,请在我的情况下使用addEventFilter:

treeView.addEventFilter(KeyEvent.ANY, new EventHandler<KeyEvent>() { ... }