javaeclipseawtrobotjnativehook

Java jnativehook typing a string not working


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

Solution

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