2

当我输入 ALT+A 时,我想输入:“/ammo”。程序运行,但它似乎在运行后立即停止:我按 alt+A 或 A 并且代码根本没有做任何事情。

package jnativehook01;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class Example implements NativeKeyListener {
    public void nativeKeyPressed(NativeKeyEvent e) {

        if (NativeKeyEvent.getKeyText(e.getKeyCode()).equals("A")) {
            try {
                GlobalScreen.unregisterNativeHook();

                Robot bot;
                try {
                    bot = new Robot();

                    String text = "/ammo";
                     StringSelection stringSelection = new StringSelection(text);
                     Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                     clipboard.setContents(stringSelection, stringSelection);

                    //type: /ammo
                    bot.keyPress(KeyEvent.VK_T);
                    bot.keyRelease(KeyEvent.VK_T);


                } catch (AWTException e1) {

                }




            } catch (NativeHookException e1) {
            }
        }
    }

    public void nativeKeyReleased(NativeKeyEvent e) {
        System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    public void nativeKeyTyped(NativeKeyEvent e) {
        System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
    }

    public static void main(String[] args) {

        new Example();
    }
}
4

2 回答 2

2

好的,现在它正在工作:

package jnativehook01;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import java.util.logging.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;

public class Example implements NativeKeyListener {
    public void nativeKeyPressed(NativeKeyEvent e) {
        if (NativeKeyEvent.getKeyText(e.getKeyCode()).equals("A")) {
            Robot bot;
            try {
                String text = "/ammo";
                StringSelection stringSelection = new StringSelection(text);
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                clipboard.setContents(stringSelection, stringSelection);

                bot = new Robot();

                for (int i = 0; i < 10; i++) {
                    //t
                    bot.keyPress(KeyEvent.VK_T);
                    bot.delay(100);
                    bot.keyRelease(KeyEvent.VK_T);

                    bot.delay(500);

                    bot.keyPress(KeyEvent.VK_CONTROL);
                    bot.keyPress(KeyEvent.VK_V);
                    bot.keyRelease(KeyEvent.VK_V);
                    bot.keyRelease(KeyEvent.VK_CONTROL);

                    bot.delay(500);

                    //Enter

                    bot.keyPress(KeyEvent.VK_ENTER);
                    bot.keyRelease(KeyEvent.VK_ENTER);

                    bot.delay(1000);

                    bot.keyPress(KeyEvent.VK_ENTER);
                    bot.keyRelease(KeyEvent.VK_ENTER);

                    bot.delay(400);
                }

            } catch (AWTException e1) {
            }

        }
    }

    public void nativeKeyReleased(NativeKeyEvent e) {

    }

    public void nativeKeyTyped(NativeKeyEvent e) {

    }

    public static void main(String[] args) {
        Example ex = new Example();
        try {
            GlobalScreen.registerNativeHook();
            Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
            logger.setLevel(Level.OFF);
        } catch (NativeHookException eb) {
            System.out.println(eb.getMessage());

        }
        GlobalScreen.addNativeKeyListener(ex);
    }

}
于 2017-09-17T22:11:38.953 回答
1

您的代码仍然存在一些问题。

if (NativeKeyEvent.getKeyText(e.getKeyCode()).equals("A")) { 这不是检查 A 键的正确方法。这将检查 AWT 是否为该键生成 ascii 'A'。这些值可以在运行时被 JVM 覆盖。您应该检查 NativeKeyEvent.VK_A 常量。您也未能检查该关键事件上的 ALT 标志。像下面这样的东西更多的是你正在寻找的东西。

if (e.getKeyCode() == NativeKeyEvent.VC_A && e.getModifiers() & NativeInputEvent.ALT_MASK) {

关于线程安全的说明。 您在键回调中依赖 AWT,但是,该库默认情况下不使用 AWT 来调度事件。您需要查看 wiki 的线程安全部分以获取线程安全示例。

为方便起见,您可以选择替换RobotsGlobalScreen.postNativeEvent(...),以省略 Swing/AWT,但这不是必需的。

关于在事件侦听器回调内部阻塞的说明。 如果您通过睡眠或其他长时间运行的进程在此函数内部阻塞,您可能会导致操作系统延迟关键事件传递,或者更糟糕的是,某些操作系统会删除库。此删除不在库的控制范围内,由操作系统控制。

于 2017-10-09T19:48:18.527 回答