無(wú)為

          無(wú)為則可為,無(wú)為則至深!

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks

          公告

          與有奉獻(xiàn)精神和分享精神的人做朋友!

          Locations of visitors to this page
          Subscribe by Anothr

          搜索

          •  

          積分與排名

          • 積分 - 582706
          • 排名 - 83

          最新評(píng)論

          閱讀排行榜

          Java.awt.Robot 類用于控制鼠標(biāo)和鍵盤。一旦你得到這種控制,你能夠通過(guò)你的Java代碼做與
          鼠標(biāo)和鍵盤任何類型的操作.這個(gè)類通常用于自動(dòng)化測(cè)試。先面的代碼樣例將向您展示Robot
          類如何處理鍵盤事件。如果你運(yùn)行此代碼,并打開(kāi)notepad,您將在notepad中看到HI CAOER。趕快試一試吧。
          import java.awt.AWTException;
          import java.awt.Robot;
          import java.awt.event.KeyEvent;

          public class RobotExp {

          public static void main(String[] args) {

          try {

          Robot robot = new Robot();
          //定義5秒的延遲以便你打開(kāi)notepad 哈哈
          // Robot 開(kāi)始寫
          robot.delay(5000);
          robot.keyPress(KeyEvent.VK_H);
          robot.keyPress(KeyEvent.VK_I);
          robot.keyPress(KeyEvent.VK_SPACE);
          robot.keyPress(KeyEvent.VK_C);
          robot.keyPress(KeyEvent.VK_A);
          robot.keyPress(KeyEvent.VK_O);
          robot.keyPress(KeyEvent.VK_E);
          robot.keyPress(KeyEvent.VK_R);

          } catch (AWTException e) {
          e.printStackTrace();
          }
          }
          }



          凡是有該標(biāo)志的文章,都是該blog博主Caoer(草兒)原創(chuàng),凡是索引、收藏
          、轉(zhuǎn)載請(qǐng)注明來(lái)處和原文作者。非常感謝。

          posted on 2007-08-09 14:59 草兒 閱讀(2889) 評(píng)論(12)  編輯  收藏 所屬分類: java

          Feedback

          # re: 如何在Java中使用Robot類 2007-08-09 17:58 bromon
          樓主你好歹也把程序執(zhí)行一下吧,一看就知道打出來(lái)的肯定不是hi budy  回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-08-09 20:29 pass86
          hi caoer  回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-08-09 21:22 草兒
          哈哈 我后來(lái)改了 HI CAOER   回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-08-10 11:11 sitinspring
          這個(gè)不錯(cuò),類似于VBVC的Sendkey,不知有沒(méi)有對(duì)應(yīng)的AppActivate函數(shù).  回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-08-10 11:27 Swing
          其實(shí)這個(gè)類有時(shí)候是很危險(xiǎn)的
          它可以輕易的對(duì)電腦文件進(jìn)行拖拽刪除等  回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-08-10 17:00 HutWang
          很有意思.

          完善下:

          import java.awt.AWTException;
          import java.awt.Robot;
          import java.awt.event.KeyEvent;

          import java.io.IOException;


          public class RobotExp {
          public static void pressKey(Robot robot, int keyvalue) {
          robot.keyPress(keyvalue);
          robot.keyRelease(keyvalue);
          }

          public static void pressKeyWithShift(Robot robot, int keyvalue) {
          robot.keyPress(KeyEvent.VK_SHIFT);
          robot.keyPress(keyvalue);
          robot.keyRelease(keyvalue);
          robot.keyRelease(KeyEvent.VK_SHIFT);
          }

          public static void closeApplication(Robot robot) {
          // pressKey(robot, KeyEvent.VK_ALT);
          // pressKey(robot, KeyEvent.VK_F4);
          robot.keyPress(KeyEvent.VK_ALT);
          robot.keyPress(KeyEvent.VK_F4);
          robot.keyRelease(KeyEvent.VK_ALT);
          robot.keyRelease(KeyEvent.VK_F4);

          //for linux.
          // robot.keyPress(KeyEvent.VK_ALT);
          // robot.keyPress(KeyEvent.VK_W);
          // robot.keyRelease(KeyEvent.VK_ALT);
          // robot.keyRelease(KeyEvent.VK_W);
          robot.keyPress(KeyEvent.VK_N);
          robot.keyRelease(KeyEvent.VK_N);
          }

          public static void main(String[] args) throws IOException {
          try {
          Robot robot = new Robot();
          Runtime.getRuntime().exec("notepad");

          // For linux.
          //Runtime.getRuntime().exec("gedit");
          //定義5秒的延遲以便你打開(kāi)notepad 哈哈
          // Robot 開(kāi)始寫
          robot.delay(3000);

          for (int i = 0; i < 100; i++) {
          pressKeyWithShift(robot, KeyEvent.VK_H);
          pressKey(robot, KeyEvent.VK_I);
          pressKey(robot, KeyEvent.VK_SPACE);

          //pressKeyWithShift(robot, KeyEvent.VK_H);
          pressKeyWithShift(robot, KeyEvent.VK_I);
          pressKey(robot, KeyEvent.VK_SPACE);
          pressKey(robot, KeyEvent.VK_A);
          pressKey(robot, KeyEvent.VK_M);
          pressKey(robot, KeyEvent.VK_SPACE);
          pressKey(robot, KeyEvent.VK_T);
          pressKey(robot, KeyEvent.VK_H);
          pressKey(robot, KeyEvent.VK_E);
          pressKey(robot, KeyEvent.VK_SPACE);
          pressKey(robot, KeyEvent.VK_J);
          pressKey(robot, KeyEvent.VK_A);
          pressKey(robot, KeyEvent.VK_V);
          pressKey(robot, KeyEvent.VK_A);
          pressKey(robot, KeyEvent.VK_SPACE);
          pressKey(robot, KeyEvent.VK_R);
          pressKey(robot, KeyEvent.VK_O);
          pressKey(robot, KeyEvent.VK_B);
          pressKey(robot, KeyEvent.VK_O);
          pressKey(robot, KeyEvent.VK_T);

          // VK_ENTER
          pressKey(robot, KeyEvent.VK_ENTER);

          //pressKey(robot, KeyEvent.);
          }

          closeApplication(robot);

          //robot.keyPress(KeyEvent.VK_SPACE);
          } catch (AWTException e) {
          e.printStackTrace();
          }
          }
          }
            回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-08-10 17:33 sitinspring
          得臨時(shí)打開(kāi)程序啊啊,激活已有程序改怎么做?

            回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-08-10 22:09 草兒
          你樓上的兄弟已經(jīng)給了 答案了 哈哈  回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-08-11 08:10 BeanSoft
          不能打開(kāi)組合鍵, 郁悶. 誰(shuí)知道如何模擬像 Ctrl + Space 這樣的按鍵組合?  回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-08-11 11:06 草兒
          robot.keyPress(KeyEvent.VK_CONTROL);
          robot.keyPress(KeyEvent.VK_SPACE );
          robot.keyRelease(KeyEvent.VK_CONTROL);
          robot.keyRelease(KeyEvent.VK_SPACE);
          這就是組合鍵,但要包括按下和釋放兩個(gè)動(dòng)作  回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-08-11 11:12 草兒
          "Key pressed" and "key released" events are lower-level and depend on the platform and keyboard layout. They are generated whenever a key is pressed or released, and are the only way to find out about keys that don't generate character input (e.g., action keys, modifier keys, etc.). The key being pressed or released is indicated by the getKeyCode method, which returns a virtual key code.

          Virtual key codes are used to report which keyboard key has been pressed, rather than a character generated by the combination of one or more keystrokes (such as "A", which comes from shift and "a").

            回復(fù)  更多評(píng)論
            

          # re: 如何在Java中使用Robot類 2007-09-15 21:20 黑蝙蝠
          @pass86
          看來(lái)這個(gè)和按鍵精靈的思路一致  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 乐山市| 田东县| 峨眉山市| 丰顺县| 松潘县| 上饶县| 洛川县| 孟村| 增城市| 屏边| 温州市| 道孚县| 清远市| 阆中市| 稻城县| 慈利县| 饶河县| 灌南县| 古田县| 泗阳县| 鸡东县| 屏山县| 阿拉善右旗| 梅州市| 庆元县| 望谟县| 龙岩市| 九寨沟县| 明水县| 河北省| 双鸭山市| 雅江县| 莎车县| 临泉县| 蓬莱市| 黔西县| 平凉市| 台东县| 吉林省| 萨迦县| 凤山县|