This is the 28th day of my participation in the August Text Challenge.More challenges in August
preface
Python provides many modules for controlling input devices, such as the traditional Win32GUI, or Pygame for game development. Win32gui is more appropriate to say based on Windows programming, its operation is rich and varied, you can get each window, can also get the window handle and so on. Pygame’s strength is in the development of 2D games. Pynput, on the other hand, is very simple to use and contains content that is more suitable for input devices. The two most important modules are Mouse and Keyboard, which provide classes for controlling the mouse and keyboard, respectively.
1. Control the mouse
Let’s start by installing this module, which is very simple to install, using PIP directly:
pip install pynput
Copy the code
This module is now ready to use. We import the Mouse module:
from pynput import mouse
Copy the code
The Controller class is provided in the Mouse module. This class is our mouse Controller. We can create objects of this class to mouse and keyboard:
from pynput import mouse
Create a mouse
m = mouse.Controller()
Copy the code
Once we get the mouse object, we can get some properties or do some operations.
1.1 Obtaining the Mouse Position
We can obtain the mouse position information, i.e. the coordinates of the current mouse position:
from pynput import mouse
Create a mouse
m = mouse.Controller()
# Output mouse position
print(m.position)
Copy the code
The output is a tuple.
1.2 Locating the Mouse
We can also change the mouse position directly:
from pynput import mouse
# create mouse
m = mouse.Controller()
# Move the mouse to the upper left corner
m.position = (0.0)
Copy the code
This way is direct positioning of the mouse, we can also move the mouse according to the current position.
1.3 Moving the Mouse
To move the mouse, call the move function:
from pynput import mouse
# create mouse
m = mouse.Controller()
# Move the mouse to the upper left corner
m.move(50, -50)
Copy the code
The first argument is the value of the x move, and the second argument is the value of the y move.
In addition, there are three control buttons on the mouse, left button, right button and scroll wheel. Let’s see how to operate them.
1.4 Clicking the Mouse
When we click a button, we press it first and then release it:
from pynput import mouse
# create mouse
m = mouse.Controller()
# Right mouse button down
m.press(mouse.Button.right)
# Panasonic right mouse button
m.release(mouse.Button.right)
Copy the code
Button class is provided in Mouse, which has built-in left key and right key constant, we can directly use it.
In addition to the above method, we can also call the click method directly and click the mouse:
from pynput import mouse
# create mouse
m = mouse.Controller()
# Click the left mouse button
m.click(mouse.Button.left)
Copy the code
1.5 Double-click the Mouse
Double-click is also a very common operation, we can also use the click method:
from pynput import mouse
# create mouse
m = mouse.Controller()
# Click the left mouse button
m.click(mouse.Button.left, 2)
Copy the code
The click method takes two parameters, the first is the button and the second is optional, meaning the number of clicks.
1.6 Scroll wheel
For large tables like Excel, we often need to scroll up, down, left, and right, and the Mouse module provides this method:
from pynput import mouse
# create mouse
m = mouse.Controller()
The first argument is the value of the y scroll, and the second argument is the value of the x scroll
m.scroll(0, -10)
Copy the code
1.7 Listening to Mouse events
There are three events in the mouse, click event, move event, scroll event, let’s look at how to monitor the mouse event:
from pynput import mouse
def on_move(x, y) :
""" Mouse movement monitoring method x, y is the position after the move ""
print('Mouse moved to {0}'.format((x, y)))
def on_click(x, y, button, pressed) :
"" mouse click monitoring method x,y is the coordinate, button is the button, pressed is whether to press ""
if pressed:
print('Clicked ({0}, {1})'.format(x, y))
else:
print('Mouse release at ({0}, {1})'.format(x, y))
def on_scroll(x, y, dx, dy) :
""" The monitoring method of mouse scrolling x, y is as, dx,dy is the scrolling amplitude ""
print('Mouse in {0}, scroll right {1}, scroll down {2}'.format((x, y), dx, dy))
Create a listener
with mouse.Listener(
# associative listener method (without parentheses)
on_move=on_move,
on_click=on_click,
on_scroll=on_scroll) as listener:
# block thread
listener.join()
Copy the code
Our Mouse module provides a Listener class whose objects are our listeners. When we fire an event, the listener executes the associated method.
2. Control the keyboard
The Keyboard module is provided in PyNput, which provides classes similar to the Mouse module that can be used to control the keyboard. The Keyboard also has a Controller class, which is our keyboard Controller.
from pynput import keyboard
Create a keyboard
kb = keyboard.Controller()
Copy the code
We can create a keyboard controller with the above code. With the controller we can operate the keyboard.
2.1 Press and release a key
Here we call the press and release methods again:
from pynput import keyboard
Create a keyboard
kb = keyboard.Controller()
# Press the A key
kb.press('a')
# Release the A key
kb.release('a')
Copy the code
Above, we push the button by passing in a character. Here, we click the single character button intelligently. The Key class in the Keyboard module provides a large number of preset buttons, which we can directly use:
from pynput import keyboard
# create keyboard
kb = keyboard.Controller()
# Press case lock
kb.press(keyboard.Key.caps_lock)
# Release case locking
kb.release(keyboard.Key.caps_lock)
Copy the code
So that’s how we use press and release.
2.2 Press two buttons
We can press a few buttons by calling press multiple times.
from pynput import keyboard
Create a keyboard
kb = keyboard.Controller()
# press shift + a
with kb.pressed(keyboard.Key.shift):
kb.press('a')
kb.release('a')
Copy the code
The effect above is that we play an A.
2.3 the typing
In theory, the press and release methods can do most keyboard operations, including typing, but for efficiency we can use the Type method:
from pynput import keyboard
# create keyboard
kb = keyboard.Controller()
# typing
kb.type('Hello world')
Copy the code
When we type text, the input method does not affect our operation. When we type English, if the input method is Chinese mode, it will be our usual struggle sound effect.
2.4 Event Listening
The Listener class in the Keyboard also implements keyboard listening:
from pynput import keyboard
# Press the button
def on_press(key) :
print('Pressed {0}'.format(key))
# Release button
def on_release(key) :
print('Let go {0}'.format(key))
# to monitor
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
Copy the code
The listening step is the same as the mouse, so I won’t repeat it here.
3. Complete the Game helper with PyNput
Pynput doesn’t have any hard actions per se, we use it more for repetitive actions, and when we play games, grinding is a lot of repetitive actions. At this point, we can replace our work with programs.
Taking Pokemon as an example, let’s take a look at the keyboard distribution of the game itself:
These are the keys that we’re going to manipulate. Next we need to achieve automatic brush function. It doesn’t matter if you don’t know the game, this is just an example, but the steps to translate the problem into code are important.
3.1 Process of monster brushing
The first thing we need to know is what the process is, which is to start the process the first time and repeat the process the second time. If you look at PokeMMO, the flow is as follows:
- The first time, it automatically adjusts its position and lets the character fly to the hospital
- From the hospital to the field
- Use this ability to draw out monsters (you can use it four times) and fight
- Recycle your ability to elicit a monster (4 times)
- Return to hospital to replenish skill value
- Adjust your position in front of the hospital
The above is a complete process, we will separate each process slowly implemented.
3.2 Break each step into a flow
In addition to the overall process above, each of our steps can be divided into a specific process, that is, a series of operations. For example, in our first step, we do the following:
- Click on the pet
- Choose aeration
- Select the city
This breaks down our first step into a flow, and we wrap each step into a function, from small to large, to achieve the complete flow.
3.3 Adjust the position and let the character fly to the hospital
We translate this process into code in contrast to the keyboard:
import time
from pynput import keyboard
Create a keyboard
kb = keyboard.Controller()
def fly() :
""" Define the method to get the character back to the hospital. """
# Choose a pet
kb.type('3')
time.sleep(0.5)
# Choose Aeration
kb.type('s')
time.sleep(0.5)
# Select city, because flying takes a long time, it sleeps for 7 seconds
kb.type('l')
kb.type('l')
time.sleep(7)
Copy the code
So we’ve done the first step.
3.4 From the hospital to the field
This step is nothing more than the change of up, down, left and right keys. According to different routes, the duration of pressing each key is different. We can encapsulate routes into a list:
# 0,1,2, and 3 represent upper right, lower left, respectively
Lmz_weed = [
# Move left for 6 seconds
(3.6),
Move up 0.4 seconds
(0.0.4)]Copy the code
The elements of this list are tuples, and each tuple represents a route. The contents of the tuple are the direction and the length of the press. So we go through any route in a circular way:
def to_weed(road) :
""" Move according to route """
# Use a bike
m_keyboard.type('c')
Move according to the line
for i in road:
key = None
if i[0] = =0:
# Move up
key = Key.up
elif i[0] = =1:
# Move right
key = Key.right
elif i[0] = =2:
# Move down
key = Key.down
elif i[0] = =3:
# Move left
key = Key.left
# Press the corresponding direction
m_keyboard.press(key)
time.sleep(i[1])
m_keyboard.release(key)
Copy the code
So that’s what we’re walking on.
3.5 Use skills to draw out monsters
This step can be divided into the following processes:
- Choose a pet
- Use the skills
- Attack the pet
- Cycle four times
We package the eliciting pet and attacking pet into two functions respectively:
def earthquake() :
""" Defeat monsters with an earthquake """
# Use earthquake
m_keyboard.type('f')
time.sleep(0.5)
m_keyboard.type('q')
time.sleep(0.5)
m_keyboard.type('e')
time.sleep(37)
def sweet() :
"" use sweet aroma. ""
# Select the second pet to use sweet breath
m_keyboard.type('2')
time.sleep(0.5)
m_keyboard.type('s')
time.sleep(15)
Copy the code
With the above two functions, we can complete the process with a simple loop:
for i in range(4) :# Bring out pets
sweet()
# attack
earthquake()
Copy the code
3.6 Return to hospital to replenish skill value
This requires us to first go back to the hospital, then enter the hospital, and then talk to the nurse:
def to_hospital() :
""" Go to the hospital """
# Select the third pet to fly
fly()
# Into the hospital
m_keyboard.press(Key.up)
time.sleep(7)
m_keyboard.release(Key.up)
# dialogue
for i in range(8):
time.sleep(1.2)
m_keyboard.type('a')
time.sleep(1)
for i in range(3):
time.sleep(1)
m_keyboard.type('b')
# Out of the hospital
m_keyboard.press(Key.down)
time.sleep(7)
m_keyboard.release(Key.down)
# Aim at pet position
fly()
Copy the code
During the conversation, we simulated daily operations, pressing A repeatedly, then b repeatedly.
3.7 Complete Process
The complete flow is when we call the corresponding methods in turn with each of our steps and put them together:
time.sleep(3)
flag = True
while(True) :""" Cyclic brush monster """
if flag:
to_hospital()
flag = False
to_weed(Lmz_weed)
for i in range(4):
sweet()
earthquake()
to_hospital()
Copy the code
There is a lot of confusion here, mainly due to the unfamiliarity with the game. But the program itself is not the point. The point is to translate the entire operation into a series of processes, then to cut a process into a series of operations, and finally to implement those operations in code.
4. To summarize
Where can we use PyNput to help us? A large number of repetitive operations, such as message bombardment, are typical. There are other hand speeds that we humans can’t reach, and we can use computers to do that. And you can explore pyNput for yourself.