Java: The Robot class - How to move the mouse cursor and type keys automatically from your Java program?
Want to simulate the various actions that the user can perform with a mouse like mouse pressed, mouse released etc.? or simulate various keyboard actions like key pressed, key released etc.? The Java class - java.awt.Robot is your answer. Read further to see a full demo of the various features this class has to offer.
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 GUIGraphical User Interface - The visual front-end of your application through which the user interacts with your application. Check out this Swing Java tutorial for basic GUI programming. 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.
- Simulate Mouse move: To move the mouse over a text field - textField
robot.mouseMove(locOnScreen.x, locOnScreen.y);
- Simulate Mouse button press and release at the current mouse pointer position
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.
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
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_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);








Comments
can any try for symbol like
can any try for symbol like +,~
in my pc its not workin for
in my pc its not workin for above symbol
I have a solution for your
I have a solution for your problem. I have posted it as a forum post. Please check it at http://www.1your.com/drupal/node/155
bnatey
توبيكات
توبيكات توبيكات
توبيكات اسلاميه
توبيكات اسلاميه
توبيكات حزينه
توبيكات حب
توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني
توبيكات منوعه
توبيكات مضحكه
توبيكات عتاب
توبيكات عتاب
توبيكات ساخره
توبيكات شعريه
توبيكات اجنبيه
توبيكات جديده
توبيكات بنات
توبيكات 2010
توبيكات اعياد
توبيكات عيد الاضحى
توبيكات مناسبات
توبيكات اعياد ميلاد
توبيكات رأس السنه
توبيكات كريسماس
توبيكات توبيكات
توبيكات كرة قدم
توبيكات كأس العالم
توبيكات مصر
توبيكات إلا رسول الله
توبيكات الاهلي
توبيكات الزمالك
توبيكات
توبيكات
توبيكات اسلاميه
توبيكات حزينه
توبيكات حب
توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني
توبيكات منوعه
توبيكات مضحكه
توبيكات عتاب
توبيكات ساخره
توبيكات شعريه
توبيكات اجنبيه
توبيكات نكت
توبيكات جوال
توبيكات سعوديه
توبيكات شخصيات فنيه
توبيكات ليلة العمر
توبيكات
توبيكات
توبيكات فلاش
توبيكات روعه
توبيكات رائعه
منتديات
دردشة
العاب
العاب بنات
شات
توبيكات مصريه
توبيكات قطريه
دليل مواقع
تحميل صور
مركز تحميل
برامج
كأس العالم
كأس العالم 2010
العاب
العاب بنات
منتدي
فيديو بنات
برامج
مركز رفع صور
شات
دليل المواقع العربيه
العاب
العاب
دليل مواقع
توبيكات
كتب
مقالات
صور
منتدى أجنبي
منتديات
thankes
trading forex
forex rates forex rates
forex exchange forex exchange
finanzas forex
forex trade
forex online
forexfactory
traderforex
forexmarket
forex broker
forex broker
thankes
trading forex
forex rates forex rates
forex exchange forex exchange
finanzas forex
forex trade
forex online
forexfactory
traderforex
forexmarket
forex broker
forex broker
Post new comment