Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

process - Can a java program "type" into another windows program like notepad

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...