Can a java program “type” into another windows program like notepad

有些话、适合烂在心里 提交于 2019-12-29 05:34:11

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/2482062/can-a-java-program-type-into-another-windows-program-like-notepad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!