Introduction
This includes Java Programs for:
- Typing on computer without physically typing
- Screen capture
- Auto Downloader
- Listing all processes in task manager
- Obtaining and setting Clipboard data
Using the Code
1) Typing on Computer without Physically Typing
The first program opens an editor like Notepad where our program will start typing automatically. This is achieved by:
Runtime.getRuntime().exec("notepad.exe");
Now we make our program type in Notepad. For this, we use the Robot
class:
Thread.sleep(2000);
Robot r=new Robot();
r.keyPress(KeyEvent.VK_T);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_H);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_I);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_S);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_I);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_S);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_F);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_U);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_N);
A starting delay of 2 secs is introduced so that Notepad gets a chance to open and our program writes in Notepad and not anywhere else.
After each letter, we introduced a delay of .5 sec so that it looks like someone is typing.
2) Screen Capture
For this also, we will make use of the Robot
class:
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
File save_path=new File("screen.jpg");
ImageIO.write(img, "JPG", save_path);
Here Dimension
gets your screen size and puts dimension
in the size
variable. createScreenCapture
function takes a screenshot. ImageIO.write
is responsible for writing the screenshot as a JPG file.
3) Auto Downloader
Here is a simple Java program to download a file with Java:
System.out.println("Enter the url to download from:");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String site = input.readLine();
System.out.println("Enter filename");
BufferedReader input1 = new BufferedReader(new InputStreamReader(System.in));
String filename = input.readLine();
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new
java.net.URL(site).openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(filename);
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int i=0;
while((i=in.read(data,0,1024))>=0)
{
bout.write(data,0,i);
}
Here, first you need to give the URL of file to download. Now you have to specify the name by which it will be saved like a .zip. Here in
variable is used to read the data obtained from URL and then this data is written to your system with the help of bout
.
4) Task Manager Processes
It is very easy to list all processes that are active in task manager. Just use this program:
String line;
Process p = Runtime.getRuntime().exec("tasklist.exe");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
Here tasklist.exe gives us all the running processes. We store all results inside a variable input
from which we later list the result to output console.
5) ClipBoard Details
It is very easy to get and set clipboard data. Here is a sample:
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String)t.getTransferData(DataFlavor.stringFlavor);
System.out.println("Current ClipBoard data is:\n"+text);
text="I changed clipboard text";
StringSelection ss = new StringSelection(text);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
System.out.println("Changed ClipBoard data is:\n"+text);
}
This program first retrieves the current clipboard content, shows it and then changes it.
Points of Interest
The Robot
class may also be used to get pixel information and can even simulate mouse click.
The Runtime.getRuntime.exec()
gives the power to Java to execute Windows application directly.
The clipboard program can be enhanced further which can help you make a good desktop app.
History
- 17th July, 2011: Initial version