欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

深入Java Robot实现控制鼠标和键盘的方法详解

程序员文章站 2023-12-13 12:08:22
java.awt.robot 类用于控制鼠标和键盘。一旦你得到这种控制,你能够通过你的java代码做与鼠标和键盘任何类型的操作。这个类通常用于自动化测试。下面的代码样例将向...
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();
}
}
}

上一篇:

下一篇: