I'm not sure if you are able to test this yourself, but I made a fix for this problem.

You could potentially test it by copying the class `javafx.scene.Scene` in your project (without changing the package -- and if modules will allow it, I don't use them personally) and then using this piece of code:

    private void setWindowFocused(boolean value) {
        windowFocused = value;

        Node node = getFocusOwner();
        if (node != null) {
            node.setFocusQuietly(windowFocused, focusOwner.focusVisible);
            node.notifyFocusListeners();
        }

        if (windowFocused && accessible != null) {
accessible.sendNotification(AccessibleAttribute.FOCUS_NODE);
        }

        if (!windowFocused) {
getInternalEventDispatcher().getKeyboardShortcutsHandler().setMnemonicsDisplayEnabled(false);
        }
    }

The last three lines are what I added. A quick test on Windows here shows that the mnemonics get disabled as soon as the window loses focus, and when returning, they're not responding as you'd expect.

I'll submit a PR as well.

--John

On 12/02/2023 15:52, Pedro Duque Vieira wrote:

    The behavior on Windows is all over the place for different
    applications.? I tested a few I've got running:


    Notepad, Notepad++, Eclipse:


    - Alt-down: Shows mnemonics on menu bar
    - Alt-up: Highlights file menu on alt release
    - Alt-tab: Shows mnemonics but doesn't highlight menu when it loses
    focus; when returning, mnemonics still highlighted, but doesn't act on
    them as menu not selected


    -> Looks buggy


    Thunderbird / Opera / Firefox:


    - Alt-down: nothing
    - Alt-up: shows menu bar (it is hidden normally)
    - Alt-tab: works as expected, no highlighting


    -> Looks well behaved


    Explorer / Excel / Wordpad:


    - Alt-down: nothing
    - Alt-up: shows mnemonics
    - Alt-tab: works as expected, no highlighting


    -> Looks well behaved


    Visual Studio Code:


    - Alt-down: Shows mnemonics on menu bar
    - Alt-up: Highlights file menu on alt release
    - Alt-tab: Shows mnemonics, but hides them once it loses focus; on
    return doesn't show mnemonics


    -> Looks well behaved


    Chrome / IntelliJ:


    -> Looks well behaved, doesn't react to alt presses in any way


    None of the applications tested reacted on a mnemonic key after
    regaining focus however, even though they may have them still
    highlighted (which I think is a bug).


    In my opinion, the behavior of notepad/notepad++/eclipse is incorrect
    (they need to hide the mnemonics on focus lost, like Visual Studio
    Code
    does, but they don't).


    There seems to be two correct ways of handling mnemonics in
    applications
    that use them, either:


    a) shows mnemonics immediately on alt-down, but hide them on focus
    lost
    (if the alt-down becomes an alt-tab, or probably any other alt
    combination)


    b) only show mnemonics on a naked alt-up


    Ticket JDK-8090647 mentions a spec that has been updated, but I can't
    find it.? It also mentions that the behavior for JavaFX should be
    what I
    described in a), so I think this is a bug that can simply be fixed.


    --John



Yap, there's quite different behaviors across apps. If you test on windows 11 you'll get yet another set of different behaviors.

But all in all, if you: alt+tab (without release) and alt+tab again so your app regains focus, you'll have the mnemonic still activated, which, as you say, sounds like a bug no matter the difference in behaviors across apps.

I agree with your suggestion: "a".

My client was quite disappointed and started to rant about javafx. It didn't help that we were hit by a couple other bugs (perhaps bad luck).  The fact that the bug was filed on 2013 (10 years later) and is still happening can be quite problematic. Perhaps it would make sense to review all bugs filed, giving highest priority to the bugs that were submitted longer ago?

Thanks!

--
Pedro Duque Vieira - https://www.pixelduke.com

Reply via email to