有一天,你老婆要你给他发1000次我爱你,怎么实现呢?
相关代码如下
package com.jie;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;
/**
* @author wei zhijie
* @version 1.0
* @date 2021/1/9
*/
public class qq {
public static void main(String[] args) throws AWTException {
String sentence ="老婆,爱你爱你啊";
Robot robot = new Robot();
robot.delay(3000);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//根据字符串分割
String[] authon=sentence.split("[,]");
for (int j = 0; j < 50; j++) {
for (int i = 0; i < authon.length; i++) {
String sentencet=authon[i];
Transferable Ttest=new StringSelection(sentencet);
clipboard.setContents(Ttest,null);
// 按下了ctrl+v,完成粘贴功能
robot.keyPress(KeyEvent.VK_CONTROL);// 按下Control键
robot.keyPress(KeyEvent.VK_V);// 按下V键
robot.keyRelease(KeyEvent.VK_CONTROL);// 释放ctrl按键
robot.delay(500);// 延迟一秒再发送
robot.keyPress(KeyEvent.VK_ENTER);// 回车
}
}
}
}
结果
发送成功