Handling of return key in dialogs



Imagine you have a dialog with a few buttons in the content, but also an OK and Cancel button at the bottom. Usually you can confirm such dialogs with the return key.

But how does one trigger the other buttons using the keyboard? On Windows the convention is to press space bar to ‘click’ the button and return to confirm the dialog. Juce buttons however by default call their click handler on pressing return.

Now we have 2 conflicting uses in dialogs. Pressing return will at once both trigger the button in focus and confirm the dialog.

This is reproducible in the demo runner: Go to the dialogs demo, click alert window with extra components and put the keyboard focus on the dropdown. Press return. The dropdown will open, but the OK button will also go to the “pressed” state. Now press Tab. The dialog will close.

So, how does one implement a dialog window which can be used with the keyboard?


probably add a keyListener, and cycle through the list of child components, giving each one focus when you press tab…

see the focus section of the Component documentation:


No. Focus cycling is working fine, and the entire point of the doc you linked is that you don’t have to add key listeners yourself to implement focus handling.

The problem is with handling the return key press, not with focus.


The problem is the different levels of keyboard input handlers. We have keyPressed which is in the example above properly ‘consumed’ by the combo box. However, button shortcuts (return in this case) are handled in keyStateChanged which is called earlier. Standard widgets like ComboBox don’t handle keyStateChanged.

Did anyone else come up with a solution before? Or should we just accept dialogs can be used with the mouse only?