is there anyway to type into a notepad.exe process from a JAVA process?
Asked
Active
Viewed 6,564 times
4
-
Please let know why do you want to do that, so that we can answer the question in the light of that knowledge. – amit kumar Mar 20 '10 at 05:28
-
I need to copy some data that I have in a java app into a form that resides into another app (customer name, address, phone number, etc) – Pablo Mar 20 '10 at 05:46
-
3There is no safe way to do this because Java can't control other applications. Yes you might be able to use a Robot, but it is not reliable because you can't guarantee that the Notepad application has focus when you invoke the Robot from Java. – camickr Mar 20 '10 at 06:03
-
Why dont you save the data in a text file in one app and read from that very text file from your other app? – Zaki Mar 20 '10 at 06:47
1 Answers
18
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