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

后端 未结 1 1556
天命终不由人
天命终不由人 2020-12-15 14:32

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

相关标签:
1条回答
  • 2020-12-15 14:47

    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 讨论(0)
提交回复
热议问题