How to make all buttons having ActionEvent handler handle Enter key in JavaFX? -


i have 50 buttons in application. buttons created handler way:

@fxml protected void handlefoobuttonactionevent(actionevent actionevent) {     ... } 

this way user can press buttons using mouse left button or space key. however, normal practice (as know) allow user press button using enter key. possible make buttons having actionevent handler (see above) handle enter key in javafx, example if have reference stage stage, scene scene or parent root?

you can configure scene it:

scene.getaccelerators().put(     keycombination.valueof("enter"), () -> {         node focusowner = scene.getfocusowner();         if (focusowner instanceof button) {             ((button) focusowner).fire();         }     }); 

you can event handler:

scene.addeventhandler(keyevent.key_pressed, e -> {     if (e.getcode() == keycode.enter) {         node focusowner = scene.getfocusowner();         if (focusowner instanceof button) {             ((button) focusowner).fire();         }     } }); 

normally i’d agree james_d changing standard behavior of buttons not idea, i’m finding gtk applications allow enter trigger button press, firefox you’ve mentioned.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -