簡體   English   中英

使用Nimbus LAF的Mac鍵盤快捷鍵

[英]Mac Keyboard Shortcuts with Nimbus LAF

有沒有辦法在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);
    }
}

我這樣做是一種解決方法,因為Nimbus在OS X上覆蓋了剪切/復制/粘貼和select-all的鍵盤快捷鍵( Meta鍵與Ctrl鍵)。 如果只是鍵盤快捷鍵沒有被覆蓋,我寧願一直使用Nimbus。

使用getMenuShortcutKeyMask()方法與NimbusLookAndFeel一起使用以啟用鍵,如本所示。 另見相關答案

JTextField的特定情況下,您可以在引發原始操作的鍵綁定中使用掩碼。

int MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JTextField jtf = new JTextField("Test");
jtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A, MASK), "select-all");
jtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, MASK), "copy");
jtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_X, MASK), "cut");
jtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V, MASK), "paste");

不同的組件使用不同的鍵,因此要映射所有鍵,您必須定義不同的鍵。 例如(從這里找到的基地):

private void addOSXKeyStrokes(InputMap inputMap) {
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.META_DOWN_MASK), DefaultEditorKit.copyAction);
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.META_DOWN_MASK), DefaultEditorKit.cutAction);
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.META_DOWN_MASK), DefaultEditorKit.pasteAction);
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.META_DOWN_MASK), DefaultEditorKit.selectAllAction);
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.META_DOWN_MASK), "copy");
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.META_DOWN_MASK), "selectAll");
}

然后可以將其映射到不同的組件,如下所示:

// This must be performed immediately after the LaF has been set
if (System.getProperty("os.name", "").startsWith("Mac OS X")) {
  // Ensure OSX key bindings are used for copy, paste etc
  // Use the Nimbus keys and ensure this occurs before any component creation
  addOSXKeyStrokes((InputMap) UIManager.get("EditorPane.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("FormattedTextField.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("PasswordField.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("TextField.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("TextPane.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("TextArea.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("Table.ancestorInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("Tree.focusInputMap"));
}

此處列出了Aqua(OS X外觀和感覺)動作名稱的完整列表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM