本文介绍了java.awt.event.KeyEvent中不能够完全mappin AZERTY键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好所以,我提出这显示keyEvent.getKey code(每个I preSS的关键时间java程序)。

Hello So I made a java program which display the keyEvent.getKeyCode() each time I press a key.

其目的是能够做出某种机器人能够使用java类机器人和部分的keyEvent驱动混帐/天际/ FRAPS的。

The purpose was to be able to make some kind of bot able to drive git/skyrim/fraps using the java robot class and some keyEvent.

不过似乎它不能映射AZERTY键盘,你可以看到一些的keyEvent code是相同的。

However it seems that it cannot map AZERTY keyboard, as you can see some keyEvent code are the same.

有驾驶键盘的任何其他方式?

is there any other way of driving a keyboard?

下面是该计划,如果你想知道:

Here is the program if you are wondering :

主类:

package keykeykey;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField nameTextField = new JTextField();
    frame.add(nameTextField, BorderLayout.NORTH);

    JTextField codeTextField = new JTextField();
    frame.add(codeTextField, BorderLayout.SOUTH);

    MyKeyListener myKeyListener = new MyKeyListener(codeTextField, nameTextField);
    nameTextField.addKeyListener(myKeyListener);

    frame.setSize(250, 100);
    frame.setVisible(true);
  }
}

重新定义的KeyListener

keyListener redefinition

package keykeykey;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;

public class MyKeyListener extends KeyAdapter implements KeyListener {

    public JTextField codeTextField;
    public JTextField writeTextField;

    public MyKeyListener(JTextField codeTextField, JTextField writeTextField) {
        this.codeTextField = codeTextField;
        this.writeTextField = writeTextField;
    }

    @Override
    public void keyPressed(KeyEvent keyEvent) {
    printIt("Pressed", keyEvent);
    codeTextField.setText("code = " + keyEvent.getKeyCode());
  }

  @Override
    public void keyReleased(KeyEvent keyEvent) {
    codeTextField.setText("");
    writeTextField.setText("");
  }

  @Override
    public void keyTyped(KeyEvent keyEvent) {
    //do nothing
  }

  private void printIt(String title, KeyEvent keyEvent) {
    int keyCode = keyEvent.getKeyCode();
    String keyText = KeyEvent.getKeyText(keyCode);
    System.out.println(title + " : " + keyText + " / " + keyEvent.getKeyChar() + " code = " + keyEvent.getKeyCode());
  }

}

感谢。

诗:是的,我知道AutoHotkey的,但我需要一个强类型语言,我爱的Java

Ps : yes I know about autoHotKey, however I need a strongly typed language and I love java.

我必须有没有前$ P $正确pssed我自己,我知道,SHIFT + VK1使1,同样适用于其他键。

I must have not expressed myself correctly, I know that shift+VK1 makes "1" and the same goes for other keys.

我的问题是,你可以在第一张照片看,一些重点产生无法识别的关键事件(带有红色条)。

My problem is that, as you can see in the first pictures, some key generate unrecognized key event (the one with red bars).

例如那些2键:

推荐答案

一个虚拟键可以根据应用到修改器来改变。实际结果是依赖于键盘驱动程序本身,而是例如...

A virtual key can be changed based on the modifiers applied to. The actual result is depended on the keyboard driver itself, but for example...

+ 1 会生成一个的KeyEvent 键code KeyEvent.VK_1 ,但其 keyChar

+ will generate a KeyEvent whose keyCode is KeyEvent.VK_1, but whose keyChar is !

您基本上忽视了这些修饰...

You are basically ignoring these modifiers...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestKeyStrokes {

    public static void main(String[] args) {
        new TestKeyStrokes();
    }

    public TestKeyStrokes() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JList keyList;
        private DefaultListModel<String> model;

        public TestPane() {
            setLayout(new BorderLayout());
            model = new DefaultListModel<>();
            keyList = new JList(model);
            add(new JScrollPane(keyList));
            keyList.addKeyListener(new KeyListener() {

                @Override
                public void keyTyped(KeyEvent e) {
                }

                @Override
                public void keyPressed(KeyEvent e) {
                }

                @Override
                public void keyReleased(KeyEvent e) {
                    StringBuilder sb = new StringBuilder(64);
                    if (e.isShiftDown()) {
                        sb.append("[SHIFT]");
                    } else if (e.isAltDown()) {
                        sb.append("[ALT]");
                    } else if (e.isControlDown()) {
                        sb.append("[CTRL]");
                    } else if (e.isAltGraphDown()) {
                        sb.append("[ALT-GRPH]");
                    } else if (e.isMetaDown()) {
                        sb.append("[META]");
                    }
                    switch (e.getKeyCode()) {
                        case KeyEvent.VK_SHIFT:
                        case KeyEvent.VK_ALT:
                        case KeyEvent.VK_CONTROL:
                        case KeyEvent.VK_ALT_GRAPH:
                        case KeyEvent.VK_META:
                            break;
                        default:
                            sb.append("[").append(getVKText(e.getKeyCode())).append("/").append(e.getKeyChar()).append("]");
                            break;
                    }

                    model.addElement(sb.toString());
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }
    }

    private static VKCollection vKCollection;

    static VKCollection getVKCollection() {
        if (vKCollection == null) {
            vKCollection = new VKCollection();
        }
        return vKCollection;
    }

    static String getVKText(int keyCode) {
        VKCollection vkCollect = getVKCollection();
        Integer key = Integer.valueOf(keyCode);
        String name = vkCollect.findName(key);
        if (name != null) {
            return name.substring(3);
        }
        int expected_modifiers
                = (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);

        Field[] fields = KeyEvent.class.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            try {
                if (fields[i].getModifiers() == expected_modifiers
                        && fields[i].getType() == Integer.TYPE
                        && fields[i].getName().startsWith("VK_")
                        && fields[i].getInt(KeyEvent.class) == keyCode) {
                    name = fields[i].getName();
                    vkCollect.put(name, key);
                    return name.substring(3);
                }
            } catch (IllegalAccessException e) {
                assert (false);
            }
        }
        return "UNKNOWN";
    }

    static class VKCollection {

        Map code2name;
        Map name2code;

        public VKCollection() {
            code2name = new HashMap();
            name2code = new HashMap();
        }

        public synchronized void put(String name, Integer code) {
            assert ((name != null) && (code != null));
            assert (findName(code) == null);
            assert (findCode(name) == null);
            code2name.put(code, name);
            name2code.put(name, code);
        }

        public synchronized Integer findCode(String name) {
            assert (name != null);
            return (Integer) name2code.get(name);
        }

        public synchronized String findName(Integer code) {
            assert (code != null);
            return (String) code2name.get(code);
        }
    }
}

这篇关于java.awt.event.KeyEvent中不能够完全mappin AZERTY键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 10:16