Hey what's up guys. I'm pretty new here and new to programming in general. Unluckily for me I decided to learn java first but I'm trying to make the best of it. So today I'm going to be teaching you how to make a simple bot(I think thats what this is called). You should be familiar with basic Java for this. TheNewBoston on youtube has some great tutorials on a whole bunch of languages/subjects.
First thing first, we need to import the Robot class.
Code:
import java.awt****bot;
Its pretty sweet it can type, move your mouse, and click.. to name a few things. Check out the API for the full list of what it can do. just google robot class java.
Continuing along
Code:
import java.awt****bot;
public class mpghTutorial {
public static void main(String[] args) {
// Next we have to instantiate it
Robot bot = new Robot();
}
}
If you type that into an IDE like eclipse, you're going to see an ominous looking error message. We need to surround it with try/catch statements...
What that basically means is, we're going to try and do something (in this case make a robot and have it do something) and if it can't accomplish this than we handle that exception.
Code:
import java.awt****bot;
public class mpghTutorial {
public static void main(String[] args) {
try{
Robot bot = new Robot();
bot.mouseMove(100, 100);
} catch (AWTException e) {
System.out.println("The robot can't process the command."); // or you can just have e.printStackTrace(); like all the cool kids
}
}
}
"mouseMove" is a method contained in the Robot class and it takes x and y coordinates for its arguments..
So now if you run this you're probably thinking.. wow this is pretty useless.. it just moves my mouse to the corner.. well yes. But you can put it in a loop and have it keep moving around and clicking...
Code:
import java.awt****bot;
import java.awt.event.InputEvent;
public class mpghTutorial {
public static void main(String[] args) {
try{
Robot bot = new Robot();
bot.mouseMove(100, 100);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (AWTException e) {
System.out.println("The robot can't process the command."); //
}
}
}
Woah, did you run that? Now we're getting somewhere. Let me add that BUTTON1 = left click, BUTTON2 = middle button(wheel), and BUTTON3 =
right click.
We're calling the mousePress method to click down, if we stopped there then mr bot would keep holding the button down so we call the mouseRelease method... to uh, release it. Oh you're so smart.
If you get creative and use loops you can make some pretty interesting stuff. I made a program that went through a website and sent friend requests to 7000 + people...
you: what how do I get it to click where I want it to click... Am i supposed to just guess the coordinates?!?
Sigh, just because I'm a nice guy I'll show you how I did it... again, I've only been programming for 1-2 months and there may be more efficient ways to do things... nonetheless here's how I did it.
Theres a class called MouseInfo and its part of the abstract windowing toolkit(awt)... and it can get your mouse location.
Code:
import java.awt.MouseInfo;
public class mpghTutorialBonus {
public static void main(String[] args) {
while(true)
System.out.println(MouseInfo.getPointerInfo().getLocation());
}
}
okay so this will keep running until you terminate the program. So what i did was resize eclipse so it was out of the way but still visible and held my mouse over the location that I wanted to click... i looked over at the console and wrote down the coordinates.
I hope that someone finds this useful and if you have any questions dont hesitate to ask..
Peace!
edit. What the, why is there asterisks?? it's supposed to be
import java [dot] awt [dot] Robot [semicolon]