开发者

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

开发者 https://www.devze.com 2022-12-23 07:08 出处:网络
is there anyway 开发者_Go百科to type into a notepad.exe process from a JAVA process?Yes, using the robot is the solution:

is there anyway 开发者_Go百科to type into a notepad.exe process from a JAVA process?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消