About Me

PhD, NET(UGC), MBA (Finance), M.com (Finance), B.COM (professional), B.Ed (Commerce + English), DIM, PGDIM, PGDIFM, NIIT Accounting package...

Wednesday, January 12, 2011

JAWA How to move the mouse cursor and type keys automatically from your Java program?

Introduction

This article demonstrates the various functionality offered by the java.awt.Robot class. From the Java API documentation "This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.

Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events.

Note that some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an AWTException will be thrown when trying to construct Robot objects. For example, X-Window systems will throw the exception if the XTEST 2.2 standard extension is not supported (or not enabled) by the X server.

Applications that use Robot for purposes other than self-testing should handle these error conditions gracefully. "

Application

  • Automated testing: As the Java API docs said, the primary intended usage for this class is to support automated testing. For example your Java program could do certain things on a mouse click on a JButton or a key release in a JTextField. When it comes to automated testing of this Java program, you can use this Robot class to simulate these user actions in a way that will be as close as possible to a real user action.
  • Demonstration: May be you want to demonstrate a particular GUI app which involves user interactions...

Features

The following are the key features/functionality offered by this Robot class.
  • Simulate/perform the following mouse events.
    • Mouse Move - Moves the mouse pointer to a given screen's coordinates.
    • Mouse press - Presses the specified mouse button at the mouse pointer's current location.
    • Mouse release - Releases the specified mouse button at the mouse pointer's current location.
    • Mouse wheel - Rotates the mouse scroll wheel to a given amount.
  • Simulate/perform the following keyboard events.
    • Key Press - Press a specified key in the keyboard.
    • Key Release - Release a specified key in the keyboard.
  • Simulate/perform a screen capture - "Creates an image containing pixels read from the screen. This image does not include the mouse cursor."
  • Get pixel color - Gets the color of the pixel at a given screen co-ordinate.

Usage

  • Instantiation: The first step to do in order to use this class's features to instantiate it. Instantiation is straight forward and you can use the default parameterless constructor.
Robot robot = new Robot();
  • Simulate Mouse move: To move the mouse over a text field - textField
Point locOnScreen = textField.getLocationOnScreen();
robot.mouseMove(locOnScreen.x, locOnScreen.y);
  • Simulate Mouse button press and release at the current mouse pointer position
// Press and release mouse button 1
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
 
// Press and release mouse button 2
    robot.mousePress(InputEvent.BUTTON2_MASK);
    robot.mouseRelease(InputEvent.BUTTON2_MASK);
  • Simulate Keyboard key presses and releases.
// Types the word 'DEMO'
    robot.keyPress(KeyEvent.VK_D);
    robot.keyRelease(KeyEvent.VK_D);
 
    robot.keyPress(KeyEvent.VK_E);
    robot.keyRelease(KeyEvent.VK_E);
 
    robot.keyPress(KeyEvent.VK_M);
    robot.keyRelease(KeyEvent.VK_M);
 
    robot.keyPress(KeyEvent.VK_O);
    robot.keyRelease(KeyEvent.VK_O);
Note: These methods could be seen as dumb methods which does exactly what you asked it to do. For example if you try to simulate a key press for the character ':' - colon like
robot.keyPress(KeyEvent.VK_COLON)
and in you keyboard the only way you can manually type a colon is to hold shift and press semi-colon ';', then the above line would throw an IllegalArgumentException. The correct way to simulate these characters is to exactly do what can be done with the connected keyboard i.e.
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
 
Full Java Code
 
/**
 *  $Id: UsingTheRobotClass.java 103 2010-03-14 01:18:55Z oneyour $
 * 
 * This is an accompanying program for the article
 * <a href="http://www.1your.com/drupal/fulljavacoderobotclassdemo
" title="http://www.1your.com/drupal/fulljavacoderobotclassdemo
">http://www.1your.com/drupal/fulljavacoderobotclassdemo
</a> * 
 * Copyright (c) 2009 - 2010 <a href="http://www.1your.com" title="www.1your.com">www.1your.com</a>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of <a href="http://www.1your.com" title="www.1your.com">www.1your.com</a> nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 
/**
 * A Java  program that demonstrates the functionality offered by the Robot
 * class.
 */
 
public class UsingTheRobotClass 
{
 
 private JTextField textField = null;
 private JTextArea messageArea = null;
 
