UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

34 lines (33 loc) 4.58 kB
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>The AWT Robot!</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="The AWT Robot!"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-16-SECT-4"/>The AWT Robot!</h1></div></div></div><p><a id="idx10912" class="indexterm"/>This topic may not be of immediate use to everyone, but sometimes an API is just interesting enough that it deserves mentioning. In Java 1.3, a class with the intriguing name <a id="I_indexterm16_id793265" class="indexterm"/><code class="literal">java.awt.Robot</code> was added. The AWT robot provides an API for generating input events such as keystrokes and mouse gestures programmatically. It could be used to build automated GUI testing tools and the like. The following example uses the <a id="I_indexterm16_id793278" class="indexterm"/><code class="literal">Robot</code> class to move the mouse to the upper-left area of the screen and perform a series of events corresponding to a double-click. On most Windows systems, this opens up the <span class="emphasis"><em>My Computer</em></span> folder that lives in that region of the screen.</p><a id="I_16_tt1000"/><pre class="programlisting"> <code class="kd">public</code> <code class="kd">class</code> <code class="nc">RobotExample</code> <code class="o">{</code> <code class="kd">public</code> <code class="kd">static</code> <code class="kt">void</code> <code class="nf">main</code><code class="o">(</code> <code class="n">String</code> <code class="o">[]</code> <code class="n">args</code> <code class="o">)</code> <code class="kd">throws</code> <code class="n">Exception</code> <code class="o">{</code> <code class="n">Robot</code> <code class="n">r</code> <code class="o">=</code> <code class="k">new</code> <code class="n">Robot</code><code class="o">();</code> <code class="n">r</code><code class="o">.</code><code class="na">mouseMove</code><code class="o">(</code><code class="mi">35</code><code class="o">,</code><code class="mi">35</code><code class="o">);</code> <code class="n">r</code><code class="o">.</code><code class="na">mousePress</code><code class="o">(</code> <code class="n">InputEvent</code><code class="o">.</code><code class="na">BUTTON1_MASK</code> <code class="o">);</code> <code class="n">r</code><code class="o">.</code><code class="na">mouseRelease</code><code class="o">(</code> <code class="n">InputEvent</code><code class="o">.</code><code class="na">BUTTON1_MASK</code> <code class="o">);</code> <code class="n">Thread</code><code class="o">.</code><code class="na">sleep</code><code class="o">(</code><code class="mi">50</code><code class="o">);</code> <code class="n">r</code><code class="o">.</code><code class="na">mousePress</code><code class="o">(</code> <code class="n">InputEvent</code><code class="o">.</code><code class="na">BUTTON1_MASK</code> <code class="o">);</code> <code class="n">r</code><code class="o">.</code><code class="na">mouseRelease</code><code class="o">(</code> <code class="n">InputEvent</code><code class="o">.</code><code class="na">BUTTON1_MASK</code> <code class="o">);</code> <code class="o">}</code> <code class="o">}</code></pre><p>In addition to its magic fingers, the AWT robot also has eyes! You can use the <code class="literal">Robot</code> class to capture an image of the screen or a rectangular portion of it by using the <a id="I_indexterm16_id793314" class="indexterm"/><code class="literal">createScreenCapture()</code> method. (Note that you can get the exact dimensions of the screen from the AWT’s <code class="literal">getScreenSize()</code> method.)</p><p>Java 5.0 added a correspondingly useful API, <a id="I_indexterm16_id793335" class="indexterm"/><code class="literal">java.awt.MouseInfo</code>, which allows the gathering of mouse movement information from anywhere on the screen (not restricted to the area within the Java application’s windows). The combination of <code class="literal">Robot</code> and <code class="literal">MouseInfo</code> should make it easier to record and play back events occurring anywhere on the screen from within Java.</p></div></body></html>