skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, September 17, 2012

Java Robot Example

Posted by Admin at 10:48 AM – 0 comments
 

On executing this program, the system will automatically search for the keyword that is passed in the java file and open that program in the system. For example this program will open notepad direclty on executing if notepad is the keywor passed.


import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
public class RobotExample {
 public static void main(String[] args) throws Exception{
  String str = "Hie Sam";
  Robot r = new Robot();

  r.mouseMove(20, 890);
  r.mousePress(InputEvent.BUTTON1_MASK);
  r.delay(20);
  r.mouseRelease(InputEvent.BUTTON1_MASK);
  typeKeys("Notepad",r);
  r.delay(50);
  r.keyPress(KeyEvent.VK_ENTER);
  r.delay(20);
  typeKeys(str, r);


 }
 public static void typeKeys(String str,Robot r)
 {
  for(int i=0;i<str.length();i++)
  {
   typeCharacter(r, ""+str.charAt(i));
  }
 }
 public static void typeCharacter(Robot robot, String letter)
 {
  try
  {
   boolean upperCase = Character.isUpperCase( letter.charAt(0) );
   String variableName = "VK_" + letter.toUpperCase();

   Class clazz = KeyEvent.class;
   Field field = clazz.getField( variableName );
   int keyCode = field.getInt(null);

   robot.delay(1000);

   if (upperCase) robot.keyPress( KeyEvent.VK_SHIFT );

   robot.keyPress( keyCode );
   robot.keyRelease( keyCode );

   if (upperCase) robot.keyRelease( KeyEvent.VK_SHIFT );
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
}


Labels: Robot Class

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger