JNativeHook KeyEvent到Java KeyCode

JNativeHook KeyEvent到Java KeyCode,java,awtrobot,Java,Awtrobot,我正在写一个程序,可以录制鼠标和按键输入,录制完成后,播放它们。为了全局记录这些输入,我使用JNativeHook。 这对于所有的鼠标侦听器都很好,但是来自NativeKeyListener的NativeKeyCodes与来自Java的KeyCodes不同。作为一种解决方法,我获取了KeyText,然后使用 AWTKeyStroke ks = AWTKeyStroke.getAWTKeyStroke(keytext); ks.getKeyCode() 这适用于字母、数字甚至F键(例如:F10)

我正在写一个程序,可以录制鼠标和按键输入,录制完成后,播放它们。为了全局记录这些输入,我使用JNativeHook。 这对于所有的鼠标侦听器都很好,但是来自NativeKeyListener的NativeKeyCodes与来自Java的KeyCodes不同。作为一种解决方法,我获取了KeyText,然后使用

AWTKeyStroke ks = AWTKeyStroke.getAWTKeyStroke(keytext);
ks.getKeyCode()
这适用于字母、数字甚至F键(例如:F10)。 但每当它尝试转换字符串(如“Backspace”)时,就会出现以下异常:

java.lang.IllegalArgumentException: String formatted incorrectly
    at java.awt.AWTKeyStroke.getVKValue(AWTKeyStroke.java:576)
    at java.awt.AWTKeyStroke.getAWTKeyStroke(AWTKeyStroke.java:522)
    at main.ScriptPlayer.executeCommand(ScriptPlayer.java:71)
    at main.ScriptPlayer.access$5(ScriptPlayer.java:55)
    at main.ScriptPlayer$1.run(ScriptPlayer.java:38)
有没有一种方法可以成功地从Java将NativeKeyEvent(例如从backspace)转换为KeyCode

侦听器中的完整代码:

package listener;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.InputEvent;

import main.InputRecorder;

import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.mouse.NativeMouseEvent;

import util.KeyBindings;

public class InputRecordListener extends InputAdapter{

    private InputRecorder recorder;

    public InputRecordListener(InputRecorder recorder){
        this.recorder = recorder;
    }

    @Override
    public void nativeMouseMoved(NativeMouseEvent e) {
        recorder.move(getLocation());
    }

    @Override
    public void nativeMouseDragged(NativeMouseEvent e) {
        nativeMouseMoved(e);
    }

    @Override
    public void nativeMousePressed(NativeMouseEvent e) {
        recorder.mousePressed(convertButtonToButtonMask(e.getButton()));
    }

    @Override
    public void nativeMouseReleased(NativeMouseEvent e) {
        recorder.mouseReleased(convertButtonToButtonMask(e.getButton()));
    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
        if (e.getKeyCode() == KeyBindings.leftButton || e.getKeyCode() == KeyBindings.rightButton) return;
        System.out.println(NativeKeyEvent.getKeyText(e.getKeyCode()));
        recorder.keyPressed(NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {
        if (e.getKeyCode() == KeyBindings.leftButton || e.getKeyCode() == KeyBindings.rightButton) return;
        System.out.println(NativeKeyEvent.getKeyText(e.getKeyCode()));
        recorder.keyReleased(NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    private static Point getLocation(){
        return MouseInfo.getPointerInfo().getLocation();
    }

    private static int convertButtonToButtonMask(int button){
        switch(button){
        case 1:
            return InputEvent.BUTTON1_MASK;
        case 2:
            return InputEvent.BUTTON3_MASK;
        case 3:
            return InputEvent.BUTTON2_MASK;
        default:
            throw new IllegalArgumentException();
        }
    }
}
Player(在Robot上执行命令的类)中的完整代码


当我尝试使用JNativeHook 2.03作为backspace时,我将NativeKeyEvent.getKeyText(event.getKeyCode())正确地设置为“backspace”。想知道是什么问题吗?@lsiva当然,我也得到了“Backspace”,但我无法从该字符串中击键(请参见问题中的异常)。对不起,问题不清楚。
package main;

import java.awt.AWTException;
import java.awt.AWTKeyStroke;
import java.awt.Robot;
import java.io.BufferedReader;
import java.io.StringReader;

import util.TimeRecorder;

public class ScriptPlayer {

    private Robot robot;
    private boolean playing;
    private boolean paused;
    private boolean loop = false;
    private long resumptionTime = 0;

    public void playScript(final String script){
        new Thread(){
            public void run(){
                playing = true;
                paused = false;
                try {
                    if (robot == null)robot = new Robot();
                } catch (AWTException e) {
                    e.printStackTrace();
                }
                do{
                    BufferedReader reader = new BufferedReader(new StringReader(script));
                    try {
                        String line = reader.readLine();
                        int i = 0;
                        TimeRecorder tr = new TimeRecorder();
                        while (line != null && playing){
                            AutoClicker.getInstance().log(++i + ": " + line);
                            tr.start();
                            executeCommand(line);
                            while(paused || resumptionTime > System.currentTimeMillis()){sleep(1);}
                            AutoClicker.getInstance().logln("     Command Executed In " + tr.getTimeMillisPassed() + " Millis");
                            line = reader.readLine();
                        }
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                        break;
                    }
                }while (loop);
                playing = false;
                AutoClicker.getInstance().stopPlaying();
            }
        }.start();
    }

    //A command can look like this: "keypress: H"
    private void executeCommand(String command){
        if (command.contains("move")){
            int xValue = Integer.parseInt(command.substring(6, command.indexOf("/")));
            int yValue = Integer.parseInt(command.substring(command.indexOf("/") + 1));
            robot.mouseMove(xValue, yValue);
        }else if (command.contains("wait")){
            int value = Integer.parseInt(command.substring(6));
            resumptionTime = System.currentTimeMillis() + value;
        }else if (command.contains("mousepress")){
            int mask = Integer.parseInt(command.substring(12));
            robot.mousePress(mask);
        }else if (command.contains("mouserelease")){
            int mask = Integer.parseInt(command.substring(14));
            robot.mouseRelease(mask);
        }else if (command.contains("keypress")){
            String keytext = command.substring(10);
            AWTKeyStroke ks = AWTKeyStroke.getAWTKeyStroke(keytext);
            System.out.println(keytext);
            robot.keyPress(ks.getKeyCode());
        }else if (command.contains("keyrelease")){
            String keytext = command.substring(12);
            AWTKeyStroke ks = AWTKeyStroke.getAWTKeyStroke(keytext);
            System.out.println(keytext);
            robot.keyRelease(ks.getKeyCode());
        }
    }

    public void terminate(){
        playing = false;
    }

    public void pause(){
        resumptionTime = resumptionTime - System.currentTimeMillis();
        paused = true;
    }

    public void resume(){
        resumptionTime = resumptionTime + System.currentTimeMillis();
        paused = false;
    }

    public void setLoop(boolean loop){
        this.loop = loop;
    }
}