From bf8cc268cc17575d0796b7931384aea8faeed3a8 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 17 Dec 2019 11:35:16 +0100 Subject: [PATCH] on Mac show mnemonics only when `Ctrl` and `Alt` keys are pressed (issue #4) --- CHANGELOG.md | 2 ++ .../java/com/formdev/flatlaf/FlatLaf.java | 26 ++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe2b4172..e4bd9dcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ FlatLaf Change Log pressing focused button with Enter key (as in Windows LaF). - Fixed clipped borders at 125%, 150% and 175% scaling when outer focus width is zero (default in "Flat Light" and "Flat Dark" themes). +- On Mac show mnemonics only when Ctrl and Alt keys are + pressed. (issue #4) ## 0.21 diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java index 92d62dbe..b1c99104 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -61,7 +61,7 @@ public abstract class FlatLaf private PropertyChangeListener desktopPropertyListener; private KeyEventPostProcessor mnemonicListener; - private static boolean altKeyPressed; + private static boolean showMnemonics; private Consumer postInitialization; @@ -113,8 +113,7 @@ public abstract class FlatLaf // add mnemonic listener mnemonicListener = e -> { - if( e.getKeyCode() == KeyEvent.VK_ALT ) - altKeyChanged( e.getID() == KeyEvent.KEY_PRESSED ); + checkShowMnemonics( e ); return false; }; KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor( mnemonicListener ); @@ -357,14 +356,27 @@ public abstract class FlatLaf } public static boolean isShowMnemonics() { - return altKeyPressed || !UIManager.getBoolean( "Component.hideMnemonics" ); + return showMnemonics || !UIManager.getBoolean( "Component.hideMnemonics" ); } - private static void altKeyChanged( boolean pressed ) { - if( pressed == altKeyPressed ) + private static void checkShowMnemonics( KeyEvent e ) { + int keyCode = e.getKeyCode(); + if( SystemInfo.IS_MAC ) { + // Ctrl+Alt keys must be pressed on Mac + if( keyCode == KeyEvent.VK_CONTROL || keyCode == KeyEvent.VK_ALT ) + showMnemonics( e.getID() == KeyEvent.KEY_PRESSED && e.isControlDown() && e.isAltDown() ); + } else { + // Alt key must be pressed on Windows and Linux + if( keyCode == KeyEvent.VK_ALT ) + showMnemonics( e.getID() == KeyEvent.KEY_PRESSED ); + } + } + + private static void showMnemonics( boolean show ) { + if( show == showMnemonics ) return; - altKeyPressed = pressed; + showMnemonics = show; // check whether it is necessary to repaint if( !UIManager.getBoolean( "Component.hideMnemonics" ) )