8 Replies Latest reply: Nov 27, 2012 9:12 AM by edward17 RSS

    Button behavior when Enter key pressed

    edward17
      I wanted to run this by the forum see if anyone else can confirm -
      On a PC when a button has focus, hitting the enter key executes the Button's onAction
      On a Mac hitting the enter key under the same conditions does nothing.

      Should the behavior be the same? Can others recreate the issue?

      I don't mind opening a ticket, just want to see if this is a 'feature' first
        • 1. Re: Button behavior when Enter key pressed
          edward17
          Any of you mac folk out there want to chime in?
          • 2. Re: Button behavior when Enter key pressed
            ytw
            You're correct.
            On a Mac hitting the enter key under the same conditions does nothing. However, hitting the spacebar will execute the Button's onAction.
            • 3. Re: Button behavior when Enter key pressed
              edward17
              is this a bug? In other frameworks (not JavaFX) does hitting the enter key while on a button fire the button action?
              • 4. Re: Button behavior when Enter key pressed
                973901
                I don't know whether or not that is a bug, but this workaround will provide the same functionality. I'm sure you already know this, but I'm gonna post it anyway for posterity.
                btn.setOnKeyPressed(new EventHandler<KeyEvent>()
                {
                     public void handle(KeyEvent evt)
                     {
                          if (evt.getCode() == KeyCode.ENTER)
                               btn.fire();
                     }
                });
                • 5. Re: Button behavior when Enter key pressed
                  edward17
                  yes, I was just wondering if I SHOULD add that based on expected user behavior on a MAC. I am a PC guying trying to cater to the MAC crowd in a respectful manner...
                  • 6. Re: Button behavior when Enter key pressed
                    973901
                    According to this article, enter (return) does the same thing on a Mac that it does on Windows =]

                    [http://lifehacker.com/5836288/six-keyboard-shortcuts-every-computer-user-should-know|http://lifehacker.com/5836288/six-keyboard-shortcuts-every-computer-user-should-know]
                    • 7. Re: Button behavior when Enter key pressed
                      jsmith
                      expected user behavior on a MAC
                      I got a Mac yesterday.

                      OS X 8 buttons are fired when they are focused and you press space on them - for JavaFX and system applications alike.

                      Default system focus behaviour is that only text boxes and lists gain focus - buttons don't focus at all. Enter fires the default button regardless of whether it is focused or not - for example OK for a message dialog or Open for a file dialog. Enter sometimes performs some other action (like making an icon name editable - similar to a slow double click in Windows). Esc does a cancel function - for example getting rid of a popup window which may or may not have a cancel button in it. Mnemonics like alt+f to open a file menu do not seem to work at all for the system applications like Safari - I didn't try it for JavaFX. To get to a menu item you can press ctrl+fn+F2, then navigate around with a keyboard by using arrow keys or by typing the first letter of a menu item, then press space to activate the menu to get a drop down, then navigate some more with the keyboard and finally press space to activate the menu item you want. Pretty much every menu item in Safari seems to have an accelerator combination of the command key+another key, so perhaps that is the way things are done in Mac land. None of the standard system applications seem to have any mnemonics at all applied to buttons.

                      If you go to System Preferences | Keyboard | Keyboard Shortcuts there is an option which says Full Keyboard Access: All Controls. This button can also be toggled anytime in the system using the ctrl+fn+f7 combination. After the option is enabled, you can tab to a button to focus it. JavaFX does not respect this setting - you can tab to buttons and other non list and textfield controls in a JavaFX application at anytime, regardless of the Full Keyboard Access value.
                      • 8. Re: Button behavior when Enter key pressed
                        edward17
                        I confirmed in the MAC UI Guidelines, and you are correct.


                        The REAL issue was that I did not define default buttons on each screen, which inherit the proper 'Enter' behavior


                        http://docs.oracle.com/javafx/2/api/javafx/scene/control/Button.html