<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    illusionist

    好好學習...天天向上

    鼠標模擬和鍵盤映射測試

    因為畢業設計需要,做了一點點鼠標和鍵盤模擬的測試,收獲了一點關于Robot知識,這是一個非常有趣的類,此類用于測試自動化,自運行演示程序和其他需要控制鼠標和鍵盤的應用程序生成本機系統輸入事件。Robot類主要目的是便于java平臺實現自動測試。主要方法如下:動作都在java.awt.event包中的KeyEvent和MouseEvent中定義
    • void keyPress(int keycode)按下給定的鍵
    • void keyRelease(int keycode)釋放給定的鍵
    • void mouseMove(int x, int y)將鼠標指針移動到給定屏幕坐標
    • void mousePress(int buttons)按下一個或多個鼠標按鈕
    • void mouseRelease(int buttons)釋放一個或多個鼠標按鈕
    • void mouseWheel(int wheelAmt)在配有滾輪的鼠標旋轉滾輪
    • BufferedImage createScreenCapture(Rectangle screenRect)創建包含從屏幕中讀取的像素的圖像
    第一個例子是鼠標模擬測試,在多線程中每隔1s隨機移動鼠標,一共隨機6次鼠標閃爍,源碼如下:
    /*
     * MouseSimulate.java
     * 
     * Created on 2007-5-7, 4:03:04
     * 
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     
    */

    package cn.edu.yutao;

    import java.awt.AWTException;
    import java.awt.Dimension;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.event.InputEvent;
    import java.util.Random;

    /**
     *
     * 
    @author Aslan
     
    */
    public class MouseSimulate implements Runnable{
        
        
    private volatile boolean isRunning = false;
        
        
    private Robot robot;
        
    private Dimension dim;
        
    private Random random;

        
    public MouseSimulate() {
            random 
    = new Random();
            dim 
    = Toolkit.getDefaultToolkit().getScreenSize();
            
            
    try{
                robot 
    = new Robot();
            }
    catch(AWTException e){
                e.printStackTrace();
            }
        }

        
    public void run() {
            
    while(isRunning){
                
    int x = random.nextInt((int)dim.getWidth());
                
    int y = random.nextInt((int)dim.getHeight());
                System.out.println(
    "the mouse located in (" + x + "," + y + ")");
                
                robot.mouseMove(x, y);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                
                
    try{
                    Thread.sleep(
    1000);
                }
    catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
        
        
    public synchronized void start(){
            isRunning 
    = true;
        }
        
        
    public synchronized void stop(){
            isRunning 
    = false;
        }
        
        
    public static void main(String[] args){
            MouseSimulate test 
    = new MouseSimulate();
            
            test.start();
            System.out.println(
    "-----------time start-------------");
            Thread thread 
    = new Thread(test);
            thread.start();
            
            
    try{
                Thread.sleep(
    10000);
            }
    catch(InterruptedException e){
                e.printStackTrace();
            }
            
            test.stop();
            System.out.println(
    "-----------time stop--------------");
        }

    }

    Robot類的方法createScreenCapture可以簡單的用于抓取屏幕圖片,可以在java應用程序中直接調用該方法抓取屏幕,檢測遠程電腦屏幕狀態,這里參考了java社區的例子,默認構造函數生成后綴為png的文件,可以在第二個構造函數傳入其他名稱,支持gif和jpg。截圖程序源碼如下:
    /*
     * GuiCamera.java
     * 
     * Created on 2007-5-7, 4:18:46
     * 
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     
    */

    package cn.edu.yutao;

    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;

    /**
     *
     * 
    @author Aslan
     
    */
    public class GuiCamera {
        
        
    private String fileName;
        
    private final String defaultFileName = "camera";
        
        
    private String format;
        
    private final String defaultFormat = "png";
        Dimension dim 
    = Toolkit.getDefaultToolkit().getScreenSize();
        

        
    public GuiCamera() {
            
    this.fileName = defaultFileName;
            
    this.format = defaultFormat;
        }

        
    public GuiCamera(String fileName, String format) {
            
    this.fileName = fileName;
            
    this.format = format;
        }
        
        
    public void capture() throws Exception{
            BufferedImage imageScreen 
    = (new Robot()).createScreenCapture(new Rectangle((int)dim.getWidth(), (int)dim.getHeight()));
            String imageName 
    = this.fileName + "." + this.format;
            File file 
    = new File(imageName);
            System.out.println(
    "Save file " + imageName);
            ImageIO.write(imageScreen, format, file);
            System.out.println(
    "Finished!!");
        }
        
        
    public static void main(String[] args){
            GuiCamera camera 
    = new GuiCamera("hello""jpg");
            
    try{
                camera.capture();
            }
    catch(Exception e){
                e.printStackTrace();
            }
        }
        
        

    }
    以上程序都在mac os 10.4.8下測試,截圖為  很漂亮~ 出現警告是因為某些api在jdk6中已經標記為廢棄。

    posted on 2007-05-09 21:44 伽藍 閱讀(2120) 評論(5)  編輯  收藏 所屬分類: Java SE

    Feedback

    # re: 鼠標模擬和鍵盤映射測試 2007-05-10 04:02 黑蝙蝠

    恩 頂一下 不錯這樣看來可以用Robot類做一個類似按鍵精靈的軟件
    學習...  回復  更多評論   

    # re: 鼠標模擬和鍵盤映射測試 2007-05-10 08:03 BeanSoft

    還可以做遠程控制, 跨平臺的... 我做過, 但是功能比較弱... 主要是不支持組合鍵. http://gro.clinux.org/frs/?group_id=740&release_id=887  回復  更多評論   

    # re: 鼠標模擬和鍵盤映射測試 2007-05-10 08:59 Swing

    @BeanSoft
    遠程控制怎么做的 一個什么思路?  回復  更多評論   

    # re: 鼠標模擬和鍵盤映射測試 2007-05-10 09:27 BeanSoft

    就是個C/S模式的服務器, 客戶端再服務器發回的截屏上點擊按鍵和鼠標, 然后把事件發會給服務器端, 服務器端呢再用 Robot 模擬按鍵和鼠標.
    詳細使用說明:
    http://gro.clinux.org/forum/forum.php?forum_id=2597  回復  更多評論   

    # re: 鼠標模擬和鍵盤映射測試 2007-05-10 16:25 伽藍

    可以在服務端寫一個servlet,將response返回流設置為image,這樣可以在客戶端查看服務器屏幕狀態  回復  更多評論   



    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 国产午夜免费秋霞影院| 成年网站免费视频A在线双飞| 宅男666在线永久免费观看| 一级毛片完整版免费播放一区| 成年人在线免费观看| 国产女高清在线看免费观看 | 永久免费A∨片在线观看| 久久99亚洲综合精品首页| 国产精品亚洲片夜色在线| 免费无码又爽又刺激网站| 亚洲av日韩av无码| 18级成人毛片免费观看| 亚洲粉嫩美白在线| 日韩一级免费视频| 一个人看的免费视频www在线高清动漫| 亚洲美女高清一区二区三区| 亚洲精品无码mⅴ在线观看| 巨胸喷奶水www永久免费| 亚洲av无码专区国产乱码在线观看 | youjizz亚洲| 亚洲国产成人精品91久久久| 亚洲一级片在线播放| 成年美女黄网站色大免费视频| 久久精品国产亚洲AV未满十八| 一个人免费观看视频www| 亚洲av乱码一区二区三区按摩| 亚洲第一区精品观看| 嫩草成人永久免费观看| 亚洲综合色自拍一区| 99免费观看视频| 亚洲av无码成人精品国产| 亚洲香蕉网久久综合影视| 亚洲w码欧洲s码免费| 国产亚洲精彩视频| 久久精品视频亚洲| 卡1卡2卡3卡4卡5免费视频| 国产.亚洲.欧洲在线| 国产免费av片在线播放| 国产偷伦视频免费观看| 亚洲精品动漫免费二区| 国产成人亚洲精品影院|