麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > 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();
}
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲自拍第二页 | 日本高清在线播放 | 国产免费激情视频 | 精品无吗乱吗av国产爱色 | 美女羞羞视频在线观看 | 久久99精品久久久久久秒播蜜臀 | 欧美精品毛片 | 日韩黄色片免费看 | 九九热精品免费 | 日韩毛片免费观看 | 精品一区二区在线观看视频 | 日韩在线激情 | 91麻豆精品国产91久久久点播时间 | 国产精品99久久久久久董美香 | 久久久久久久久久久久久久久伊免 | 亚州综合一区 | 亚洲欧洲日韩av | 色视频在线 | 久久精品视频黄色 | 欧美日韩一区二区综合 | 黄在线观看在线播放720p | 免费看真人a一级毛片 | 少妇色诱麻豆色哟哟 | 黄色免费电影网址 | 九九热视频这里只有精品 | 91av资源在线 | 日日做夜夜操 | 国产成人精品免费视频大全最热 | 久久精品99国产国产精 | 色妞妞视频 | 久久亚洲精品久久国产一区二区 | 欧产日产国产精品v | 黄色免费入口 | 久久99深爱久久99精品 | 色播视频在线播放 | 国产永久免费观看 | 最新黄色电影网站 | arabxxxxvideos| 91真视频 | 91久久国产综合久久91猫猫 | 爽爽淫人综合网网站 |