Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Sep 10, 2001

Using the Java Robot Class


This tip demonstrates a user interaction (mouse move & mouse click) using native Robot methods.

Here's the algorithm of the program:

1. Quickly to move the mouse cursor to the upper left corner.

2. Slowly to move the mouse cursor to the lower left corner and then to stop the cursor on the "Start" button (for a Windows9x/NT/2000-based system).

3. Press this button to show START-menu.

4. Press this button again to close START-menu.
 
import java.awt.*;
public class example
{
 Robot robot=null;

 void _init() {
  try {
   robot = new Robot(); }
  catch (AWTException AEX) { }
 }
 void _move() {
  robot.mouseMove(20,1);
  //screen resolution y=750 {1024x768}, y=585 {800x600}, 
y=460 {640x480};  int y=750;
  //Sets the number of milliseconds this Robot sleeps 
after generating an event.
  robot.setAutoDelay(5);
  //moves the mouse to "Start"
  for (int i=1;i<y;i++)
   { robot.mouseMove(20, i); }
  //presses the left button of the mouse
  robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
  //waits 1 s.
  robot.delay(1000);
  //press the left button of the mouse again
  robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
 }

 public static void main(String[] args)     {
   example ex=new example();
   ex._init();
   ex._move();
   System.exit(0);
  }
}

You can define the screen resolution by using abstract class java.awt.Toolkit.
Sergejs Svitnevs
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date