Search code examples
javaswingkeyboard-shortcutslook-and-feel

Java Swing L&F native keyboard shortcuts


How do I leverage standard operating system keyboard shortcuts in a Java Swing application? I see how to add a javax.swing.JToolbar or a menu bar to my application, but it doesn't appear to be bound to de facto standard keyboard shortcuts (like Ctrl+S for Save).


Solution

  • You can use Toolkit.getDefaultToolkit.getMenuShortcutKeyMask() to get the correct modifier key per-platform (Ctrl, Command, etc.), but I'm not aware of any way to find out what the "standard" shortcuts for an action are, without defining them yourself.

    Something that excels a little bit more at being native, like SWT, might be better at this kind of thing, though.