Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

One, foreword

QQ is our commonly used chat software. Sometimes we have to send a lot of words for a long time when we are in a dispute with others. At this time, we can use Python to implement a simple message bombing tool to free our hands.

Two, the implementation principle

The implementation of message bombing is as simple as typing text all the time and pressing enter. So we only need to be able to control the keyboard to achieve message bombardment. In addition, whether it is wechat or other chat software, the implementation is the same.

3. Pynput module

There are many ways to control the mouse and keyboard in Python, and today we are using a simple and useful module. The PyNput module encapsulates keyboard and mouse operations and is very simple to use. Let’s take a closer look.

1. Install the module

To install the module, simply execute the following statement:

pip install pynput
Copy the code

Once installed, we can import the modules. The import method is as follows:

from pynput import keyboard, mouse
Copy the code

Here we import two word modules, used to operate the keyboard and mouse respectively. Today we’re just going to use the keyboard.

2. Keyboard input

The first thing we need to do to implement the message bombing is to input the content, and the operation of input is very simple. The general process is as follows:

  1. Create keyboard Controller
  2. The input

The code is as follows:

from pynput import keyboard
Create keyboard controller
kb = keyboard.Controller()
# Input content
kb.type(" test ")Copy the code

After we create the keyboard controller object, we call the type method to pass in the content we need to enter to achieve typing.

3. Press the keyboard

In addition to directly entering the text above, we can also click the specified button to perform the following operations:

from pynput import keyboard
kb = keyboard.Controller()
# Press the A keyKB. Press (" a ")# Release the A keyKB. Release (" a ")Copy the code

Note that we need to call the press and Release methods separately to achieve a complete button click. Press the button and release the button. Now we can implement message bombing.

Four, news bombardment

Message bombing is just typing over and over again and hitting Enter. So we need to use a loop, and we also need to get the focus of the input window before typing, so we need to leave some time for us to manually click the input box, as follows:

import time
from pynput import, Key keyboard
# Bombing interval
span = 1
kb = keyboard.Controller()
time.sleep(3)
while True:
	kb.type(" test ") KB. Press (Key. The enter) KB. Release (Key. The enter) time. Sleep (span)Copy the code

Here we click on the button and we pass in key. enter which means press Enter. We can also improve:

import time
import random
from pynput import, Key keyboard
span = 1Sentences = [abusive words1"" Curse words2"" Curse words3"" Curse words4",] KB = keyboard.controller () time.sleep()3)
while flag:
	# Random typing
	num = random.randint(1.len(sentences))
	kb.type(sentences[num])	
	kb.press(Key.enter)
	kb.release(Key.enter)
	time.sleep(span)
Copy the code

Above we added the effect of randomly typing sentences. We just need to open QQ, and then run the program and click the input box to start the message bombardment.