4

is there anyway to type into a notepad.exe process from a JAVA process?

4
  • Please let know why do you want to do that, so that we can answer the question in the light of that knowledge. Commented Mar 20, 2010 at 5:28
  • I need to copy some data that I have in a java app into a form that resides into another app (customer name, address, phone number, etc) Commented Mar 20, 2010 at 5:46
  • 3
    There is no safe way to do this because Java can't control other applications. Yes you might be able to use a Robot, but it is not reliable because you can't guarantee that the Notepad application has focus when you invoke the Robot from Java. Commented Mar 20, 2010 at 6:03
  • Why dont you save the data in a text file in one app and read from that very text file from your other app? Commented Mar 20, 2010 at 6:47

1 Answer 1

18

Yes, using the robot is the solution:

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

public class Notepad {

    static int keyInput[] = { KeyEvent.VK_J, KeyEvent.VK_A, KeyEvent.VK_V,
            KeyEvent.VK_A, KeyEvent.VK_SPACE };

    public static void main(String[] args) throws Exception {

        Runtime.getRuntime().exec("notepad");

        Robot robot = new Robot();
        for (int i = 0; i < keyInput.length; i++) {
            robot.keyPress(keyInput[i]);
            robot.delay(100);
        }
    }
}

if you want to convert a String to keyEvents check this question Convert String to KeyEvents

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

1 Comment

Oh, I've got plenty of ideas for uses of this ;)

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.