Backgroud
原理很簡單:robot類模擬鍵盤輸入,快捷鍵打開微信,搜索好友,把發送內容發送到粘貼板實作,
程式原始碼
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 T6 {
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 = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
//打開微信 Ctrl+Alt+W
assert 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);
// 發送訊息
sendMsg();
}
private static void sendMsg() throws InterruptedException {
String[] mottoes = {
"我只愛你四天,春天夏天秋天冬天!",
"我只愛你三天,昨天,今天,明天!",
"我只愛你兩天,白天,黑天!",
"我只愛你一天,每一天!",
"[玫瑰]愛你么么噠!",
"[呲牙][壞笑]",
"[奸笑]"
};
for (String motto : mottoes) {
sendOneMsg(motto);
}
Thread.sleep(2000);
sendOneMsg("[得意]就問你,膩不膩害!");
}
}
效果如下圖所示

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/179423.html
標籤:其他
下一篇:格式化文本標記
