How to define a shortcut for special keys in Java Swing, for example. German umlaut key Ä?

How to define a key combination for special top-level keys such as german umlaut key Ä ? I found a way to match the unicode letters that are used for standard keyboards by default, see here . But the key event for the German umlaut key Ä is:

java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='ä',keyLocation=KEY_LOCATION_STANDARD,rawCode=222,primaryLevelUnicode=228,scancode=40] on frame0 

The idea is to register keyboard actions:

 import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; public class KeyStrokeForGermanUmlaut { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(); frame.setPreferredSize(new Dimension(600, 400)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); final JLabel label = new JLabel("Text shall change with shortcut"); panel.add(label); panel.registerKeyboardAction(new AbstractAction() { @Override public void actionPerformed(ActionEvent event) { label.setText("It is working!!!"); } }, KeyStroke.getKeyStroke("control typed Ä"), JComponent.WHEN_IN_FOCUSED_WINDOW); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }); } } 
+6
source share
3 answers

I'm afraid there is something suspicious about modifier handling for CTRL. That is: when checking the received modifier key = InputEvent.CTRL_MASK, advanced modifier = InputEvent.CTRL_DOWN_MASK. And the javadoc API is a little suspicious.

In addition, Ä is not a special case when “control” is not taken into account.

To make it work, I had to add a dirty hack: register a key listener that calls this action. I have to control something.

Otherwise, I used InputMap / ActionMap, as expected. The input map does not seem to work, but as far as I know, it does not work if it is added to the JTextField or in another answer (for Ä ). The following works are terrible.

 final JLabel label = new JLabel("Text shall change with shortcut"); final KeyStroke key = KeyStroke.getKeyStroke((Character)'k', InputEvent.CTRL_DOWN_MASK, false); final Object actionKey = "auml"; final Action action = new AbstractAction() { @Override public void actionPerformed(ActionEvent event) { System.out.println("aha"); label.setText("It is working!!!"); } }; label.addKeyListener(new KeyAdapter() { @Override public void keyPressed(java.awt.event.KeyEvent e) { if (e.isControlDown() && e.getKeyChar() == 'ä') { System.out.println("Ctrl-ä"); label.getActionMap().get(actionKey).actionPerformed(null); // return; } super.keyPressed(e); } }); label.getInputMap().put(key, actionKey); label.getActionMap().put(actionKey, action); 
+2
source
  • you can focus on JLabel, nothing happens for KeyEvents

  • should be the starting point when moving focus to JFrames ContentPane (can be used as a JPanel, but has a BorderLayout compared to a simple JPanel - FlowLayout)

-

 import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; public class KeyStrokeForGermanUmlaut { private JFrame frame = new JFrame(); private JLabel label = new JLabel("Text shall change with shortcut"); public KeyStrokeForGermanUmlaut() { frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), "CTRL + A"); frame.getRootPane().getActionMap().put("CTRL + A", updateCol()); frame.setPreferredSize(new Dimension(600, 100)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(label); frame.pack(); frame.setLocation(150, 150); frame.setVisible(true); } private Action updateCol() { return new AbstractAction("Hello World") { private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent e) { label.setText(label.getText() + " presses"); } }; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new KeyStrokeForGermanUmlaut(); } }); } } 

.

.


EDIT see description in getKeyStroke / vs getKeyStrokeForEvent API

then the result may be (a bit lost when and how to use SHIFT modifiers with the uppercase form (ä and ú) for these two characters, maybe someone will help us with these parts of KeyEvents)

enter image description here

from

 import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; public class KeyStrokeForGermanUmlaut { private JFrame frame = new JFrame(); private JLabel label = new JLabel("Text shall change with shortcut"); public KeyStrokeForGermanUmlaut() { frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( KeyStroke.getKeyStroke("typed ä"), "typed ä"); frame.getRootPane().getActionMap().put("typed ä", updateCol()); frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( KeyStroke.getKeyStroke("typed ú"), "typed ú"); frame.getRootPane().getActionMap().put("typed ú", updateCol1()); frame.setPreferredSize(new Dimension(600, 100)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(label); frame.pack(); frame.setLocation(150, 150); frame.setVisible(true); } private Action updateCol() { return new AbstractAction("Hello World") { private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent e) { label.setText(label.getText() + " presses - ä"); } }; } private Action updateCol1() { return new AbstractAction("Hello World") { private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent e) { label.setText(label.getText() + " presses - ú"); } }; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new KeyStrokeForGermanUmlaut(); } }); } } 
+4
source
 Ä, \xC4, Ä, Ä, %C4, %C3%84 

There is a code for Ä . Try it out, maybe one of them will work for you.

-1
source

All Articles