From 4e8befb43a8af4bf7ca2983539a14b72d88807a9 Mon Sep 17 00:00:00 2001 From: Sergey Malenkov Date: Thu, 28 Mar 2019 18:27:57 +0300 Subject: [PATCH] IDEA-209426: support AltGr only for some specific languages --- .../src/com/intellij/ide/IdeEventQueue.java | 44 ++++++++++--------- .../keymap/impl/IdeKeyEventDispatcher.java | 23 +++++----- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/ide/IdeEventQueue.java b/platform/platform-impl/src/com/intellij/ide/IdeEventQueue.java index ee8a76fe4150..64cd1b86aab9 100644 --- a/platform/platform-impl/src/com/intellij/ide/IdeEventQueue.java +++ b/platform/platform-impl/src/com/intellij/ide/IdeEventQueue.java @@ -70,7 +70,7 @@ public class IdeEventQueue extends EventQueue { private static final Logger LOG = Logger.getInstance("#com.intellij.ide.IdeEventQueue"); private static final Logger TYPEAHEAD_LOG = Logger.getInstance("#com.intellij.ide.IdeEventQueue.typeahead"); private static final Logger FOCUS_AWARE_RUNNABLES_LOG = Logger.getInstance("#com.intellij.ide.IdeEventQueue.runnables"); - private static final boolean JAVA_11_OR_LATER = SystemInfo.isJavaVersionAtLeast(11, 0, 0); + public static final boolean JAVA_11_OR_LATER = SystemInfo.isJavaVersionAtLeast(11, 0, 0); private static TransactionGuardImpl ourTransactionGuard; /** @@ -370,27 +370,21 @@ public class IdeEventQueue extends EventQueue { if (Registry.is("keymap.windows.as.meta") && metaEvent != null) { e = metaEvent; } - if (JAVA_11_OR_LATER && (SystemInfo.isMac || SystemInfo.isWindows)) { - if (e instanceof KeyEvent && ((KeyEvent)e).getKeyCode() == KeyEvent.VK_ALT_GRAPH) { - if (!Registry.is("actionSystem.force.alt.gr") && Registry.is("actionSystem.fix.alt.gr")) { - ((KeyEvent)e).setKeyCode(KeyEvent.VK_ALT); - } + if (JAVA_11_OR_LATER && e instanceof KeyEvent && ((KeyEvent)e).getKeyCode() == KeyEvent.VK_ALT_GRAPH && !isAltGrExpected((KeyEvent)e)) { + ((KeyEvent)e).setKeyCode(KeyEvent.VK_ALT); + } + if (JAVA_11_OR_LATER && e instanceof InputEvent && ((InputEvent)e).isAltGraphDown() && !isAltGrExpected((InputEvent)e)) { + try { + Field field = InputEvent.class.getDeclaredField("modifiers"); + field.setAccessible(true); + int modifiers = field.getInt(e); + modifiers |= InputEvent.ALT_MASK; + modifiers |= InputEvent.ALT_DOWN_MASK; + modifiers &= ~InputEvent.ALT_GRAPH_MASK; + modifiers &= ~InputEvent.ALT_GRAPH_DOWN_MASK; + field.setInt(e, modifiers); } - if (e instanceof InputEvent && ((InputEvent)e).isAltGraphDown()) { - if (!Registry.is("actionSystem.force.alt.gr") && Registry.is("actionSystem.fix.alt.gr")) { - try { - Field field = InputEvent.class.getDeclaredField("modifiers"); - field.setAccessible(true); - int modifiers = field.getInt(e); - modifiers |= InputEvent.ALT_MASK; - modifiers |= InputEvent.ALT_DOWN_MASK; - modifiers &= ~InputEvent.ALT_GRAPH_MASK; - modifiers &= ~InputEvent.ALT_GRAPH_DOWN_MASK; - field.setInt(e, modifiers); - } - catch (Exception ignored) { - } - } + catch (Exception ignored) { } } @@ -1415,4 +1409,12 @@ public class IdeEventQueue extends EventQueue { } r.run(); } + + private static boolean isAltGrExpected(InputEvent event) { + if (SystemInfo.isMac) return false; // never use AltGr on Mac + if (!SystemInfo.isWindows) return true; // default behaviour on Linux + if (Registry.is("actionSystem.force.alt.gr")) return true; + if (!Registry.is("actionSystem.fix.alt.gr")) return false; + return IdeKeyEventDispatcher.isAltGrLayout(event.getComponent()); + } } diff --git a/platform/platform-impl/src/com/intellij/openapi/keymap/impl/IdeKeyEventDispatcher.java b/platform/platform-impl/src/com/intellij/openapi/keymap/impl/IdeKeyEventDispatcher.java index 42d0b7419eae..cdd868c26014 100644 --- a/platform/platform-impl/src/com/intellij/openapi/keymap/impl/IdeKeyEventDispatcher.java +++ b/platform/platform-impl/src/com/intellij/openapi/keymap/impl/IdeKeyEventDispatcher.java @@ -422,23 +422,15 @@ public final class IdeKeyEventDispatcher implements Disposable { DataContext dataContext = myContext.getDataContext(); KeyEvent e = myContext.getInputEvent(); + if (IdeEventQueue.JAVA_11_OR_LATER && SystemInfo.isWindows && e.isAltGraphDown()) return false; // don't search for shortcuts + // http://www.jetbrains.net/jira/browse/IDEADEV-12372 boolean isCandidateForAltGr = myLeftCtrlPressed && myRightAltPressed && focusOwner != null && e.getModifiers() == (InputEvent.CTRL_MASK | InputEvent.ALT_MASK); if (isCandidateForAltGr) { if (Registry.is("actionSystem.force.alt.gr")) { return false; } - final InputContext inputContext = focusOwner.getInputContext(); - if (inputContext != null) { - Locale locale = inputContext.getLocale(); - if (locale != null) { - @NonNls final String language = locale.getLanguage(); - if (ALT_GR_LAYOUTS.contains(language)) { - // don't search for shortcuts - return false; - } - } - } + if (isAltGrLayout(focusOwner)) return false; // don't search for shortcuts } KeyStroke originalKeyStroke = KeyStrokeAdapter.getDefaultKeyStroke(e); @@ -986,4 +978,13 @@ public final class IdeKeyEventDispatcher implements Disposable { } return SwingUtilities.getRootPane(component); } + + public static boolean isAltGrLayout(Component component) { + if (component == null) return false; + InputContext context = component.getInputContext(); + if (context == null) return false; + Locale locale = context.getLocale(); + if (locale == null) return false; + return ALT_GR_LAYOUTS.contains(locale.getLanguage()); + } }