Search code examples
javamacosswinglook-and-feel

Java: Windows L&F under OSX. Substitute ctrl to command


I have to use one of Windows L&F for my Java application on OSX. Unfortunately, I can't change L&F to any Apple compatible.

It's very inconvenient to use windows hot keys (ctrl+c instead of cmd+c etc). Possible exists a "hack" that allows me globally to use command key instead of control (cmd -> ctrl) in hotkeys of windows L&F (don't wish to override key binding for each control)?

I use JGoodies Looks if it's matter.


Solution

  • Found what I've looked for:

    if (Desktop.getDesktop().isMacOSX()){
        // see MetalLookAndFeel class for details
        String[] keys = {"TextField.focusInputMap", "PasswordField.focusInputMap", "TextArea.focusInputMap", "TextPane.focusInputMap", "EditorPane.focusInputMap", "FormattedTextField.focusInputMap"};
        //              , "List.focusInputMap", "Table.ancestorInputMap", "Tree.focusInputMap"};
        for(String item : keys){
            InputMapUIResource map = (InputMapUIResource) UIManager.get(item);
            map.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.META_MASK), DefaultEditorKit.copyAction);
            map.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.META_MASK), DefaultEditorKit.pasteAction);
            map.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.META_MASK), DefaultEditorKit.selectAllAction);
            map.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.META_MASK), DefaultEditorKit.cutAction);
        }