熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

如何在Java中使用Robot類

2022-06-13   來源: Java核心技術 

  JavaawtRobot 類用於控制鼠標和鍵盤一旦你得到這種控制你能夠通過你的Java代碼做與鼠標和鍵盤任何類型的操作這個類通常用於自動化測試先面的代碼樣例將向您展示Robot類如何處理鍵盤事件如果你運行此代碼並打開notepad您將在notepad中看到HI CAOER趕快試一試吧

  

  import javaawtAWTException; import javaawtRobot; import javaawteventKeyEvent; public class RobotExp { public static void main(String[] args) { try { Robot robot = new Robot(); //定義秒的延遲以便你打開notepad 哈哈 // Robot 開始寫 robotdelay(); robotkeyPress(KeyEventVK_H); robotkeyPress(KeyEventVK_I); robotkeyPress(KeyEventVK_SPACE); robotkeyPress(KeyEventVK_C); robotkeyPress(KeyEventVK_A); robotkeyPress(KeyEventVK_O); robotkeyPress(KeyEventVK_E); robotkeyPress(KeyEventVK_R); } catch (AWTException e) { eprintStackTrace(); } } }

  網友完善了以上代碼

  

  import javaawtAWTException; import javaawtRobot; import javaawteventKeyEvent; import javaioIOException; public class RobotExp { public static void pressKey(Robot robot int keyvalue) { robotkeyPress(keyvalue); robotkeyRelease(keyvalue); } public static void pressKeyWithShift(Robot robot int keyvalue) { robotkeyPress(KeyEventVK_SHIFT); robotkeyPress(keyvalue); robotkeyRelease(keyvalue); robotkeyRelease(KeyEventVK_SHIFT); } public static void closeApplication(Robot robot) { // pressKey(robot KeyEventVK_ALT); // pressKey(robot KeyEventVK_F); robotkeyPress(KeyEventVK_ALT); robotkeyPress(KeyEventVK_F); robotkeyRelease(KeyEventVK_ALT); robotkeyRelease(KeyEventVK_F); //for linux // robotkeyPress(KeyEventVK_ALT); // robotkeyPress(KeyEventVK_W); // robotkeyRelease(KeyEventVK_ALT); // robotkeyRelease(KeyEventVK_W); robotkeyPress(KeyEventVK_N); robotkeyRelease(KeyEventVK_N); } public static void main(String[] args) throws IOException { try { Robot robot = new Robot(); RuntimegetRuntime()exec(notepad); // For linux //RuntimegetRuntime()exec(gedit); //定義秒的延遲以便你打開notepad 哈哈 // Robot 開始寫 robotdelay(); for (int i = ; i < ; i++) { pressKeyWithShift(robot KeyEventVK_H); pressKey(robot KeyEventVK_I); pressKey(robot KeyEventVK_SPACE); //pressKeyWithShift(robot KeyEventVK_H); pressKeyWithShift(robot KeyEventVK_I); pressKey(robot KeyEventVK_SPACE); pressKey(robot KeyEventVK_A); pressKey(robot KeyEventVK_M); pressKey(robot KeyEventVK_SPACE); pressKey(robot KeyEventVK_T); pressKey(robot KeyEventVK_H); pressKey(robot KeyEventVK_E); pressKey(robot KeyEventVK_SPACE); pressKey(robot KeyEventVK_J); pressKey(robot KeyEventVK_A); pressKey(robot KeyEventVK_V); pressKey(robot KeyEventVK_A); pressKey(robot KeyEventVK_SPACE); pressKey(robot KeyEventVK_R); pressKey(robot KeyEventVK_O); pressKey(robot KeyEventVK_B); pressKey(robot KeyEventVK_O); pressKey(robot KeyEventVK_T); // VK_ENTER pressKey(robot KeyEventVK_ENTER); //pressKey(robot KeyEvent); } closeApplication(robot); //robotkeyPress(KeyEventVK_SPACE); } catch (AWTException e) { eprintStackTrace(); } } }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25521.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.