如何定义国际键盘的可读快捷方式? [英] How to define readable shortcuts for international keyboards?

查看:107
本文介绍了如何定义国际键盘的可读快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

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

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...

这篇关于如何定义国际键盘的可读快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