How to tie a team? How is the swing accelerator for the help menu?

The standard key combination for reference command is ? for macs. How to bind this key combination to a menu item.

Note. Since our users have different keyboard layouts, I am looking for a solution that does not require knowledge of which key "?" is situated on.

Using KeyStroke.getKeyStroke(String) , which says javadoc;

 Parses a string and returns a `KeyStroke`. The string must have the following syntax: <modifiers>* (<typedID> | <pressedReleasedID>) modifiers := shift | control | ctrl | meta | alt | button1 | button2 | button3 typedID := typed <typedKey> typedKey := string of length 1 giving Unicode character. pressedReleasedID := (pressed | released) key key := KeyEvent key code name, ie the name following "VK_". 

I have an example code:

 import javax.swing.*; import java.awt.Dimension; import java.awt.event.ActionEvent; public class HelpShortcut extends JFrame { public HelpShortcut(){ // A few keystrokes to experiment with //KeyStroke keyStroke = KeyStroke.getKeyStroke("pressed A"); // A simple reference - Works //KeyStroke keyStroke = KeyStroke.getKeyStroke("typed ?"); // Works KeyStroke keyStroke = KeyStroke.getKeyStroke("meta typed ?"); // What we want - Does not work // If we provide an invalid keystroke we get a null back - fail fast if (keyStroke==null) throw new RuntimeException("Invalid keystroke"); // Create a simple menuItem linked to our action with the keystroke as accelerator JMenuItem helpMenuItem = new JMenuItem(new HelpAction()); helpMenuItem.setAccelerator(keyStroke); // Install the menubar with a help menu JMenuBar mainMenu = new JMenuBar(); JMenu helpMenu = new JMenu("Help"); helpMenu.add(helpMenuItem); mainMenu.add(helpMenu); setJMenuBar(mainMenu); } // Scaffolding public static void main(String[] pArgs) { HelpShortcut helpShortcut= new HelpShortcut(); helpShortcut.setLocationRelativeTo(null); helpShortcut.setSize(new Dimension(100, 162)); helpShortcut.setVisible(true); } private class HelpAction extends AbstractAction { public HelpAction() { putValue(Action.NAME,"Help me!"); } @Override public void actionPerformed(final ActionEvent pActionEvent) { JOptionPane.showMessageDialog(HelpShortcut.this,"You should ask StackOverflow!"); } } } 
+7
source share
3 answers

On my keyboard "?" above the "/" key, so that you also use the shift key to enter "?". Therefore, to complete the binding you need to use:

 // KeyStroke keyStroke = KeyStroke.getKeyStroke("meta typed ?"); int modifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() + KeyEvent.SHIFT_DOWN_MASK; KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_SLASH, modifier); 
+6
source

See KeyEvent API Document - Notes Section:

Not all characters have a key code associated with them. For example, for a question mark there is no key code, because there is no keyboard for which it is displayed at the main level.

+4
source

(surprisingly, to me :-) the keyID identifier binding modifiers are not “supported”: while you can create and associate them with inputMap, they are never detected, because keyStrokes created inside typed keys really use keyChar and ignore modifiers. This creation occurs in JComponent.processKeyBindings (...)

 boolean processKeyBindings(KeyEvent e, boolean pressed) { if (!SwingUtilities.isValidKeyEventForKeyBindings(e)) { return false; } // Get the KeyStroke KeyStroke ks; if (e.getID() == KeyEvent.KEY_TYPED) { ks = KeyStroke.getKeyStroke(e.getKeyChar()); } else { ks = KeyStroke.getKeyStroke(e.getKeyCode(),e.getModifiers(), (pressed ? false:true)); } 

Reflecting on this, this may make sense: pressing / disabling controls physical keys, and typed controls the resulting combined “output” of one or more physical keys. If no valid keyChar exists for any given combination, then the keyTyped event is not generated.

The main problem is the famous usa-centrism of swing / awt developers: they are considered physical keys only on the user's layout ;-) In no way (what I know) to get other keys in the layout is the diagnostic method. Hope that turns out to be erroneous

+3
source

All Articles