How to Sendkeys to an application in Java using the Robot Class
For any of you that have used Visual Basic, you will probably know about SendKeys.
SendKeys is basically a way of sending keystrokes to an application.
Java now has this ability thanks to the Robot Class.
This code will show you how to send keystrokes to an application. In this case, Notepad.exe with a delay between each key.
The Robot class can do a lot more so its worth looking into!
Code Java:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
public class Robot1{
static int keyInput[] =
{
KeyEvent.VK_J, KeyEvent.VK_A, KeyEvent.VK_V, KeyEvent.VK_A, KeyEvent.VK_SPACE,
KeyEvent.VK_P, KeyEvent.VK_R, KeyEvent.VK_O, KeyEvent.VK_G, KeyEvent.VK_R,
KeyEvent.VK_A, KeyEvent.VK_M, KeyEvent.VK_M, KeyEvent.VK_I, KeyEvent.VK_N,
KeyEvent.VK_G, KeyEvent.VK_SPACE, KeyEvent.VK_F, KeyEvent.VK_O, KeyEvent.VK_R,
KeyEvent.VK_U, KeyEvent.VK_M, KeyEvent.VK_S, KeyEvent.VK_SPACE, KeyEvent.VK_PERIOD,
KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_M
};
public static void main(String[] args) throws AWTException,IOException {
Runtime.getRuntime().exec("notepad");
Robot robot = new Robot();
for (int i = 0; i < keyInput.length; i++)
{
robot.keyPress(keyInput[i]);
robot.delay(100);
}
}
}
Output:
http://www.javaprogrammingforums.com...eys_output.bmp
Try it for yourself. Its cool...
Re: How to Sendkeys to an application in Java using the Robot Class
wow, does the Robot do everything short of key logging and injecting DLL's into executables?
hahaha
Re: How to Sendkeys to an application in Java using the Robot Class
ei this is one is cool ,
anyway sir java , what other kind of executable file can i use with this method..?
in a console output.. how should i code it?
Re: How to Sendkeys to an application in Java using the Robot Class
SendKeys passes the keys to the currently active window and then they are proccessed as if the user typed them. So you can send them to any application, and you can change focuses too.
Chris
Re: How to Sendkeys to an application in Java using the Robot Class
Re: How to Sendkeys to an application in Java using the Robot Class
wow, its superb dude :)
i am searching in Google for a JAVA code to implement this feature only and finally i got it :o
Re: How to Sendkeys to an application in Java using the Robot Class
I am using MAC OS. So, how will i do to open in BBEdit or TextEdit as there is no notepad. Also I tried by replacing "notepad" with "BBedit" but displayed error as:
Exception in thread "main" java.io.IOException: Cannot run program "BBEdit": error=2, No such file or directory