import java.awt.AWTException; import java.awt.MouseInfo; import java.awt.Point; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.St
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.InputEvent;
import java.util.Timer;
import java.util.TimerTask;
public class WindowsUtils {
static int curX = 0;
static int curY = 0;
private Robot myRobot;
public WindowsUtils() {
try {
myRobot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
/**
* 获取当前位置
*/
public static void getCurPostion() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Point point = MouseInfo.getPointerInfo().getLocation();
if (curX != point.x && curY != point.y) {
System.out.println("Location:x=" + point.x + ", y = " + point.y);
}
curX = point.x;
curY = point.y;
}
},
100, 100);
}
/**
* 从剪贴板中获取文本(粘贴)
*/
public String getClipboardString() {
// 获取系统剪贴板
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// 获取剪贴板中的内容
Transferable trans = clipboard.getContents(null);
if (trans != null) {
// 判断剪贴板中的内容是否支持文本
if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
// 获取剪贴板中的文本内容
return (String) trans.getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 把文本设置到剪贴板(复制)
*/
public void setClipboardString(String text) {
// 获取系统剪贴板
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// 封装文本内容
Transferable trans = new StringSelection(text);
// 把文本内容设置到系统剪贴板
clipboard.setContents(trans, null);
}
/**
* 按键
*
* @param keyCode KeyEvent.VK_SHIFT,KeyEvent.VK_Q,KeyEvent.VK_Q,KeyEvent.VK_SHIFT
*/
public void keyPress(int keyCode) {
// 打出一个大写的Q
myRobot.keyPress(keyCode); // 模拟键盘按下shift键
myRobot.keyRelease(keyCode);
// myRobot.keyPress(KeyEvent.VK_Q); // 模拟键盘按下Q键(小写)
// myRobot.keyRelease(KeyEvent.VK_Q); // 模拟键盘释放Q键
// myRobot.keyPress(KeyEvent.VK_SHIFT); // 模拟键盘按下shift键
// myRobot.keyRelease(KeyEvent.VK_SHIFT); // 模拟键盘释放shift键
}
// 模拟鼠标右键
public void clickMouseRight() {
myRobot.mousePress(InputEvent.BUTTON3_MASK);
myRobot.mouseRelease(InputEvent.BUTTON3_MASK);
myRobot.delay(200);
}
// 鼠标移动
public void change(int type, int x, int y) {
Point p = MouseInfo.getPointerInfo().getLocation();
int width = (int) p.getX() + x;
int heigh = (int) p.getY() + y;
if (type == 0) {
width = x;
heigh = y;
}
myRobot.mouseMove(width, heigh);
}
// 模拟鼠标左键
public void clickMouseLeft() {
myRobot.mousePress(InputEvent.BUTTON1_MASK);
myRobot.mouseRelease(InputEvent.BUTTON1_MASK);
}
/**
* 单击
*/
public void click(int x, int y) {
//鼠标移动到某一点
myRobot.mouseMove(x, y);
//模拟鼠标按下左键
myRobot.mousePress(InputEvent.BUTTON1_MASK);
// 延时100毫秒
// myRobot.delay(100);
//模拟鼠标松开左键
myRobot.mouseRelease(InputEvent.BUTTON1_MASK);
//InputEvent.BUTTON2_MASK表示鼠标中键
//InputEvent.BUTTON3_MASK表示鼠标右键
//robot还可以模拟键盘点击,如有需要请自行百度
}
}