8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import sun.awt.ExtendedKeyCodes; 27 import sun.awt.SunToolkit; 28 import sun.security.action.GetIntegerAction; 29 30 import java.awt.AWTException; 31 import java.awt.Robot; 32 import java.awt.GraphicsDevice; 33 import java.awt.Toolkit; 34 import java.awt.Point; 35 import java.awt.MouseInfo; 36 import java.awt.event.InputEvent; 37 import java.security.AccessController; 38 import java.security.PrivilegedAction; 39 40 /** 41 * ExtendedRobot is a subclass of {@link java.awt.Robot}. It provides some convenience methods that are 42 * ought to be moved to {@link java.awt.Robot} class. 43 * <p> 44 * ExtendedRobot uses delay {@link #getSyncDelay()} to make syncing threads with {@link #waitForIdle()} 45 * more stable. This delay can be set once on creating object and could not be changed throughout object 46 * lifecycle. Constructor reads vm integer property {@code java.awt.robotdelay} and sets the delay value 47 * equal to the property value. If the property was not set 500 milliseconds default value is used. 48 * <p> 49 * When using jtreg you would include this class via something like: 50 * <pre> 51 * {@literal @}library ../../../../lib/testlibrary 52 * {@literal @}build ExtendedRobot 53 * </pre> 54 * 55 * @author Dmitriy Ermashov 56 * @since 1.9 57 */ 58 293 x += tDx; 294 y += tDy; 295 mouseMove((int)x, (int)y); 296 delay(speed); 297 } 298 299 // Ensure the mouse moves to the right destination. 300 // The steps may have led the mouse to a slightly wrong place. 301 mouseMove(destX, destY); 302 } 303 304 /** 305 * Moves mouse pointer to given screen coordinates. 306 * 307 * @param position Target position 308 * 309 * @see java.awt.Robot#mouseMove(int, int) 310 */ 311 public synchronized void mouseMove(Point position) { 312 mouseMove(position.x, position.y); 313 } 314 315 /** 316 * Successively presses and releases a given key. 317 * <p> 318 * Key codes that have more than one physical key associated with them 319 * (e.g. {@code KeyEvent.VK_SHIFT} could mean either the 320 * left or right shift key) will map to the left key. 321 * 322 * @param keycode Key to press (e.g. {@code KeyEvent.VK_A}) 323 * @throws IllegalArgumentException if {@code keycode} is not 324 * a valid key 325 * 326 * @see java.awt.Robot#keyPress(int) 327 * @see java.awt.Robot#keyRelease(int) 328 * @see java.awt.event.KeyEvent 329 */ 330 public void type(int keycode) { 331 keyPress(keycode); 332 waitForIdle(DEFAULT_SPEED); | 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import sun.awt.ExtendedKeyCodes; 27 import sun.awt.SunToolkit; 28 29 import java.awt.AWTException; 30 import java.awt.Robot; 31 import java.awt.GraphicsDevice; 32 import java.awt.Toolkit; 33 import java.awt.Point; 34 import java.awt.MouseInfo; 35 import java.awt.event.InputEvent; 36 37 /** 38 * ExtendedRobot is a subclass of {@link java.awt.Robot}. It provides some convenience methods that are 39 * ought to be moved to {@link java.awt.Robot} class. 40 * <p> 41 * ExtendedRobot uses delay {@link #getSyncDelay()} to make syncing threads with {@link #waitForIdle()} 42 * more stable. This delay can be set once on creating object and could not be changed throughout object 43 * lifecycle. Constructor reads vm integer property {@code java.awt.robotdelay} and sets the delay value 44 * equal to the property value. If the property was not set 500 milliseconds default value is used. 45 * <p> 46 * When using jtreg you would include this class via something like: 47 * <pre> 48 * {@literal @}library ../../../../lib/testlibrary 49 * {@literal @}build ExtendedRobot 50 * </pre> 51 * 52 * @author Dmitriy Ermashov 53 * @since 1.9 54 */ 55 290 x += tDx; 291 y += tDy; 292 mouseMove((int)x, (int)y); 293 delay(speed); 294 } 295 296 // Ensure the mouse moves to the right destination. 297 // The steps may have led the mouse to a slightly wrong place. 298 mouseMove(destX, destY); 299 } 300 301 /** 302 * Moves mouse pointer to given screen coordinates. 303 * 304 * @param position Target position 305 * 306 * @see java.awt.Robot#mouseMove(int, int) 307 */ 308 public synchronized void mouseMove(Point position) { 309 mouseMove(position.x, position.y); 310 } 311 312 313 /** 314 * Emulate native drag and drop process using {@code InputEvent.BUTTON1_DOWN_MASK}. 315 * The method successively moves mouse cursor to point with coordinates 316 * ({@code fromX}, {@code fromY}), presses mouse button 1, drag mouse to 317 * point with coordinates ({@code toX}, {@code toY}) and releases mouse 318 * button 1 at last. 319 * 320 * @param fromX Source point x coordinate 321 * @param fromY Source point y coordinate 322 * @param toX Destination point x coordinate 323 * @param toY Destination point y coordinate 324 * 325 * @see #mousePress(int) 326 * @see #glide(int, int, int, int) 327 */ 328 public void dragAndDrop(int fromX, int fromY, int toX, int toY){ 329 mouseMove(fromX, fromY); 330 mousePress(InputEvent.BUTTON1_DOWN_MASK); 331 waitForIdle(); 332 glide(toX, toY); 333 mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 334 waitForIdle(); 335 } 336 337 /** 338 * Emulate native drag and drop process using {@code InputEvent.BUTTON1_DOWN_MASK}. 339 * The method successively moves mouse cursor to point {@code from}, 340 * presses mouse button 1, drag mouse to point {@code to} and releases 341 * mouse button 1 at last. 342 * 343 * @param from Source point 344 * @param to Destination point 345 * 346 * @see #mousePress(int) 347 * @see #glide(int, int, int, int) 348 * @see #dragAndDrop(int, int, int, int) 349 */ 350 public void dragAndDrop(Point from, Point to){ 351 dragAndDrop(from.x, from.y, to.x, to.y); 352 } 353 354 /** 355 * Successively presses and releases a given key. 356 * <p> 357 * Key codes that have more than one physical key associated with them 358 * (e.g. {@code KeyEvent.VK_SHIFT} could mean either the 359 * left or right shift key) will map to the left key. 360 * 361 * @param keycode Key to press (e.g. {@code KeyEvent.VK_A}) 362 * @throws IllegalArgumentException if {@code keycode} is not 363 * a valid key 364 * 365 * @see java.awt.Robot#keyPress(int) 366 * @see java.awt.Robot#keyRelease(int) 367 * @see java.awt.event.KeyEvent 368 */ 369 public void type(int keycode) { 370 keyPress(keycode); 371 waitForIdle(DEFAULT_SPEED); |