比较 setOnAction 事件和 Keycode

Compare setOnAction event with Keycode

有没有办法将 setOnAction 事件与键码(如向上和向下箭头)进行比较?

这是我的样本:

 comboBox.setOnAction(e ->{
//compare event key with KeyCode up or down
                if(e.getSource().equals(KeyCode.DOWN)) {
                    // do stuff
                } else {
                comboBox.hide();
                // do some other stuff
                }
            });

试试这个

comboBox.setOnKeyPressed(ke->{
    if(ke.getCode().equals(KeyCode.DOWN)){
        // do stuff
    }
    else{
        comboBox.hide();
        // do some other stuff
    }
})