2

I'd like to type: "/ammo" when I type ALT+A. The program runs but it seems like to stop right after the running: I press alt+A or A and the code is not doing anything at all.

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();
    }
}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
fini
  • 146
  • 8

2 Answers2

2

Ok, now it's working:

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

}
fini
  • 146
  • 8
1

There are still a few problems with your code.

if (NativeKeyEvent.getKeyText(e.getKeyCode()).equals("A")) { This is not the correct way to check for the A key. This will check to see if AWT produces the ascii 'A' for that key. These values could be overridden by the JVM at runtime. You should check for the NativeKeyEvent.VK_A constant. You also fail to check for the ALT flag on that key event. Something like the following is more of what you are looking for.

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

A note on thread safety. You are relying on AWT inside of the key callback, however, this library does not use AWT to dispatch events by default. You need to take a look at the Thread Safety section of the wiki for Thread Safe examples.

You may choose to replace the Robots class with GlobalScreen.postNativeEvent(...) for convenience, to omit Swing/AWT, but it is not required.

A note on blocking inside of the event listener callback. If you block inside this function, via sleep or other long running process, you may cause a delay in key event delivery by the OS or worse, library removal by some operating systems. This removal is outside the control of the library and controlled by the OS.

Alex Barker
  • 4,316
  • 4
  • 28
  • 47