国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

深入Java Robot實現控制鼠標和鍵盤的方法詳解

2019-11-26 16:05:44
字體:
來源:轉載
供稿:網友
Java.awt.Robot 類用于控制鼠標和鍵盤。一旦你得到這種控制,你能夠通過你的Java代碼做與鼠標和鍵盤任何類型的操作。這個類通常用于自動化測試。下面的代碼樣例將向您展示Robot類如何處理鍵盤事件。如果你運行此代碼,并打開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秒的延遲以便你打開notepad
// Robot 開始寫
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();
}
}
}

網友完善了以上代碼:
復制代碼 代碼如下:

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秒的延遲以便你打開notepad 哈哈
// Robot 開始寫
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();
}
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岑巩县| 西宁市| 姜堰市| 平凉市| 厦门市| 苍南县| 阿坝县| 许昌县| 定边县| 绥中县| 扎囊县| 长治市| 凤台县| 四川省| 五家渠市| 万安县| 崇左市| 宜宾县| 江口县| 秭归县| 萍乡市| 化隆| 甘洛县| 甘孜县| 湾仔区| 松溪县| 理塘县| 陆良县| 杂多县| 海安县| 桑植县| 晴隆县| 兴安县| 平定县| 西藏| 科技| 泰宁县| 兴宁市| 洛川县| 逊克县| 威海市|