Search code examples
javamacosswinglook-and-feel

Shortcut key bindings for cross platform look & feel(metal l&f)


I was reading about the cross-platform look & feel ie the metal L&f that looks the same across all platforms. I wanted to know what will be the shortcut keys for this l&f. I'm facing an issue where in MAC OS, if I have to copy/paste I have to use window shortcuts(ctrl + v/c) for copy/paste actions.I'm unable to use mac shortcuts (cmd+v/c). I'm wondering if it could be due to the l&f. Please help.Thanks in advance.


Solution

  • Yes, the look and feel can do this. One thing you can do, which I hope somebody has a better answer, is to set the input map for the components you want to behave differently. Here's a way to use the platform default input map for a text field.

    public static void startGui(){
        try{
            LookAndFeel aqua = UIManager.getLookAndFeel(); //aqua
            UIDefaults def = UIManager.getDefaults();
            Object b = def.get("TextField.focusInputMap");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            UIDefaults def2 = UIManager.getDefaults();
            def2.put("TextField.focusInputMap", b);
        }catch(Exception e){
            e.printStackTrace();
        }
    
        JFrame frame = new JFrame("title");
    
        JTextField field = new  JTextField();
        frame.add(field);
        frame.setSize(640, 480);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String[] args) throws Exception {
        EventQueue.invokeLater( ()->startGui() );
    }
    

    If you run that, then cmd+v should paste, but if you commend out the line def2.put(...) then ctrl+v will paste.

    Verified with jdk11 on OSX.