Paul Taylor
Paul Taylor

Reputation: 13200

With newer version of Java some of my applications shortcuts are being overridden by MacOS shortcuts

My application used Java 8 and on MacOS many actions have shortcuts defined that work fine, such as this one defined to use COMMAND-1

public final class AutoCorrectAction
    extends CorrecterAction
{
    private static final String ACTION_NAME = "autocorrect";
    

    public AutoCorrectAction(final int paneIndex)
    {
        super(ACTION_NAME, TextLabel.MENU_AUTOCORRECT.getMsg(), paneIndex);
        if (paneIndex == TagDisplayer.INDEX_NONE)
        {
            putValue(ACCELERATOR_KEY,(KeyStroke.getKeyStroke(KeyEvent.VK_1,
                                          Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())));

        }
     
        this.putValue(Action.SMALL_ICON, JaikozIcon.LOCAL_AUTOCORRECT.getIconSmall());
        this.putValue(JaikozAction.LARGE_ICON, JaikozIcon.LOCAL_AUTOCORRECT.getIconLarge());
        this.putValue(Action.SHORT_DESCRIPTION,TextLabel.MENU_AUTOCORRECTTOOLTIP.getMsg());

    }

    public final void actionPerformed(final ActionEvent e)
    {
        start.logger.entering(this.getClass().getName(), JaikozLogFormatter.ACTION_PERFORMED);      
        this.performTask(new AutoCorrecter(start, isSelectedOnly(e), isUseRowSelection));
        start.logger.exiting(this.getClass().getName(), JaikozLogFormatter.ACTION_PERFORMED);
    }
}

My latest version of software now uses Java 11, and now when i press COMMAND-1 it minimizes the windows and shows in Finder, a google search determined this is a standard mac shortcut - https://support.apple.com/en-us/HT201236

Command-1: View the items in the Finder window as icons.

So my question is what is the correct mac behaviour, should my shortcut in my application override the MacOS shortcut or not, i.e is this a bug introduced in Java 11 or was the previous behaviour buggy and now fixed.

Confusingly, I have another action defined in the same way that uses Command-2, this is also a standard MacOS shortcut

Command-2: View the items in a Finder window as a list.

public final class CreateAcousticIdAction
    extends CorrecterAction
{
    private static final String ACTION_NAME = "createmusicipacousticid";


    public CreateAcousticIdAction(final int paneIndex)
    {
        super(CreateAcousticIdAction.ACTION_NAME, TextLabel.MENU_CREATE_MUSICIP_ACOUSTIC_ID.getMsg(), paneIndex);
        this.putValue(Action.SMALL_ICON, JaikozIcon.CREATE_MUSICIP_PUID.getIconSmall());
        this.putValue(JaikozAction.LARGE_ICON, JaikozIcon.CREATE_MUSICIP_PUID.getIconLarge());

        this.putValue(Action.SHORT_DESCRIPTION, TextLabel.MENU_CREATE_MUSICIP_ACOUSTIC_IDTOOLTIP.getMsg());
        putValue(ACCELERATOR_KEY,(KeyStroke.getKeyStroke(KeyEvent.VK_2,
                                          Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())));
    }

    public final void actionPerformed(final ActionEvent e)
    {
        this.performTask(new CreateAcoustId(start,  isSelectedOnly(e), isUseRowSelection), JaikozThreadGroup.THREAD_CREATE_ACOUSTICID);
    }
}

yet my shortcut continues to work and is not overridden by MacOS shortcuts !

Update I have created movie as requested - http://www.jthink.net/jaikoz/scratch/cmd1issue.mov

What I now notice is that when I press Cmd-1 it minimizes the window, but its run autocorrect as well so it does both things.

Upvotes: 2

Views: 178

Answers (3)

Paul Taylor
Paul Taylor

Reputation: 13200

enter image description hereFound the issue, I didnt realize that I was also using Command-1 for maxmizing windows within the application as well, removing this shortcut resolved the issue

Upvotes: 1

Patrick A
Patrick A

Reputation: 1

disclaimer: I don't have much experience with MacOS or creating OS macros from java that interact with non-third party apps (generally against the goal of java which is meant to be platform independent).

Answer: The macro hierarchy would generally be determined by the OS and be affected by the java compiler/jvm implementation. Ideally a third party and likely untrusted java program (which is generally the case for developers) should be last in priority to receive the key macro thus any programs that use macros should avoid any likely pre-existing macros or ensure that they have a greater focus than the OS/other programs to ensure they receive all input before them.

Theoretical Implementation: Two main ways I can think of to achieve this are to create a configure file/program to allow the user to define what key-macro to access the program (which makes it always possible to avoid conflicts regardless of OS) or to have a wake up macro that is unlikely to be by other programs (mac equiv of ctr-shift-k) which causes a program to gain focus over the OS and then the program can call as many key macros as it wants.

Note: regarding why it happened, I would guess the java code previously required extra privileges to run (which makes it more dangerous if an exploit is found) thus as a side effect it was given higher priority by the OS in other aspects too. Thus when the privilege level dropped after it was fixed (with all the other security features in java 11) its position in the key-macro hierarchy would have dropped too.

Upvotes: 0

Glenn
Glenn

Reputation: 881

So my question is what is the correct mac behaviour, should my shortcut in my application override the MacOS shortcut or not, i.e is this a bug introduced in Java 11 or was the previous behaviour buggy and now fixed.

Not that it helps, but...I think the question itself is flawed, at least for this issue.

Your google search was correct that command-1 in Finder lists the contents of a window as icons. But the key there is in Finder. That behaviour does not affect other applications; it's specific to Finder. Said differently, it's a Finder shortcut, not a macOS shortcut.

There are some system-wide ("macOS") shortcuts, like for capturing the image of a screen (command-shift-3 et al), or for bringing up Spotlight (command-space). But command-1 is not one of those, nor is command-2.

So your original question, of which shortcut (app or OS) should override the other, is moot for command-1 and command-2.

A quick search did not reveal Mac-specific release notes for JDK11 LTR, but I suspect the issue is indeed part of some change in JDK9, 10, or 11. I do not know how to work around that, but if you have Mac-specific release notes, they may provide a hint.

To answer the original question in general -- not related to this specific issue -- if memory serves, the macOS shortcut takes precedence...but there's also a way around that. Apple > System Preferences... > Keyboard > Shortcuts lets you change an existing system-wide shortcut (though generally I would advise against that), or -- better yet -- add one that you think an app should have.

The catches with that last are (1) it has to be an executable app (so not a .jar), and (2) it must be a standard GUI app where the desired action exists as a menu item, not a UNIX CLI executable.

Upvotes: 2

Related Questions