<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ryan's Blog</title>
	<atom:link href="http://webbryan.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://webbryan.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Thu, 30 Oct 2008 23:24:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='webbryan.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ryan's Blog</title>
		<link>http://webbryan.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://webbryan.wordpress.com/osd.xml" title="Ryan&#039;s Blog" />
	<atom:link rel='hub' href='http://webbryan.wordpress.com/?pushpress=hub'/>
		<item>
		<title>java.awt.Robot: a simple tutorial</title>
		<link>http://webbryan.wordpress.com/2008/10/30/simple-javaawtrobot-tutorial/</link>
		<comments>http://webbryan.wordpress.com/2008/10/30/simple-javaawtrobot-tutorial/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 09:04:56 +0000</pubDate>
		<dc:creator>webbryan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[controlling computer using java]]></category>
		<category><![CDATA[java.awt.Robot]]></category>
		<category><![CDATA[Robot]]></category>

		<guid isPermaLink="false">http://webbryan.wordpress.com/?p=3</guid>
		<description><![CDATA[Hello!  I have a very simple and friendly sample program here of using java.awt.Robot of Java API. The code I have written pretty much explains the basic concepts you need to know about Robot class,  I have included Java Docs, and my comments in the source code.  Of course, for your learning pleasure. Basically I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webbryan.wordpress.com&amp;blog=5348636&amp;post=3&amp;subd=webbryan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hello!  I have a very simple and friendly sample program here of using java.awt.Robot of Java API.<br />
The code I have written pretty much explains the basic concepts you need to know about Robot class,  I have included Java Docs, and my comments in the source code.  Of course, for your learning pleasure.</p>
<p>Basically I have 3 classes here,  HappyHalloween class,  Keyboard class,  and the Mouse class.<br />
Keyboard class and Mouse class are classes inside HappyHalloween class.  And you may use these 2 classes for your own app (like doing some overnight testings or whatever your imagination leads you)</p>
<p>Keyboard and Mouse classes are utilizing the Robot class (of java.awt package)</p>
<p>Here&#8217;s the source code:<br />
////////////////////////////////////////////////////////////////////////<br />
<code><br />
/*<br />
*	Happy Halloween!<br />
*<br />
*	A spooky software for the Halloween!<br />
*<br />
*  Scare your work mate, friends and classmate with this simple<br />
*  friendly java code.<br />
*  Also provides a simple demonstration about Java controlling<br />
*	your desktop natively.<br />
*<br />
*	This may be compiled using javac HappyHalloween.java and run using java HappyHalloween<br />
*	OR<br />
*	Through Eclipse / Netbeans,<br />
*<br />
*<br />
*	(c)2008 Ryan G. Webb 2008/10/30 (just in time for Halloween!)<br />
*	The author of this code is not liable for any damage this may cause.<br />
*	Please include this last paragraph if you happen to use my Keyboard and Mouse Class.<br />
*/</code></p>
<p>import java.awt.AWTException;<br />
import java.awt.event.InputEvent;<br />
import java.awt.event.KeyEvent;<br />
import java.awt.Robot;<br />
import javax.swing.JOptionPane;</p>
<p>/**<br />
*<br />
*	@author Ryan G. Webb 2008/10/30<br />
*/<br />
public class HappyHalloween {</p>
<p>/**<br />
*	Class Keyboard<br />
*	Emulates the typing process of a Physical Keyboard.<br />
*<br />
*	@author Ryan G. Webb 2008/10/30<br />
*/<br />
private static class Keyboard {</p>
<p>private Robot robot;<br />
private int typeSpeed;</p>
<p>/**<br />
*	Emulate the Pressing of a Physical keyboard.<br />
*  Letters are from 0&#215;41 ~ 0x5A (Hex) equivalent to regular A ~ Z (ASCII)<br />
*	The Windows(TM) key on keyboard is 0x020c.</p>
<p>*	For more information see JDK API on KeyEvent Topics AND/OR Refer to ASCII CHART<br />
*<br />
*	@param letter array of primitive ints that represent keys of a keyboard.<br />
*	@author Ryan G. Webb 2008/10/30<br />
*/<br />
private void type(int[] letter) throws AWTException {<br />
try {<br />
robot = new Robot();<br />
int len = letter.length;</p>
<p>//* If there&#8217;s a shift / alt key.<br />
for (int i=0; i<br />
robot.delay(this.getTypeSpeed());<br />
if (letter[i] == 0&#215;10 || letter[i] == 0&#215;12) { //* alt = 0&#215;12; shift = 0&#215;10<br />
robot.keyPress(letter[i]);     			 //* Press Shift/Alt<br />
robot.keyPress(letter[++i]);              //* Press Key<br />
robot.keyRelease(letter[i]);              //* Release Key<br />
robot.keyRelease(letter[--i]);            //* Release Shift/Alt<br />
i++;<br />
} else {<br />
robot.keyPress(letter[i]);<br />
robot.keyRelease(letter[i]);<br />
}<br />
}<br />
} finally {<br />
robot = null;<br />
}<br />
}</p>
<p>/**<br />
*	@return typeSpeed a primitive int used to represent typing speed in ms.<br />
*<br />
*	@author Ryan G. Webb 2008/10/30<br />
*/<br />
private int getTypeSpeed() {<br />
return this.typeSpeed;<br />
}</p>
<p>/**<br />
*	@param typeSpeed a primitive int used to represent typing speed in ms.<br />
*<br />
*	@author Ryan G. Webb 2008/10/30<br />
*/<br />
private void setTypeSpeed(int typeSpeed) {<br />
this.typeSpeed = typeSpeed;<br />
}<br />
}</p>
<p>/**<br />
*	Class Mouse<br />
*	Emulate the functions of a physical mouse<br />
*<br />
*	@author Ryan G. Webb 2008/10/30<br />
*/<br />
private static class Mouse {</p>
<p>private Robot robot;<br />
private int scrollSpeed;<br />
private static int xStart = 613;<br />
private static int yStart = 484;</p>
<p>/**<br />
*	Sets the mouse pointer at center screen, then moves to specified position<br />
*	using the X and Y Coordinates.<br />
*<br />
*	@param x a primitive int that represents position in relevance to vertical<br />
*	@param y a primitive int that represents position in relevance to horizontal<br />
*	@param click a primitive boolean that tells if a click action is necessary (true=click action)<br />
*<br />
*	@author Ryan G. Webb 2008/10/30<br />
*/<br />
private void move(int x, int y, boolean click) throws AWTException {<br />
try {<br />
robot = new Robot();<br />
//* Initialize starting position estimated: center of screen<br />
robot.mouseMove(xStart, yStart);<br />
while (xStart != x || yStart != y) {</p>
<p>int xAxis, yAxis;<br />
if (xStart &lt; x) {<br />
xAxis = ++xStart;<br />
} else if (xStart &gt; x){<br />
xAxis = &#8211;xStart;<br />
} else {<br />
xAxis = xStart;<br />
}</p>
<p>if (yStart &lt; y) {<br />
yAxis = ++yStart;<br />
} else if(yStart &gt; y){<br />
yAxis = &#8211;yStart;<br />
} else {<br />
yAxis = yStart;<br />
}</p>
<p>robot.mouseMove(xAxis, yAxis);<br />
robot.delay(this.getScrollSpeed());<br />
}</p>
<p>if (click)<br />
leftClick();<br />
} finally {<br />
robot = null;<br />
}<br />
}</p>
<p>/**<br />
*	Provides a simulation of a mouse left click.<br />
*<br />
*	@author Ryan G. Webb 2008/10/30<br />
*/<br />
private void leftClick() throws AWTException, IllegalArgumentException {<br />
try {<br />
robot = new Robot();<br />
robot.mousePress(InputEvent.BUTTON1_MASK);<br />
robot.mouseRelease(InputEvent.BUTTON1_MASK);<br />
} finally {<br />
robot = null;<br />
}<br />
}</p>
<p>/**<br />
*	@return scrollSpeed a primitive int used to represent scroll speed in ms.<br />
*<br />
*	@author Ryan G. Webb 2008/10/30<br />
*/<br />
private int getScrollSpeed() {<br />
return this.scrollSpeed;<br />
}</p>
<p>/**<br />
*	@param scrollSpeed a primitive int used to represent scroll speed in ms.<br />
*<br />
*	@author Ryan G. Webb 2008/10/30<br />
*/<br />
private void setScrollSpeed(int scrollSpeed) {<br />
this.scrollSpeed = scrollSpeed;<br />
}<br />
}</p>
<p>/*<br />
*	Main Program for running Happy Halloween class<br />
*/<br />
public static void main(String[] args) {<br />
int[] executeNotepad = { 0x020C, 0&#215;52, 0x4E, 0x4F, 0&#215;54, 0&#215;45, 0&#215;50, 0&#215;41, 0&#215;44, KeyEvent.VK_ENTER };<br />
int[] maximize = { 0&#215;12, 0&#215;20, 0&#215;58 };<br />
int[] message = { 0&#215;10, 0&#215;48, 0&#215;41, 0&#215;50, 0&#215;50, 0&#215;59, 0&#215;20, 0&#215;10, 0&#215;48, 0&#215;41, 0x4C, 0x4C, 0x4F, 0&#215;57, 0&#215;45, 0&#215;45, 0x4E, 0&#215;10, 0&#215;31 };<br />
Keyboard k = new Keyboard();<br />
Mouse m = new Mouse();</p>
<p>try {<br />
k.setTypeSpeed(500);<br />
k.type(executeNotepad);<br />
k.type(maximize);<br />
k.type(message);</p>
<p>m.setScrollSpeed(2);<br />
m.move(11, 35, true);  //* moves to File then click<br />
m.move(40, 104, true); //* moves to Save As then click<br />
} catch (AWTException e) {<br />
//* Display a message box telling the user what happened<br />
JOptionPane.showMessageDialog(null, e.getMessage(), &#8220;Happy Halloween!&#8221;, JOptionPane.ERROR_MESSAGE);<br />
} catch (IllegalArgumentException e) {<br />
//* Display a message box telling the user what happened<br />
JOptionPane.showMessageDialog(null, e.getMessage(), &#8220;Happy Halloween!&#8221;, JOptionPane.ERROR_MESSAGE);<br />
}<br />
}<br />
}</p>
<p>////////////////////////////////////////////////////////////////////////</p>
<p>Don&#8217;t worry if you see some Hex code like 0&#215;10 it means Shift (I believe I put that as comment).  I used Hex because I think it looks cool!<br />
0&#215;41 &#8211; A<br />
0&#215;42 &#8211; B<br />
.<br />
.<br />
.<br />
And so on&#8230;(Refer to java.awt.event.KeyEvent for code constants)</p>
<p>you may also put a char literal instead of a hex code like this:</p>
<p>int[] message = { 0&#215;10, &#8216;H&#8217;, &#8216;A&#8217;, &#8216;P&#8217;, &#8216;P&#8217;, &#8216;Y&#8217;, 0&#215;20, 0&#215;10, &#8216;H&#8217;, &#8216;A&#8217;, &#8216;L&#8217;, &#8216;L&#8217;, &#8216;O&#8217;, &#8216;W&#8217;, &#8216;E&#8217;, &#8216;E&#8217;, &#8216;N&#8217;, 0&#215;10, &#8217;1&#8242; };</p>
<p>make sure you type UPPERCASE char literal lowercase literals doesn&#8217;t work!  So put a 0&#215;10 (Shift key) if you want to produce uppercase.</p>
<p>I hope you learned / gained something&#8230;</p>
<p>Many thanks!</p>
<p>(comments / sugestions / errata about this subject is very much welcome)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webbryan.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webbryan.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webbryan.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webbryan.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webbryan.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webbryan.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webbryan.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webbryan.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webbryan.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webbryan.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webbryan.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webbryan.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webbryan.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webbryan.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webbryan.wordpress.com&amp;blog=5348636&amp;post=3&amp;subd=webbryan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webbryan.wordpress.com/2008/10/30/simple-javaawtrobot-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/59cf7e9edc86fbbeaa5e4076dc4197d2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">webbryan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
