﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-sealyu-随笔分类-Eclipse SWT/JFace RCP</title><link>http://www.blogjava.net/sealyu/category/30672.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 10 Apr 2008 22:43:35 GMT</lastBuildDate><pubDate>Thu, 10 Apr 2008 22:43:35 GMT</pubDate><ttl>60</ttl><item><title>使用SWT模拟鼠标键盘事件</title><link>http://www.blogjava.net/sealyu/archive/2008/04/10/192001.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 10 Apr 2008 14:42:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/04/10/192001.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/192001.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/04/10/192001.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/192001.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/192001.html</trackback:ping><description><![CDATA[<h3>最近在学习SWT/JFace，在做一个小东西的时候需要模拟鼠标键盘事件，在网上搜了一下，在java中模拟这些事件，主要有两种方法：</h3>
<h4>一、使用AWT中的Robot类</h4>
<h2>Robot</h2>
<pre><a title="java.lang 中的类" href="http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/lang/Object.html">java.lang.Object</a>
<img alt="继承者 " src="file:///C:/Documents%20and%20Settings/yuhaibo/My%20Documents/Robot%20%28Java%202%20Platform%20SE%205_0%29.files/inherit.gif" /><strong>java.awt.Robot</strong>
</pre>
<h5>
<hr />
</h5>
<dl><dt>
<pre>public class <strong>Robot</strong><dt>extends <a title="java.lang 中的类" href="http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/lang/Object.html">Object</a></dt></pre>
</dt></dl>
<h5>此类用于为测试自动化、自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件。Robot 的主要目的是便于 Java 平台实现自动测试。类中几个主要的模拟函数如下：</h5>
<div>
<table summary="" border="1" cellpadding="3" cellspacing="0" width="100%">
    <tbody>
        <tr bgcolor="white">
            <td align="right" valign="top" width="1%"><font size="-1"><code>void</code></font></td>
            <td><code><strong><a href="http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/awt/Robot.html#keyPress%28int%29">keyPress</a></strong>(int&nbsp;keycode)</code> <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;按下给定的键。</td>
        </tr>
        <tr bgcolor="white">
            <td align="right" valign="top" width="1%"><font size="-1"><code>&nbsp;void</code></font></td>
            <td><code><strong><a href="http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/awt/Robot.html#keyRelease%28int%29">keyRelease</a></strong>(int&nbsp;keycode)</code> <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;释放给定的键。</td>
        </tr>
        <tr bgcolor="white">
            <td align="right" valign="top" width="1%"><font size="-1"><code>&nbsp;void</code></font></td>
            <td><code><strong><a href="http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/awt/Robot.html#mouseMove%28int,%20int%29">mouseMove</a></strong>(int&nbsp;x, int&nbsp;y)</code> <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;将鼠标指针移动到给定屏幕坐标。</td>
        </tr>
        <tr bgcolor="white">
            <td align="right" valign="top" width="1%"><font size="-1"><code>&nbsp;void</code></font></td>
            <td><code><strong><a href="http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/awt/Robot.html#mousePress%28int%29">mousePress</a></strong>(int&nbsp;buttons)</code> <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;按下一个或多个鼠标按钮。</td>
        </tr>
        <tr bgcolor="white">
            <td align="right" valign="top" width="1%"><font size="-1"><code>&nbsp;void</code></font></td>
            <td><code><strong><a href="http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/awt/Robot.html#mouseRelease%28int%29">mouseRelease</a></strong>(int&nbsp;buttons)</code> <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;释放一个或多个鼠标按钮。</td>
        </tr>
        <tr bgcolor="white">
            <td align="right" valign="top" width="1%"><font size="-1"><code>&nbsp;void</code></font></td>
            <td><code><strong><a href="http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/awt/Robot.html#mouseWheel%28int%29">mouseWheel</a></strong>(int&nbsp;wheelAmt)</code> <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在配有滚轮的鼠标上旋转滚轮。</td>
        </tr>
    </tbody>
