程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 深刻Java Robot完成掌握鼠標和鍵盤的辦法詳解

深刻Java Robot完成掌握鼠標和鍵盤的辦法詳解

編輯:關於JAVA

深刻Java Robot完成掌握鼠標和鍵盤的辦法詳解。本站提示廣大學習愛好者:(深刻Java Robot完成掌握鼠標和鍵盤的辦法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是深刻Java Robot完成掌握鼠標和鍵盤的辦法詳解正文


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();
}
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved