1

Forgive me if this question has been asked many times, but after looking up many of the questions I still can't find a definitive answer. I have a robot class that sends keyboard inputs like a macro running this in Eclipse.

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;


public class Macro 
{   
    public static void main (String args[]) throws AWTException, InterruptedException 
    {
         Robot r = new Robot();
         while(true) 
         {
              r.keyPress(KeyEvent.VK_V);
              r.keyRelease(KeyEvent.VK_V);
              r.delay(500);
              r.keyPress(KeyEvent.VK_T);
              r.KeyRelease(KeyEvent.VK_T);
              r.delay(500);
        }
    }
}

Assume this scenario where run the robot, open notepad, put focus into notepad, the program will work fine typing the keys into notepad. However, If I were to minimize notepad, open up google, the robot will start typing in google and not notepad. What I want is that the robot will ONLY run in my selected program (for example notepad) AND ALSO RUN when that selected program is minimized. This way my robot can run in the minimized program, and I can do other things within my computer without the robot affecting me.

I've heard scenarios where its not possible in java and that I have to this in native code, but others have said it is possible with extensions such as JNativeHook or some type of Actionlisteners. I delved into JNativeHook yet it doesn't seem to fix my exact problems. JNativeHook was able to give keyboard input detection while Ecipse was minimized, yet rather than Eclipse detecting my Keyboard inputs, I wanted a selected program of my choice (for example notepad) to detect the keyboard inputs.

The second problem was, I didn't want "GLOBAL" detection, but rather to filter the detection to ONLY my robot class. Therefore it will be able to distinguish detection from my typing and the robot typing. Even if I could solve the first two problems. The third problem would be that I wanted the robot to type ONLY within the selected application (for example notepad) and not interfere with whatever I'm typing while doing other things.

Is what I want to achieve possible with Eclipse/java ? Is there an alternative ? Any input is greatly appreciated.

2
  • Instead of giving typing events to your notepad file, you can just give output to your file.
    – afzalex
    Aug 23, 2015 at 12:30
  • Thanks for your input, I'm relatively new to java, if by output do you mean using something like a printwriter and creating a new .txt to print something in the .txt? I do believe that is the case for notepad. However, the purpose of this program is to create a macro that will run in the background for other programs (notepad was just an example) , programs such as games where keys must be pressed in a certain order running for a period of time, while still allowing the user to perform other tasks without interference from the robot.
    – Bao
    Aug 23, 2015 at 12:44

1 Answer 1

0

The short answer is it is not possible, even with native API.

You could use platform specific API with JNA to detect what Window currently has focus and only deliver events in particular circumstances, but delivering events to a minimized window's text area without focus is going to be very tricky if not impossible across Java supported platforms.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.