1

I'd like to use a simple java Robot that types a text when I click Ctrl+Q. But this has to be done even if I am focused an another app (eg. a game). My code works fine, but it runs only if my JFrame is in focus.

button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Robot robot = null;
                try {
                    robot = new Robot();
                } catch (AWTException e) {
                    e.printStackTrace();
                }

                robot.mouseMove(350, 150);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);


                robot.keyPress(KeyEvent.VK_T);
                robot.keyRelease(KeyEvent.VK_T);

                // Solution for different keyboard layouts (ALT values)
                try {
                    alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD4, KeyEvent.VK_NUMPAD7);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                robot.keyPress(KeyEvent.VK_Q);
                robot.keyRelease(KeyEvent.VK_Q);

            }
eltabo
  • 3,749
  • 1
  • 21
  • 33
fucorogu
  • 43
  • 6

1 Answers1

2

you should try the jnativehook

example usage

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.Robot;
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 = new Robot();
            bot.keyPress(KeyEvent.VK_A);
           }
       }
       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);
}

    }

This code uses native methods of windows but the good thing is, its easily readable by a java programmer and not a c#,c++,c etc programmer.This library of classes will listen to the key pressed on any application(it is a global keyboard listener), if a certain key is press then perform the Robot class methods(e.g. mousePress() etc.).

P.S. the documentation of classes used is in the file of jnativehook that you are going to download

0xDEADBEEF
  • 590
  • 1
  • 5
  • 16
  • FYI, >=JNativeHook-2.0.0 has support for posting back native events as well so you don't need to convert to something the Robots class can understand. – Alex Barker Aug 28 '17 at 23:23