 public UsingTheRobotClass() 
 {
  JFrame loginScreen = creatFrame();
  loginScreen.setVisible(true);
 
  try 
  {
   Robot robot = new Robot();
 
   messageArea.append("In 5 seconds the Mouse will move over the Text Field...\n");
   robot.delay(5000);
   Point locOnScreen = textField.getLocationOnScreen();
   robot.mouseMove(locOnScreen.x, locOnScreen.y);
   robot.delay(1000);
   messageArea.append("In 5 seconds the Mouse will move to the top left corner of the screen...\n");
   robot.delay(5000);
   robot.mouseMove(0, 0);
 
   robot.delay(1000);
   messageArea.append("In 5 seconds the Mouse will move over the Text Field and perform a LEFT click...\n");
   robot.delay(5000);
   locOnScreen = textField.getLocationOnScreen();
   robot.mouseMove(locOnScreen.x, locOnScreen.y);
   robot.mousePress(InputEvent.BUTTON1_MASK);
   robot.delay(1000);
   robot.mouseRelease(InputEvent.BUTTON1_MASK);
 
   robot.delay(1000);
   messageArea.append("In 5 seconds the Mouse will perform a RIGHT click...\n");
   robot.delay(5000);
   robot.mousePress(InputEvent.BUTTON2_MASK);
   robot.delay(1000);
   robot.mouseRelease(InputEvent.BUTTON2_MASK);
 
   robot.delay(1000);
   messageArea.append("In 5 seconds the word 'demo:' will be typed...\n");
   robot.delay(5000);
 
   robot.keyPress(KeyEvent.VK_D);
   robot.keyRelease(KeyEvent.VK_D);
 
   robot.keyPress(KeyEvent.VK_E);
   robot.keyRelease(KeyEvent.VK_E);
 
   robot.keyPress(KeyEvent.VK_M);
   robot.keyRelease(KeyEvent.VK_M);
 
   robot.keyPress(KeyEvent.VK_O);
   robot.keyRelease(KeyEvent.VK_O);
 
   robot.keyPress(KeyEvent.VK_SHIFT);
   robot.keyPress(KeyEvent.VK_SEMICOLON);
   robot.keyRelease(KeyEvent.VK_SEMICOLON);
   robot.keyRelease(KeyEvent.VK_SHIFT);
 
   robot.delay(1000);
   messageArea.append("End of demo\n");
  } 
  catch (AWTException exception) 
  {
   String errorString = "Platform configuration does not allow low-level input control. Exiting the demo in 5 seconds."; 
   System.err.println(errorString);
   messageArea.append(errorString);
 
   try 
   {
    Thread.sleep(5000);
   } 
   catch (InterruptedException e) {/* Do nothing */}
   System.exit(1);
  }
 }
 
 /**
  * The entry point of this program
  */
 public static void main(String[] args) 
 {
  new UsingTheRobotClass();
 }
 
 public JFrame creatFrame()
 {
  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setTitle("Robot Class Demo");
 
  JPanel contentPane = (JPanel)frame.getContentPane();
  contentPane.setLayout(new BorderLayout(10,10));
 
  contentPane.add(createTextField(), BorderLayout.NORTH);
 
  contentPane.add(createStatusArea(), BorderLayout.CENTER);
 
  contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  frame.setSize(600,500);
 
  // Center the JFrame in the screen
  frame.setLocationRelativeTo(null);
 
  return frame;
 
 }
 
 private JTextField createTextField()
 {
  textField = new JTextField(30);
  textField.setToolTipText("This is a JTextField");
  textField.addMouseListener(new MouseAdapter()
  {
    public void mouseEntered(MouseEvent e) 
    {
     messageArea.append("\t--> Mouse Entered. Do you see tool tip?\n");
    }
    public void mouseExited(MouseEvent e) 
    {
     messageArea.append("\t--> Mouse Exited. Tool tip disappeared?\n");
    }
    public void mouseClicked(MouseEvent e) 
    {
     messageArea.append("\t--> Mouse " + getMouseButton(e.getButton()) + " Clicked.\n");
 
    }
    public void mousePressed(MouseEvent e) 
    {
     messageArea.append("\t--> Mouse " + getMouseButton(e.getButton()) + " Pressed.\n");
    }
    public void mouseReleased(MouseEvent e) 
    {
     messageArea.append("\t--> Mouse " + getMouseButton(e.getButton()) + " Released.\n");
    }
  });
  return textField;
 }
 
 private String getMouseButton(int button)
 {
   String buttonText;
 
   switch(button)
   {
    case MouseEvent.BUTTON1:
     buttonText = "Button 1";
     break;
 
    case MouseEvent.BUTTON2:
     buttonText = "Button 2";
     break;
 
    case MouseEvent.BUTTON3:
     buttonText = "Button 3";
     break;
 
    default:
     buttonText = "Unknown Button";
   }
 
   return buttonText;
 
 }
 
 private JScrollPane createStatusArea()
 {
  messageArea = new JTextArea();
  JScrollPane scrollPane = new JScrollPane(messageArea);
  textField.setToolTipText("This is a JTextArea");
 
  return scrollPane;
 }
 
}

No comments:

Post a Comment

Need Conflict

Need Conflict --- #### **Introduction to Need Conflict** - **Definition:** Need conflict occurs when an individual experiences competing des...