如何定义国际键盘的可读快捷方式?

时间:2014-04-02 14:36:50

标签: java swing keyboard keyboard-shortcuts

我以为我可以使用键盘键的字符串表示来配置快捷键,即让某人在资源包中定义键击并将该值直接传递给javax.swing.KeyStroke.getKeyStroke(String)。但这对国际键盘不起作用,例如:阿拉伯。键盘键只是高级键输入事件,但需要将快捷键定义为低级键按下事件及其键代码表示。如果一个人知道相应的密钥代码,就可以使用该密钥的英文表示来使其工作,但是应该从哪里知道这个?我写了一个关键的监听器,告诉我,但我在编程时解析String时需要一些东西。是否有任何想法使用自然语言相关的密钥表示来表示快捷方式?

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);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

这似乎不可能。我检查了Microsoft Office和Eclipse,他们都没有使用当前语言环境的真正unicode字母,而是根据虚拟键代码的字母......