Use default KeyMap native OS

In Java, using the non-default appearance of the system, we will have a different map.

For example, I use Mac OS X and use the appearance and appearance (non-standard appearance of the system). The effect is that I lose my "meta" key to select everything in the text component. Mac os x should have "meta + a", but using Substance we should use "ctrl + a" (and much more, for example " next word "," pre word "," end of line "," beginning of line ", etc.) Thus, we did not have the sensation of mac os x, using the appearance of a non-standard system (appearance and perception of matter )

Is there a way to use the appearance of the system not by default, but use the system (native) layout?

+5
source share
2 answers

Work around

A less elegant solution, you can try adding a key listener to override the default behavior of "ctrl + a" by implementing the keyPressed method (note that the following example does not prohibit "ctrl + a" just adds support for "meta + a"):

@Override
public void keyPressed(final KeyEvent e) {
  // Get the default toolkit shortcut mask ("meta" for OSX).
  int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

  // You could also check modifiers against KeyEvent.META_MASK...
  if (e.getModifiers() == keyMask && e.getKeyCode() == KeyEvent.VK_A) {
    // Select everything (assumes member of child text component class).
    this.selectAll();

    // We handled this keystroke, 'a' will be ignored by underlying text component.
    e.consume(); 
  }
}

A better alternative would be to use inputMaps (see uudashr comment below).


Thoughts on the root cause

, , ( LAF) , , " ", . , SubstanceLookAndFeel BasicLookAndFeel, . , BasicLookAndFeel, initComponentDefaults. UIDefaults LAF, getDefaults().

:

  • " ", , , .
  • - LAF... - ?
+1

- META- CTRL. , OS X META, CTRL. , CTRL META LAF. , , . , JMenuItem CTRL + O, META + O .

java.awt.Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

            public void eventDispatched(AWTEvent event) {
                KeyEvent kev = (KeyEvent) event;
                if (kev.getID() == KeyEvent.KEY_PRESSED || kev.getID() == KeyEvent.KEY_RELEASED || kev.getID() == KeyEvent.KEY_PRESSED) {
                    if ((kev.getModifiersEx() & KeyEvent.META_DOWN_MASK) != 0 && !((kev.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) != 0)) {
                        kev.consume(); // Drop the original event, this is really optional.
                        KeyEvent fake = new KeyEvent(kev.getComponent(),
                                kev.getID(),
                                kev.getWhen(),
                                (kev.getModifiersEx() & ~KeyEvent.META_DOWN_MASK) | KeyEvent.CTRL_DOWN_MASK,
                                kev.getKeyCode(), kev.getKeyChar());
                        java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(fake);
                    }
                }
            }
        }, KeyEvent.KEY_EVENT_MASK);

AWTEventListener AWTEvent .

0

All Articles