使用Nimbus LAF的Mac键盘快捷键

swo*_*ing 5 java macos swing keyboard-shortcuts nimbus

有没有办法在OS X上使用Nimbus LAF(外观和感觉),同时仍然能够使用该Meta键进行剪切/复制/粘贴和选择所有操作?

我目前在我的Swing应用程序的main方法中有以下代码,它根据操作系统更改了LAF(OS X的默认值,所有其他的Nimbus):

if (!System.getProperty("os.name", "").startsWith("Mac OS X")) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(LicenseInspectorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(LicenseInspectorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(LicenseInspectorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(LicenseInspectorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
}
Run Code Online (Sandbox Code Playgroud)

我这样做是一种解决方法,因为Nimbus会覆盖在OS X上剪切/复制/粘贴和select-all的键盘快捷键(Meta键与Ctrl键).如果只是键盘快捷键没有被覆盖,我宁愿一直使用Nimbus.

tra*_*god 3

使用该getMenuShortcutKeyMask()方法可以NimbusLookAndFeel启用该\xe2\x8c\x98密钥,如本示例所示。另请参阅此相关答案

\n\n

在 a 的特殊情况下,您可以在键绑定JTextField中使用掩码,您可以在唤起原始操作的

\n\n
int MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();\nJTextField jtf = new JTextField("Test");\njtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A, MASK), "select-all");\njtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, MASK), "copy");\njtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_X, MASK), "cut");\njtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V, MASK), "paste");\n
Run Code Online (Sandbox Code Playgroud)\n