The Robot class Demo - How to move the mouse cursor and type keys automatically from your Java program? - Full Java Code
The Robot class Demo - How to move the mouse cursor and type keys automatically from your Java program? - Full Java Code
This article is part of a tutorial.
Tutorial Index page - Java Keyboard and Mouse Event Handling
/** * $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; } }
Feedback or Questions?
We welcome feedback and questions and will try our best to attend to it as quickly as possible!
Please note that you would have to register before you can post in our forums and this is purely to guard us from the spam-bots. Be assured that we do not send spam mails and our website registration only takes minutes.
