Question

I thought I can use the string representation of a keyboard key to configure shortcuts, i.e. let somebody define a keystroke in a resource bundle and pass that value directly to javax.swing.KeyStroke.getKeyStroke(String). But this does not work for international keyboards, e.g. arabic. There the keyboard keys are only high level key typed events, but shortcuts need to be defined as low level key pressed events with their key code representation. If one knows the according key code one can use the english representation of that key to get it working, but where should one know this from? I wrote a key listener that tells that, but I need something while parsing a String programmatically. Any ideas to use the natural language dependent key representation for shortcuts?

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;


public class InternationalKeyStroke {

    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("Type the key you want to know the key code of!");
                panel.add(label);
                frame.addKeyListener(new KeyAdapter() {

                    @Override
                    public void keyPressed(KeyEvent e) {
                        String text = "Key " + e.getKeyChar() + " needs to be configured with char ='" + (char) e.getKeyCode() + "'";
                        label.setText(text);
                        System.out.println(text);
                    }

                });
                frame.getContentPane().add(panel);
                frame.pack();
                frame.setVisible(true);

                KeyStroke working = KeyStroke.getKeyStroke("control typed A");
                label.registerKeyboardAction(new AbstractAction() {

                    @Override
                    public void actionPerformed(ActionEvent event) {
                        System.out.println("Working action " + event);
                    }
                }, working, JComponent.WHEN_IN_FOCUSED_WINDOW);

                KeyStroke target = KeyStroke.getKeyStroke("control typed ش");
                System.out.println(target);
                label.registerKeyboardAction(new AbstractAction() {

                    @Override
                    public void actionPerformed(ActionEvent event) {
                        System.out.println("Not working Action " + event);
                    }
                }, target, JComponent.WHEN_IN_FOCUSED_WINDOW);
            }
        });
    }
}
Was it helpful?

Solution

This seems not to be possible. I checked Microsoft Office and Eclipse and they all don't use the real unicode letter of the current locale, but the letter of the according virtual key code...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top