Java中模擬滑鼠和鍵盤輸入
阿新 • • 發佈:2019-02-15
{
private Robot robot =null;
public RobotDemo() {
try{
robot =new Robot();
}catch (AWTException e) {
e.printStackTrace();
}
}
/** 可以彈出QQ */
publicvoid keyBoardDemo() {
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_ALT);
}
/** 前提是有個最大化的視窗,功能是移動到標題欄,然後拖拽到600,600的位置*/
publicvoid mouseDemo(){
robot.mouseMove(80, 10);
robot.mousePress(KeyEvent.BUTTON1_MASK);
try{
Thread.sleep(20);
}catch (InterruptedException e) {
e.printStackTrace();
}
robot.mouseMove(600, 600);
robot.mouseRelease(KeyEvent.BUTTON1_MASK);
}
/**
* @param args
*/
publicstaticvoid main(String[] args) {
RobotDemo demo=new RobotDemo();
demo.keyBoardDemo();
demo.mouseDemo();
}
}
private Robot robot =null;
public RobotDemo() {
try{
robot =new Robot();
}catch (AWTException e) {
e.printStackTrace();
}
}
/** 可以彈出QQ */
publicvoid keyBoardDemo() {
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_ALT);
}
/** 前提是有個最大化的視窗,功能是移動到標題欄,然後拖拽到600,600的位置*/
publicvoid mouseDemo(){
robot.mouseMove(80, 10);
robot.mousePress(KeyEvent.BUTTON1_MASK);
try{
Thread.sleep(20);
}catch (InterruptedException e) {
e.printStackTrace();
robot.mouseMove(600, 600);
robot.mouseRelease(KeyEvent.BUTTON1_MASK);
}
/**
* @param args
*/
publicstaticvoid main(String[] args) {
RobotDemo demo=new RobotDemo();
demo.keyBoardDemo();
demo.mouseDemo();
}
}