</table>
</div>
<div><strong>二、使用SWT中的鼠标键盘事件</strong>
<div>&nbsp;&nbsp;&nbsp; 在SWT的snippets中有两个例子用来介绍这两个方法的使用，如下：</div>
<div><br />
(1)、模拟鼠标事件<br />
&nbsp;* UI Automation (for testing tools) snippet: post mouse events<br />
import org.eclipse.swt.*;</div>
<div>public class Snippet142 {</div>
<div>public static void main(String[] args) {<br />
&nbsp;final Display display = new Display();<br />
&nbsp;final Shell shell = new Shell(display);<br />
&nbsp;final Button button = new Button(shell,SWT.NONE);<br />
&nbsp;button.setSize(100,100);<br />
&nbsp;button.setText("Click");<br />
&nbsp;shell.pack();<br />
&nbsp;shell.open();<br />
&nbsp;button.addListener(SWT.MouseDown, new Listener() {<br />
&nbsp;&nbsp;public void handleEvent(Event e){<br />
&nbsp;&nbsp;&nbsp;System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")");<br />
&nbsp;&nbsp;}<br />
&nbsp;});<br />
&nbsp;final Point pt = display.map(shell, null, 50, 50);<br />
&nbsp;new Thread(){<br />
&nbsp;&nbsp;Event event;<br />
&nbsp;&nbsp;public void run(){<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(300);<br />
&nbsp;&nbsp;&nbsp;} catch (InterruptedException e) {}<br />
&nbsp;&nbsp;&nbsp;event = new Event();<br />
&nbsp;&nbsp;&nbsp;event.type = SWT.MouseMove;<br />
&nbsp;&nbsp;&nbsp;event.x = pt.x;<br />
&nbsp;&nbsp;&nbsp;event.y = pt.y;<br />
&nbsp;&nbsp;&nbsp;display.post(event);<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(300);<br />
&nbsp;&nbsp;&nbsp;} catch (InterruptedException e) {}<br />
&nbsp;&nbsp;&nbsp;event.type = SWT.MouseDown;<br />
&nbsp;&nbsp;&nbsp;event.button = 1;<br />
&nbsp;&nbsp;&nbsp;display.post(event);<br />
&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(300);<br />
&nbsp;&nbsp;&nbsp;} catch (InterruptedException e) {}<br />
&nbsp;&nbsp;&nbsp;event.type = SWT.MouseUp;<br />
&nbsp;&nbsp;&nbsp;display.post(event);<br />
&nbsp;&nbsp;}&nbsp;<br />
&nbsp;}.start();<br />
&nbsp;while (!shell.isDisposed()) {<br />
&nbsp;&nbsp;if (!display.readAndDispatch()) display.sleep();<br />
&nbsp;}<br />
&nbsp;display.dispose();<br />
}<br />
}<br />
(2)、模拟键盘事件</div>
<div><br />
&nbsp;* UI Automation (for testing tools) snippet: post key events<br />
import org.eclipse.swt.*;</div>
<div>public class Snippet146 {</div>
<div>public static void main(String[] args) {<br />
&nbsp;final Display display = new Display();<br />
&nbsp;final Shell shell = new Shell(display);<br />
&nbsp;final Text text = new Text(shell, SWT.BORDER);<br />
&nbsp;text.setSize(text.computeSize(150, SWT.DEFAULT));<br />
&nbsp;shell.pack();<br />
&nbsp;shell.open();<br />
&nbsp;new Thread(){<br />
&nbsp;&nbsp;public void run(){<br />
&nbsp;&nbsp;&nbsp;String string = "Love the method.";<br />
&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; string.length(); i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;char ch = string.charAt(i);<br />
&nbsp;&nbsp;&nbsp;&nbsp;boolean shift = Character.isUpperCase(ch);<br />
&nbsp;&nbsp;&nbsp;&nbsp;ch = Character.toLowerCase(ch);<br />
&nbsp;&nbsp;&nbsp;&nbsp;if (shift) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Event event = new Event();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.type = SWT.KeyDown;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.keyCode = SWT.SHIFT;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display.post(event);&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;Event event = new Event();<br />
&nbsp;&nbsp;&nbsp;&nbsp;event.type = SWT.KeyDown;<br />
&nbsp;&nbsp;&nbsp;&nbsp;event.character = ch;<br />
&nbsp;&nbsp;&nbsp;&nbsp;display.post(event);<br />
&nbsp;&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(10);<br />
&nbsp;&nbsp;&nbsp;&nbsp;} catch (InterruptedException e) {}<br />
&nbsp;&nbsp;&nbsp;&nbsp;event.type = SWT.KeyUp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;display.post(event);<br />
&nbsp;&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(100);<br />
&nbsp;&nbsp;&nbsp;&nbsp;} catch (InterruptedException e) {}<br />
&nbsp;&nbsp;&nbsp;&nbsp;if (shift) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event = new Event();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.type = SWT.KeyUp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.keyCode = SWT.SHIFT;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display.post(event);&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}&nbsp;<br />
&nbsp;}.start();<br />
&nbsp;while (!shell.isDisposed()) {<br />
&nbsp;&nbsp;if (!display.readAndDispatch()) display.sleep();<br />
&nbsp;}<br />
&nbsp;display.dispose();<br />
}<br />
}<br />
</div>
<div>两种方法中都是先声明一个Event，然后将该Event的type类型设置为对应的
SWT.KeyUp/KeyDown/MouseUp/MouseDown，最后通过Display类的post（Event）方法来模拟对应的事件。注
意：如果有多个控件，需要得到对应控件的display属性，然后post该事件。例如，在一个窗口中有一个Browser，则需要使用
thisClass.browser.getDisplay().post(event)。</div>
</div>
<table>
    <tbody>
        <tr>
            <td><br />
            </td>
        </tr>
    </tbody>
</table>
<script type="text/javascript"><!--
google_ad_client = "pub-6625678643128649";
google_alternate_color = "FFFFFF";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="";
//--></script>
<img src ="http://www.blogjava.net/sealyu/aggbug/192001.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-04-10 22:42 <a href="http://www.blogjava.net/sealyu/archive/2008/04/10/192001.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>修改 SWT browser类，添加getText()方法</title><link>http://www.blogjava.net/sealyu/archive/2008/04/10/191997.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 10 Apr 2008 14:35:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/04/10/191997.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/191997.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/04/10/191997.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/191997.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/191997.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最近用到了SWT中的Browser类，这个类不允许被继承，在网上搜索的时候发现了下面的帖子。这个类实际上是使用OLE，将IE直接嵌套进SWT程序中去。下面代码的修改实际上将SWT中的WebBrowser类的子类IE直接和Browser类合到一起，而同时也就没有了原来SWT中的跨平台的优点。该作者添加了setHtml这个函数和_text属性。从而得到对应的网页的html内容。/*...&nbsp;&nbsp;<a href='http://www.blogjava.net/sealyu/archive/2008/04/10/191997.html'>阅读全文</a><img src ="http://www.blogjava.net/sealyu/aggbug/191997.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-04-10 22:35 <a href="http://www.blogjava.net/sealyu/archive/2008/04/10/191997.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>