Backgroud
原理很简单:robot类模拟键盘输入,快捷键打开微信,搜索好友,把发送内容发送到粘贴板实现。
另外一篇博客介绍了通过python实现Python指定wx好友自动发送消息
程序源码
package com.cloudansys.test;import java.awt.*;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.Transferable;import java.awt.event.KeyEvent;public class T5 {public static void main(String[] args) throws InterruptedException {// 好友昵称// String friendNickName = "文件传输助手";String friendNickName = "不器";searchMyFriendAndSend(friendNickName);}private static void searchMyFriendAndSend(String friendNickName) throws InterruptedException {// 创建Robot对象Robot robot = getRobot();//打开微信 Ctrl+Alt+Wassert robot != null;robot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_ALT);robot.keyPress(KeyEvent.VK_W);//释放Ctrl按键,像Ctrl,退格键,删除键这样的功能性按键,在按下后一定要释放robot.keyRelease(KeyEvent.VK_CONTROL);robot.keyRelease(KeyEvent.VK_ALT);// 该延迟不能少,否则无法搜索robot.delay(1000);// Ctrl + F 搜索指定好友robot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_F);robot.keyRelease(KeyEvent.VK_CONTROL);// 将好友昵称发送到剪切板Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();Transferable tText = new StringSelection(friendNickName);clip.setContents(tText, null);// 以下两行按下了ctrl+v,完成粘贴功能robot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_V);robot.keyRelease(KeyEvent.VK_CONTROL);robot.delay(1000);robot.keyPress(KeyEvent.VK_ENTER);robot.delay(1000);// 发送消息sendMsg();}private static void sendMsg() throws InterruptedException {String[] mottoes = {"我只爱你四天,春天夏天秋天冬天!","我只爱你三天,昨天,今天,明天!","我只爱你两天,白天,黑天!","我只爱你一天,每一天!","[玫瑰]爱你么么哒!","[呲牙][坏笑]","[奸笑]"};for (String motto : mottoes) {sendOneMsg(motto);}Thread.sleep(2000);sendOneMsg("[得意]就问你,腻不腻害!");}private static void sendOneMsg(String msg) {// 创建Robot对象Robot robot = getRobot();Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();// 将字符串复制到剪切板Transferable tText = new StringSelection(msg);clip.setContents(tText, null);// 以下两行按下了ctrl+v,完成粘贴功能robot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_V);robot.keyRelease(KeyEvent.VK_CONTROL);// 回车发送robot.keyPress(KeyEvent.VK_ENTER);robot.delay(1000);}private static Robot getRobot(){// 创建Robot对象Robot robot = null;try {robot = new Robot();} catch (AWTException e) {e.printStackTrace();}return robot;}}