Nice postyou might wanna do a "try" "catch" that way if the robot isnt
created you can fix it alot easier
Good post though keep it up
Today we will be looking at the Robot class. I find this to be quite fun and is very useful, especially in the making of bots in java
The Robot class makes it possible for the java program to take over the control of the mouse and keyboard input at the operating system level.
(lol so you dont wanna leave it in an infinite loop or it might be a pain to close :P )
So lets get started with an example.
First of all you need the import
java.awt.Robot
We should probably declase a method
The reason we have throws AWTException is because it is needed when using the robot class.Code:import java.awt.*; import java.awt.event.*; import java.io.IOException; public class testRobot{ public static void main(String[] args) throws AWTException{ Robot myRobot = new Robot(); } }
The application will throw that exception if the platform configuration does not allow low-level input control.
Another exception that can be thrown however is SecurityException but I wont go into that.
Ok I did say earlier that you are able to control the mouse and keyboard, so here were go
but lets learn the functions first.
mouseMove(int x,int y) - Moves the mouse pointer to a set of specified absolute screen coordinates given in pixels.
mousePress(int button) - Presses one of the buttons on the mouse.
mouseRelease(int button) - Releases one of the buttons on the mouse.
keyPress(int key) - Presses a specified key on the keyboard.
keyRelease(int key) - Releases specified key on the keyboard.
all the numbers for the buttons and keys are stored in a class so dont worry about that to much. Hey I got a good Idea how about we test it by making it type in notepad
:O we have just opened notepad, and the keys that we want to type are stored in a variable, thats some pretty incredible shit right there :PCode:import java.awt.*; import java.awt.event.*; import java.io.IOException; public class testRobot{ public static void main(String[] args) throws AWTException{ Static int keys = { KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_V, KeyEvent.VK_E, KeyEvent.VK_R, KeyEvent.VK_E, KeyEvent.VK_U, KeyEvent.VK_X } //Spells devereux :D Runtime.getRuntime().exec("notepad"); } }
[code]
Guess what that will do, it will first hold down shift, but after the first character is typed it releases shift and continues the typing. There is also about a half second delay in pressing and releasing of the keys just so that it has time to press it properly.Code:import java.awt.*; import java.awt.event.*; import java.io.IOException; public class testRobot{ public static void main(String[] args) throws AWTException{ Static int keys = { KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_V, KeyEvent.VK_E, KeyEvent.VK_R, KeyEvent.VK_E, KeyEvent.VK_U, KeyEvent.VK_X } //Spells devereux :D Runtime.getRuntime().exec("notepad"); Robot myRobot = new Robot(); myRobot.keyPress(KeyEvent.VK_SHIFT); for(int loop = 0; loop < keys.length; loop++){ if(loop > 0){ myRobot.keyRelease(KeyEvent.VK_SHIFT); } myRobot.KeyPress(keys[loop]); myRobot.delay(500); myRobot.KeyRelease(keys[loop]); } } }
Credits to: http://java.sun.com/j2se/1.4.2/docs/...awt/Robot.html
Similar Threads:
Nice postyou might wanna do a "try" "catch" that way if the robot isnt
created you can fix it alot easier
Good post though keep it up
good post, but a few typos in the code that prevent it from working. for example the loop never runs becauseshould have beenCode:loop > keys.lengthanyone who tried this and it didnt work, please try the code below.Code:loop < keys.length
Code:import java.awt.*; import java.awt.event.*; import java.io.IOException; public class TestRobot{ public static void main(String[] args) throws AWTException{ int[] keys = { KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_V, KeyEvent.VK_E, KeyEvent.VK_R, KeyEvent.VK_E, KeyEvent.VK_U, KeyEvent.VK_X }; //Spells devereux :D try{ Runtime.getRuntime().exec("notepad"); } catch(IOException e) { e.printStackTrace(); } Robot myRobot = new Robot(); myRobot.keyPress(KeyEvent.VK_SHIFT); for(int loop = 0; loop < keys.length; loop++){ if(loop > 0){ myRobot.keyRelease(KeyEvent.VK_SHIFT); } myRobot.keyPress(keys[loop]); myRobot.delay(500); myRobot.keyRelease(keys[loop]); } } }
Is java.awt a premade file made to help work the functions of the program?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Design Sites:
vBulletin Mods
Facebook Covers | Free Facebook Covers
Google Covers | Google Plus Covers
Game Sites:
MMORPG | MW3