深入Java Robot實(shí)現(xiàn)控制鼠標(biāo)和鍵盤的方法詳解
更新時(shí)間:2013年05月17日 11:24:21 作者:
本篇文章是對(duì)Java中用Robot實(shí)現(xiàn)控制鼠標(biāo)和鍵盤的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
Java.awt.Robot 類用于控制鼠標(biāo)和鍵盤。一旦你得到這種控制,你能夠通過你的Java代碼做與鼠標(biāo)和鍵盤任何類型的操作。這個(gè)類通常用于自動(dòng)化測(cè)試。下面的代碼樣例將向您展示Robot類如何處理鍵盤事件。如果你運(yùn)行此代碼,并打開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();
}
}
}
網(wǎng)友完善了以上代碼:
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();
}
}
}
復(fù)制代碼 代碼如下:
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();
}
}
}
網(wǎng)友完善了以上代碼:
復(fù)制代碼 代碼如下:
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();
}
}
}
相關(guān)文章
SpringBoot內(nèi)部調(diào)用事務(wù)不起作用問題的解決方案
這篇文章主要介紹了SpringBoot事務(wù)不起作用問題的解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10SpringBoot下載Excel文件時(shí),報(bào)錯(cuò)文件損壞的解決方案
這篇文章主要介紹了SpringBoot下載Excel文件時(shí),報(bào)錯(cuò)文件損壞的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06SpringSecurity中的Filter Chain(過濾器鏈)
Spring Security的Filter Chain是由一系列過濾器組成的管道,每個(gè)過濾器執(zhí)行特定的安全功能,Spring Security能夠提供強(qiáng)大而靈活的安全控制機(jī)制,從而保護(hù)你的應(yīng)用程序不受各種網(wǎng)絡(luò)安全威脅的侵害,本文介紹SpringSecurity中的Filter Chain,感興趣的朋友跟隨小編一起看看吧2024-06-06springboot整合shiro實(shí)現(xiàn)記住我功能
這篇文章主要介紹了springboot整合shiro實(shí)現(xiàn)記住我功能,配置類 ShiroConfig,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10Spring Boot啟動(dòng)過程(五)之Springboot內(nèi)嵌Tomcat對(duì)象的start教程詳解
這篇文章主要介紹了Spring Boot啟動(dòng)過程(五)之Springboot內(nèi)嵌Tomcat對(duì)象的start的相關(guān)資料,需要的朋友可以參考下2017-04-04Java分析Lambda表達(dá)式Stream流合并分組內(nèi)對(duì)象數(shù)據(jù)合并
Lambda表達(dá)式,基于Lambda所帶來的函數(shù)式編程,又引入了一個(gè)全新的Stream概念,用于解決集合類庫既有的弊端,Lambda 允許把函數(shù)作為一個(gè)方法的參數(shù)(函數(shù)作為參數(shù)傳遞進(jìn)方法中)。使用 Lambda 表達(dá)式可以使代碼變的更加簡(jiǎn)潔緊湊2022-12-12