使用Nimbus LAF的Mac键盘快捷键 [英] Mac Keyboard Shortcuts with Nimbus LAF
问题描述
有没有办法在OS X上使用Nimbus LAF(外观和感觉),同时仍然可以使用 Meta 键进行剪切/复制/粘贴和选择所有操作?
Is there a way to use Nimbus LAF (Look And Feel) on OS X while still being able to use the Meta key for cut/copy/paste and select-all operations?
我目前在我的Swing应用程序的main方法中有以下代码,它根据操作系统更改了LAF(OS X的默认值,所有其他的Nimbus):
I currently have the following code in my Swing app's main method, which changes up the LAF based on the operating system (default for OS X, Nimbus for all others):
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。
I do this as a workaround because Nimbus overrides the keyboard shortcuts for cut/copy/paste and select-all on OS X (Meta key versus Ctrl key). I would prefer to use Nimbus all the time, if only the keyboard shortcuts weren't overridden.
推荐答案
使用 getMenuShortcutKeyMask()
方法与 NimbusLookAndFeel
配合使用以启用⌘键,如此example 。另请参阅此相关答案。
Using the getMenuShortcutKeyMask()
method works with NimbusLookAndFeel
to enable the ⌘ key, as shown in this example. See also this related answer.
在特定情况下a JTextField
,您可以在键绑定。
In the particular case of a JTextField
, you can use the mask in a key binding that evokes the original action.
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");
这篇关于使用Nimbus LAF的Mac键盘快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!