Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use default KeyMap of native OS

In Java, by using non-default system look and feel we will have different keymap.

For example I'm using Mac OS X and use Substance look and feel (non-default system look and feel). The effect is I'm loosing my "meta" key for select all on text component In mac os x should be "meta + a", but using Substance we have to use "ctrl + a" (and a lot more such as "next word", "prev word", "end line, "begin line", etc) So we didn't have the mac os x feel by using non-default system look and feel (Substance look and feel).

Is there a way to use non-default system look and feel but use system (native) keymap?

like image 826
uudashr Avatar asked Nov 14 '22 14:11

uudashr


1 Answers

The work around

A less elegant solution, you could try adding a key listener to override the default "ctrl + a" behavior by implementing a keyPressed method (please note that the following sample does not disallow "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 comment by uudashr below).


Thoughts on the root cause

Unfortunately, as the class name suggests the look and feel (or LAF) is a combination of appearance i.e. look, as well as "system behavior", i.e. feel. If you dig around the substance source, the SubstanceLookAndFeel overrides the BasicLookAndFeel that ships with swing. It looks as though it is within the BasicLookAndFeel the offending behavior is set in initComponentDefaults. You should be able to get the UIDefaults from the LAF by calling getDefaults().

Problems from here are:

  • "System behaviors" which you wish to change are intermingled with appearance settings you wish to leave untouched.
  • I have also been unable to find any easy way to inject these defaults into substance at the LAF level... Anyone with other ideas on this?
like image 191
Clinton Avatar answered Dec 09 '22 17:12

Clinton