IDEA-209426: support AltGr only for some specific languages

This commit is contained in:
Sergey Malenkov
2019-03-28 19:33:11 +03:00
parent 3c674de4bb
commit 4e8befb43a
2 changed files with 35 additions and 32 deletions
@@ -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());
}
}
@@ -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());
}
}