java消息轰炸 Java实现消息轰炸的方法 附带源码
覅乆 人气:0想了解Java实现消息轰炸的方法 附带源码的相关内容吗,覅乆在本文为您仔细讲解java消息轰炸的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java消息轰炸,java轰炸源码,下面大家一起来学习吧。
1. 用到的技术
ArrayList
Robot (java中自动控制电脑输入设备的封装类)
swing
awt
先写个最简单的Demo然后逐渐完善。
2. 使用Robot 自动发送消息(v1.0)
robot.delay(3000); 停止3000毫秒
KeyEvent.VK_W是java封装好的常量,本质上就是就是每一个按键对应一个id
太多了,就截取了一部分。
按键事件:(还有鼠标按钮点击事件,这里我们主要使用了键盘点击事件)
robot.keyPress(KeyEvent.VK_W); 按下W键
robot.keyRelease(KeyEvent.VK_W); W键弹起
代码:
import java.awt.*; import java.awt.event.KeyEvent; public class TestRobot2 { public static void main(String[] args) throws AWTException { Robot robot = new Robot(); robot.delay(3000); for (int i = 0; i < 5; i++) { robot.keyPress(KeyEvent.VK_W); robot.keyRelease(KeyEvent.VK_W); robot.keyPress(KeyEvent.VK_O); robot.keyRelease(KeyEvent.VK_O); robot.keyPress(KeyEvent.VK_A); robot.keyRelease(KeyEvent.VK_A); robot.keyPress(KeyEvent.VK_I); robot.keyRelease(KeyEvent.VK_I); robot.keyPress(KeyEvent.VK_N); robot.keyRelease(KeyEvent.VK_N); robot.keyPress(KeyEvent.VK_I); robot.keyRelease(KeyEvent.VK_I); robot.delay(100); robot.keyPress(KeyEvent.VK_SPACE); robot.keyRelease(KeyEvent.VK_SPACE); robot.delay(100); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); } } }
使用for循环可以反复发送该消息。
(注意:如果循环使用了死循环,一定要在robot中添加延时函数,否则发送消息的QQ会卡死,亲测)
效果:
3. 记录用户操作(v2.0)
染过要实现自动发消息,不可能让用户像上一步一样去直接与代码打交道,并且对于我们来说,如果每次都像上一步那样,既费时又费力,那么可不可以用一个相对来说比较简便的方法呢,它来了。
我们应该知道,java中每个按键对应了一个id,那是不是就可以将这些id存储起来,在使用的时候调用它呢
这里我们先存储用户的操作信息,还原是下一步
实现方法:
使用ArrayList动态数组,存储用户操作信息
还可以使用switch语句,给用户提示信息:
先上一部分代码:
ArrayList<KeyValue> list = new ArrayList<KeyValue>();//用于存储用户操作的动态数组 int n = 0;//自定义一个变量,用于存储式第几次,和下面的 addList方法搭配使用 //这个方法用于向数组汇总存储用户的操作信息 public void addList(int Key, String name) { //自定义了一个对象,KeyValue,里面有Key和Value两个自定义值 list.add(new KeyValue(++n, Key, name)); }
自定义的KeyValue对象:
class KeyValue { public int Num;//存储是第几次按下的 public int Key;//存储按键对应的KeyCode public String Value;//存储按键的信息,主要用于给用户提示 public KeyValue(int num, int key, String value) { Key = key; Num = num; Value = value; } }
先看看效果:
上代码:
package GUI.Demo0418; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; //尝试写个记录按键记录的demo public class Test { public static void main(String[] args) throws AWTException { new MyFrame(); } } class MyFrame extends Frame { ArrayList<KeyValue> list = new ArrayList<KeyValue>();//用于存储用户操作的动态数组 int n = 0;//自定义一个变量,用于存储式第几次,和下面的 addList方法搭配使用 //这个方法用于向数组汇总存储用户的操作信息 public void addList(int Key, String name) { //自定义了一个对象,KeyValue,里面有Key和Value两个自定义值 list.add(new KeyValue(++n, Key, name)); } public MyFrame() throws AWTException { super("记录用户的按键操作"); JButton button3 = new JButton("查看记录信息"); button3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (KeyValue ky : list) { System.out.println("第" + ky.Num + "次" + ky.Key + " " + ky.Value); } } }); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("窗口正在关闭"); System.exit(0); } }); setLayout(new FlowLayout(FlowLayout.LEADING)); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); switch (keyCode) { case KeyEvent.VK_A: System.out.println("你点击了 A 键,这个按键的ID为:" + keyCode); addList(keyCode, "A"); break; case KeyEvent.VK_B: System.out.println("你点击了 B 键,这个按键的ID为:" + keyCode); addList(keyCode, "B"); break; case KeyEvent.VK_C: System.out.println("你点击了 C 键,这个按键的ID为:" + keyCode); addList(keyCode, "C"); break; case KeyEvent.VK_D: System.out.println("你点击了 D 键,这个按键的ID为:" + keyCode); addList(keyCode, "D"); break; case KeyEvent.VK_E: System.out.println("你点击了 E 键,这个按键的ID为:" + keyCode); addList(keyCode, "E"); break; case KeyEvent.VK_F: System.out.println("你点击了 F 键,这个按键的ID为:" + keyCode); addList(keyCode, "F"); break; case KeyEvent.VK_G: System.out.println("你点击了 G 键,这个按键的ID为:" + keyCode); addList(keyCode, "G"); break; case KeyEvent.VK_H: System.out.println("你点击了 H 键,这个按键的ID为:" + keyCode); addList(keyCode, "H"); break; case KeyEvent.VK_I: System.out.println("你点击了 I 键,这个按键的ID为:" + keyCode); addList(keyCode, "I"); break; case KeyEvent.VK_J: System.out.println("你点击了 J 键,这个按键的ID为:" + keyCode); addList(keyCode, "J"); break; case KeyEvent.VK_K: System.out.println("你点击了 K 键,这个按键的ID为:" + keyCode); addList(keyCode, "K"); break; case KeyEvent.VK_L: System.out.println("你点击了 L 键,这个按键的ID为:" + keyCode); addList(keyCode, "L"); break; case KeyEvent.VK_M: System.out.println("你点击了 M 键,这个按键的ID为:" + keyCode); addList(keyCode, "M"); break; case KeyEvent.VK_N: System.out.println("你点击了 N 键,这个按键的ID为:" + keyCode); addList(keyCode, "N"); break; case KeyEvent.VK_O: System.out.println("你点击了 O 键,这个按键的ID为:" + keyCode); addList(keyCode, "O"); break; case KeyEvent.VK_P: System.out.println("你点击了 P 键,这个按键的ID为:" + keyCode); addList(keyCode, "P"); break; case KeyEvent.VK_Q: System.out.println("你点击了 Q 键,这个按键的ID为:" + keyCode); addList(keyCode, "Q"); break; case KeyEvent.VK_R: System.out.println("你点击了 R 键,这个按键的ID为:" + keyCode); addList(keyCode, "R"); break; case KeyEvent.VK_S: System.out.println("你点击了 S 键,这个按键的ID为:" + keyCode); addList(keyCode, "S"); break; case KeyEvent.VK_T: System.out.println("你点击了 T 键,这个按键的ID为:" + keyCode); addList(keyCode, "T"); break; case KeyEvent.VK_U: System.out.println("你点击了 U 键,这个按键的ID为:" + keyCode); addList(keyCode, "U"); break; case KeyEvent.VK_V: System.out.println("你点击了 V 键,这个按键的ID为:" + keyCode); addList(keyCode, "V"); break; case KeyEvent.VK_W: System.out.println("你点击了 W 键,这个按键的ID为:" + keyCode); addList(keyCode, "W"); break; case KeyEvent.VK_X: System.out.println("你点击了 X 键,这个按键的ID为:" + keyCode); addList(keyCode, "X"); break; case KeyEvent.VK_Y: System.out.println("你点击了 Y 键,这个按键的ID为:" + keyCode); addList(keyCode, "Y"); break; case KeyEvent.VK_Z: System.out.println("你点击了 Z 键,这个按键的ID为:" + keyCode); addList(keyCode, "Z"); break; case KeyEvent.VK_SPACE: System.out.println("你点击了 空格 键,这个按键的ID为:" + keyCode); addList(keyCode, "空格"); break; case KeyEvent.VK_ENTER: System.out.println("你点击了 ENTER 键,这个按键的ID为:" + keyCode); addList(keyCode, "回车"); break; default: System.out.println("该按键没有收录"); } } }); add(button3); setBounds(100, 100, 500, 500); setVisible(true); setResizable(false); setAlwaysOnTop(true); } } class KeyValue { public int Num; public int Key; public String Value; public KeyValue(int num, int key, String value) { Key = key; Num = num; Value = value; } }
4. 还原操作(v3.0)
简单的说就是遍历上一步存储的用户操作信息,再加上几个按钮,用来控制何时进行还原,和还原的次数。
直接上代码:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; //尝试写个记录按键记录的demo public class Test { public static void main(String[] args) throws AWTException { new MyFrame(); } } class MyFrame extends Frame { ArrayList<KeyValue> list = new ArrayList<KeyValue>();//用于存储用户操作的动态数组 Robot robot = new Robot(); //下拉框,用于获取用户想要发送几次这个消息 String[] num = {"1", "2", "3", "4", "5", "6", "10", "死"}; JComboBox jComboBox = new JComboBox(num); int n = 0;//自定义一个变量,用于存储式第几次,和下面的 addList方法搭配使用 //这个方法用于向数组汇总存储用户的操作信息 public void addList(int Key, String name) { //自定义了一个对象,KeyValue,里面有Key和Value两个自定义值 list.add(new KeyValue(++n, Key, name)); } public MyFrame() throws AWTException { super("记录用户的按键操作"); JButton button1 = new JButton("清空"); JButton button2 = new JButton("开始"); JButton button3 = new JButton("查看记录信息"); button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { list = null; } }); button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { robot.delay(3000); System.out.println(jComboBox.getSelectedItem()); if (jComboBox.getSelectedItem().equals("死")) { while (true) { for (KeyValue ky : list) { robot.keyPress(ky.Key); robot.keyRelease(ky.Key); robot.delay(10); } } } else { int time = Integer.parseInt((String) jComboBox.getSelectedItem()); for (int i = 0; i < time; i++) { for (KeyValue ky : list) { robot.keyPress(ky.Key); robot.keyRelease(ky.Key); robot.delay(100); } } } } }); button3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (KeyValue ky : list) { System.out.println("第" + ky.Num + "次" + ky.Key + " " + ky.Value); } } }); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("窗口正在关闭"); System.exit(0); } }); setLayout(new FlowLayout(FlowLayout.LEADING)); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); switch (keyCode) { case KeyEvent.VK_A: System.out.println("你点击了 A 键,这个按键的ID为:" + keyCode); addList(keyCode, "A"); break; case KeyEvent.VK_B: System.out.println("你点击了 B 键,这个按键的ID为:" + keyCode); addList(keyCode, "B"); break; case KeyEvent.VK_C: System.out.println("你点击了 C 键,这个按键的ID为:" + keyCode); addList(keyCode, "C"); break; case KeyEvent.VK_D: System.out.println("你点击了 D 键,这个按键的ID为:" + keyCode); addList(keyCode, "D"); break; case KeyEvent.VK_E: System.out.println("你点击了 E 键,这个按键的ID为:" + keyCode); addList(keyCode, "E"); break; case KeyEvent.VK_F: System.out.println("你点击了 F 键,这个按键的ID为:" + keyCode); addList(keyCode, "F"); break; case KeyEvent.VK_G: System.out.println("你点击了 G 键,这个按键的ID为:" + keyCode); addList(keyCode, "G"); break; case KeyEvent.VK_H: System.out.println("你点击了 H 键,这个按键的ID为:" + keyCode); addList(keyCode, "H"); break; case KeyEvent.VK_I: System.out.println("你点击了 I 键,这个按键的ID为:" + keyCode); addList(keyCode, "I"); break; case KeyEvent.VK_J: System.out.println("你点击了 J 键,这个按键的ID为:" + keyCode); addList(keyCode, "J"); break; case KeyEvent.VK_K: System.out.println("你点击了 K 键,这个按键的ID为:" + keyCode); addList(keyCode, "K"); break; case KeyEvent.VK_L: System.out.println("你点击了 L 键,这个按键的ID为:" + keyCode); addList(keyCode, "L"); break; case KeyEvent.VK_M: System.out.println("你点击了 M 键,这个按键的ID为:" + keyCode); addList(keyCode, "M"); break; case KeyEvent.VK_N: System.out.println("你点击了 N 键,这个按键的ID为:" + keyCode); addList(keyCode, "N"); break; case KeyEvent.VK_O: System.out.println("你点击了 O 键,这个按键的ID为:" + keyCode); addList(keyCode, "O"); break; case KeyEvent.VK_P: System.out.println("你点击了 P 键,这个按键的ID为:" + keyCode); addList(keyCode, "P"); break; case KeyEvent.VK_Q: System.out.println("你点击了 Q 键,这个按键的ID为:" + keyCode); addList(keyCode, "Q"); break; case KeyEvent.VK_R: System.out.println("你点击了 R 键,这个按键的ID为:" + keyCode); addList(keyCode, "R"); break; case KeyEvent.VK_S: System.out.println("你点击了 S 键,这个按键的ID为:" + keyCode); addList(keyCode, "S"); break; case KeyEvent.VK_T: System.out.println("你点击了 T 键,这个按键的ID为:" + keyCode); addList(keyCode, "T"); break; case KeyEvent.VK_U: System.out.println("你点击了 U 键,这个按键的ID为:" + keyCode); addList(keyCode, "U"); break; case KeyEvent.VK_V: System.out.println("你点击了 V 键,这个按键的ID为:" + keyCode); addList(keyCode, "V"); break; case KeyEvent.VK_W: System.out.println("你点击了 W 键,这个按键的ID为:" + keyCode); addList(keyCode, "W"); break; case KeyEvent.VK_X: System.out.println("你点击了 X 键,这个按键的ID为:" + keyCode); addList(keyCode, "X"); break; case KeyEvent.VK_Y: System.out.println("你点击了 Y 键,这个按键的ID为:" + keyCode); addList(keyCode, "Y"); break; case KeyEvent.VK_Z: System.out.println("你点击了 Z 键,这个按键的ID为:" + keyCode); addList(keyCode, "Z"); break; case KeyEvent.VK_SPACE: System.out.println("你点击了 空格 键,这个按键的ID为:" + keyCode); addList(keyCode, "空格"); break; case KeyEvent.VK_ENTER: System.out.println("你点击了 ENTER 键,这个按键的ID为:" + keyCode); addList(keyCode, "回车"); break; default: System.out.println("该按键没有收录"); } } }); add(button1); add(jComboBox); add(button2); add(button3); setBounds(100, 100, 500, 500); setVisible(true); setResizable(false); setAlwaysOnTop(true); } } class KeyValue { public int Num; public int Key; public String Value; public KeyValue(int num, int key, String value) { Key = key; Num = num; Value = value; } }
效果:
5. 总结
昨天晚上突然跳出的想法,花了点时间实现了,虽然界面不太美观,但是想要的功能都实现了,感觉代码冗余有点多。。可能会有梗简洁的代码吧(时间紧,博客写的有点匆忙)
加载全部内容