Simple bot in Java using Robot class

Hi, today we are learning about how to build a bot using Robot class in Java. Robot Class is a package of JavaAWT(For more details on Robot Class Click here).

Using this we will be automating the launching of notepad and greeting the user.

So let’s discuss pseudo-code and logic to build this simple bot.

First, we will get the information about the os of the system according to which we will execute command for that we will code a snippet.

Sting os = System.getProperty("os.name").toString();
if(os.equals("Linux")){
//then command will be according to the Linux.
}
else if(os.startsWith("Windows")){//here we will use starts with because windows have so many versions like Windows XP/Vista/10/8/7 etc.
//then command will be according to the Windows.
}

Next, we have to execute the command required to launch the notepad according to the os of the system. For that, we require a Runtime class object so let’s code for it.

Runtime runtime = Runtime.getRuntime();
runtime.exec(cmd); // Here cmd is the string which stores the command to launch the notepad

After launching the notepad we just need to write the text on the notepad using Robot class. For that, we are using StringSelection and Clipboard class because there is no method or function available to type a string through a single function/method call.

So we are using Clipboard to copy text string in the system and using robot class we will paste it on the notepad( Using the cntrl+v command).

String t="Hello From Codespeedy\nWe hope your doing well";
StringSelection stringSelection=new StringSelection(t);
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
clip.setContents(stringSelection, stringSelection);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);

So these are the logics and code snippets to build this Bot.

( Note: Do not execute this code in a loop if you’re executing it in a loop then the loop must end after executing 5 to 6 because opening multiple notepad tabs in a very short amount of time might cause damage to the system this blog is for education purpose only. )

Complete Code

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.*;

public class Simplebot {
    public static void main(String[] args) throws IOException,AWTException,InterruptedException{
        String cmd="";
        String os=System.getProperty("os.name").toString();
        if(os.equals("Linux")){
            cmd="gedit";
        }
        else if(os.startsWith("Windows")){
            cmd="notepad";
        }
        Runtime runtime = Runtime.getRuntime();
        runtime.exec(cmd);
        Thread.sleep(2000);
        Robot rb=new Robot();
        String t=" Hello From Codespeedy \n We hope you are doing well";
        StringSelection stringSelection=new StringSelection(t);
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        clip.setContents(stringSelection, stringSelection);
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_V);
        rb.keyRelease(KeyEvent.VK_CONTROL);

    }
}

 

Thank you for reading this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *