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

            }
4
  • See this post: stackoverflow.com/questions/800747 Commented Aug 27, 2017 at 19:04
  • " But this has to be done even if I am focused an another app" Sounds like the makings of a key logger. Why should text typed in other apps be any business of this (your) app? Commented Aug 27, 2017 at 20:50
  • @AndrewThompson Sounds more like a game helper. Years ago, I wrote a program using Robot to send the keystrokes to twist chants on my paladin in Dark Age of Camelot. Commented Aug 29, 2017 at 9:55
  • @AndrewThompson I'm writing a remote desktop app Commented May 30, 2022 at 15:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.