Results 1 to 8 of 8
  1. #1
    Senior Member
    Devereux is offline

    Posts
    266

    [Java] Robot Class - Getting Started

    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

    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 reason we have throws AWTException is because it is needed when using the robot class.
    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

    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");
    	}
    }
    :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 :P

    [code]

    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]);
    		}
    	}
    }
    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.

    Credits to: http://java.sun.com/j2se/1.4.2/docs/...awt/Robot.html

    Similar Threads:



  2. #2
    Senior Member
    Catkill666 is offline

    Posts
    698
    Nice post you 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


  3. #3
    Server Administrator
    shadow is offline

    Posts
    790
    good post, but a few typos in the code that prevent it from working. for example the loop never runs because
    Code:
     loop > keys.length
    should have been
    Code:
      loop < keys.length
    anyone who tried this and it didnt work, please try the code below.

    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]);
    		}
    	}
    }



  4. #4
    Senior Member
    Catkill666 is offline

    Posts
    698
    Shows how much attention we were paying


  5. #5
    Administrator
    Montecristoe is online now

    Posts
    5,820
    Is java.awt a premade file made to help work the functions of the program?


  6. #6
    B-A-N-N-E-D
    pipboy is offline

    Posts
    632
    Good guide, will you be the new DIY professor here now?


  7. #7
    Senior Member
    Catkill666 is offline

    Posts
    698
    Quote Originally Posted by Davee View Post
    Is java.awt a premade file made to help work the functions of the program?
    Pretty much abit like the standard C library but for java...awt is mainly for drawing and such


  8. #8
    Senior Member
    Awesomexr is offline

    Posts
    378
    Great tutorial, helped me out a lot while trying to learn java - thanks


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright © 2009-2012 BuyPoe.com All Rights Reserved.

Design Sites:
vBulletin Mods
Facebook Covers
| Free Facebook Covers
Google Covers | Google Plus Covers

Game Sites:
MMORPG
| MW3