import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
public class Robot05 {
// Create an array of keycode data
static int keyInput[] = { KeyEvent.VK_H, KeyEvent.VK_E, KeyEvent.VK_L,
KeyEvent.VK_L, KeyEvent.VK_O };// end keyInput array
public static void main(String[] args) throws AWTException, IOException {
// Start the Windows Notepad program running
// in a separate process. It should become
// the active window, capable of accepting
// input from the keyboard.
Process proc = Runtime.getRuntime().exec("notepad");
// Get a Robot object that will be used to
// enter characters into the Notepad document
Robot robot = new Robot();
// Enter the keycodes contained in the
// keyInput array into the Notepad document.
// Make the first character upper case and
// the remaining characters lower case.
robot.keyPress(KeyEvent.VK_SHIFT);
for (int cnt2 = 0; cnt2 < keyInput.length; cnt2++) {
if (cnt2 > 0) {
robot.keyRelease(KeyEvent.VK_SHIFT);
}// end if
robot.keyPress(keyInput[cnt2]);
// Insert a one-half second delay between
// characters.
robot.delay(500);
}// end for loop
}// main
}
直接在任务管理器中杀掉程序就可以了。
你java程序启动notepad后,就可以自己退出结束了。notepad是独立进程,不会受影响,一直自己运行
main方法里Process proc = Runtime.getRuntime().exec("notepad");只保留这一句不就够了吗
你这个代码的效果就是打开记事本,并模拟键盘向记事本里输入Hello。然后整个程序就已经执行完成了,
这个时候java程序已经正常退出了。记事本开着,里面有个Hello
现在要只打开记事本,只留这个不就够了吗?不知道我理解的对不?