1 /* Robot.java -- a native input event generator
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import gnu
.java
.awt
.ClasspathToolkit
;
43 import java
.lang
.reflect
.InvocationTargetException
;
44 import java
.awt
.event
.InputEvent
;
45 import java
.awt
.image
.BufferedImage
;
46 import java
.awt
.peer
.RobotPeer
;
49 * The Robot class is used to simulate user interaction with graphical
50 * programs. It can generate native windowing system input events and
51 * retrieve image data from the current screen. Robot is used to test
52 * the AWT and Swing library implementations; it can also be used to
53 * create self-running demo programs.
55 * Since Robot generates native windowing system events, rather than
56 * simply inserting {@link AWTEvents} on the AWT event queue, its use
57 * is not restricted to Java programs. It can be used to
58 * programatically drive any graphical application.
60 * This implementation requires an X server that supports the XTest
63 * @author Thomas Fitzsimmons (fitzsim@redhat.com)
69 private boolean waitForIdle
;
70 private int autoDelay
;
71 private RobotPeer peer
;
74 * Construct a Robot object that operates on the default screen.
76 * @exception AWTException if GraphicsEnvironment.isHeadless()
77 * returns true or if the X server does not support the XTest
79 * @exception SecurityException if createRobot permission is not
82 public Robot () throws AWTException
84 if (GraphicsEnvironment
.isHeadless ())
85 throw new AWTException ("Robot: headless graphics environment");
87 SecurityManager sm
= System
.getSecurityManager ();
89 sm
.checkPermission (new AWTPermission ("createRobot"));
91 ClasspathToolkit tk
= (ClasspathToolkit
) Toolkit
.getDefaultToolkit ();
93 // createRobot will throw AWTException if XTest is not supported.
94 peer
= tk
.createRobot (GraphicsEnvironment
.getLocalGraphicsEnvironment ()
95 .getDefaultScreenDevice ());
99 * Construct a Robot object that operates on the specified screen.
101 * @exception AWTException if GraphicsEnvironment.isHeadless()
102 * returns true or if the X server does not support the XTest
104 * @exception IllegalArgumentException if screen is not a screen
106 * @exception SecurityException if createRobot permission is not
109 public Robot (GraphicsDevice screen
) throws AWTException
111 if (GraphicsEnvironment
.isHeadless ())
112 throw new AWTException ("Robot: headless graphics environment");
114 if (screen
.getType () != GraphicsDevice
.TYPE_RASTER_SCREEN
)
115 throw new IllegalArgumentException ("Robot: graphics"
116 + " device is not a screen");
118 SecurityManager sm
= System
.getSecurityManager ();
120 sm
.checkPermission (new AWTPermission ("createRobot"));
122 ClasspathToolkit tk
= (ClasspathToolkit
) Toolkit
.getDefaultToolkit ();
124 // createRobot will throw AWTException if XTest is not supported.
125 peer
= tk
.createRobot (screen
);
129 * Move the mouse pointer to absolute coordinates (x, y).
131 * @param x the destination x coordinate
132 * @param y the destination y coordinate
134 public void mouseMove(int x
, int y
)
136 peer
.mouseMove (x
, y
);
146 * Press one or more mouse buttons.
148 * @param buttons the buttons to press; a bitmask of one or more of
149 * these {@link InputEvent} fields:
152 * <li>BUTTON1_MASK</li>
153 * <li>BUTTON2_MASK</li>
154 * <li>BUTTON3_MASK</li>
157 * @exception IllegalArgumentException if the button mask is invalid
159 public void mousePress (int buttons
)
161 if ((buttons
& InputEvent
.BUTTON1_MASK
) == 0
162 && (buttons
& InputEvent
.BUTTON2_MASK
) == 0
163 && (buttons
& InputEvent
.BUTTON3_MASK
) == 0)
164 throw new IllegalArgumentException ("Robot: mousePress:"
165 + " invalid button mask");
167 peer
.mousePress (buttons
);
177 * Release one or more mouse buttons.
179 * @param buttons the buttons to release; a bitmask of one or more
180 * of these {@link InputEvent} fields:
183 * <li>BUTTON1_MASK</li>
184 * <li>BUTTON2_MASK</li>
185 * <li>BUTTON3_MASK</li>
188 * @exception IllegalArgumentException if the button mask is invalid
190 public void mouseRelease(int buttons
)
192 if ((buttons
& InputEvent
.BUTTON1_MASK
) == 0
193 && (buttons
& InputEvent
.BUTTON2_MASK
) == 0
194 && (buttons
& InputEvent
.BUTTON3_MASK
) == 0)
195 throw new IllegalArgumentException ("Robot: mouseRelease:"
196 + " invalid button mask");
198 peer
.mouseRelease (buttons
);
208 * Rotate the mouse scroll wheel.
210 * @param wheelAmt number of steps to rotate mouse wheel. negative
211 * to rotate wheel up (away from the user), positive to rotate wheel
212 * down (toward the user).
216 public void mouseWheel (int wheelAmt
)
218 peer
.mouseWheel (wheelAmt
);
230 * @param keycode key to press, a {@link KeyEvent} VK_ constant
232 * @exception IllegalArgumentException if keycode is not a valid key
234 public void keyPress (int keycode
)
236 peer
.keyPress (keycode
);
248 * @param keycode key to release, a {@link KeyEvent} VK_ constant
250 * @exception IllegalArgumentException if keycode is not a valid key
252 public void keyRelease (int keycode
)
254 peer
.keyRelease (keycode
);
264 * Return the color of the pixel at the given screen coordinates.
266 * @param x the x coordinate of the pixel
267 * @param y the y coordinate of the pixel
269 * @return the Color of the pixel at screen coodinates <code>(x, y)</code>
271 public Color
getPixelColor (int x
, int y
)
273 return new Color (peer
.getRGBPixel (x
, y
));
277 * Create an image containing pixels read from the screen. The
278 * image does not include the mouse pointer.
280 * @param screenRect the rectangle of pixels to capture, in screen
283 * @return a BufferedImage containing the requested pixels
285 * @exception IllegalArgumentException if requested width and height
286 * are not both greater than zero
287 * @exception SecurityException if readDisplayPixels permission is
290 public BufferedImage
createScreenCapture (Rectangle screenRect
)
292 if (screenRect
.width
<= 0)
293 throw new IllegalArgumentException ("Robot: capture width is <= 0");
295 if (screenRect
.height
<= 0)
296 throw new IllegalArgumentException ("Robot: capture height is <= 0");
298 SecurityManager sm
= System
.getSecurityManager ();
300 sm
.checkPermission (new AWTPermission ("readDisplayPixels"));
302 int[] pixels
= peer
.getRGBPixels (screenRect
);
304 BufferedImage bufferedImage
=
305 new BufferedImage (screenRect
.width
, screenRect
.height
,
306 BufferedImage
.TYPE_INT_ARGB
);
308 bufferedImage
.setRGB (0, 0, screenRect
.width
, screenRect
.height
,
309 pixels
, 0, screenRect
.width
);
311 return bufferedImage
;
315 * Check if this Robot automatically calls {@link waitForIdle} after
316 * generating an event.
318 * @return true if waitForIdle is automatically called
320 public boolean isAutoWaitForIdle ()
326 * Set whether or not this Robot automatically calls {@link
327 * waitForIdle} after generating an event.
329 * @param isOn true if waitForIdle should be called automatically
331 public void setAutoWaitForIdle (boolean isOn
)
337 * Retrieve the length of time this Robot sleeps after generating an
340 * @return the length of time in milliseconds
342 public int getAutoDelay ()
348 * Set the length of time this Robot sleeps after generating an
351 * @param ms the length of time in milliseconds
353 * @exception IllegalArgumentException if ms is not between 0 and
354 * 60,000 milliseconds inclusive
356 public void setAutoDelay (int ms
)
358 if (ms
<= 0 || ms
>= 60000)
359 throw new IllegalArgumentException ("Robot: delay length out-of-bounds");
365 * Sleep for a specified length of time.
367 * @param ms the length of time in milliseconds
369 * @exception IllegalArgumentException if ms is not between 0 and
370 * 60,000 milliseconds inclusive
372 public void delay (int ms
)
374 if (ms
< 0 || ms
> 60000)
375 throw new IllegalArgumentException ("Robot: delay length out-of-bounds");
381 catch (InterruptedException e
)
383 System
.err
.println ("Robot: delay interrupted");
388 * Wait until all events currently on the event queue have been
391 public void waitForIdle ()
393 if (EventQueue
.isDispatchThread ())
394 throw new IllegalThreadStateException ("Robot: waitForIdle called from "
395 + "the event dispatch thread");
399 EventQueue
.invokeAndWait (new Runnable () { public void run () { } });
401 catch (InterruptedException e
)
403 System
.err
.println ("Robot: waitForIdle interrupted");
405 catch (InvocationTargetException e
)
407 System
.err
.println ("Robot: waitForIdle cannot invoke target");
412 * Return a string representation of this Robot.
414 * @return a string representation
416 public String
toString ()
418 return getClass ().getName ()
419 + "[ autoDelay = " + autoDelay
+ ", autoWaitForIdle = "
420 + waitForIdle
+ " ]";