Python键鼠自动化

引言

控制鼠标和键盘是自动化任务中的常见需求。在Python中,pynput库是一种强大的工具,可以帮助我们实现这些操作。

1. 安装pynput库

首先,我们需要安装pynput库。可以使用pip命令来进行安装:

1
pip install pynput

2. 控制鼠标

pynput库提供了控制鼠标的功能。下面是一些常见的鼠标操作示例:

1. 获取当前鼠标位置:

运行后三秒内将鼠标移到要点击的地方

1
2
3
4
5
6
7
from pynput.mouse import Button, Controller
import time

time.sleep(3)
mouse = Controller()
# 获取鼠标坐标
print('当前鼠标的坐标是 {0}'.format(mouse.position))

2. 移动鼠标位置:

1
2
3
4
5
6
from pynput.mouse import Controller


mouse = Controller()
# 将鼠标移动到指定位置(x, y)
mouse.position = (500, 500)

3. 模拟鼠标点击:

1
2
3
4
5
6
7
8
from pynput.mouse import Controller, Button


mouse = Controller()
# 模拟左键点击
mouse.click(Button.left)
# 模拟右键点击
mouse.click(Button.right)

3. 控制键盘

pynput库还提供了控制键盘的功能。下面是一些常见的键盘操作示例:

1. 模拟按键:

1
2
3
4
5
6
7
8
9
10
11
from pynput.keyboard import Controller, Key


keyboard = Controller()
# 模拟按下和释放键盘上的空格按键
keyboard.press(Key.space)
keyboard.release(Key.space)

# 模拟按下和释放键盘上的删除按键
keyboard.press(keyboard.Key.backspace)
keyboard.release(keyboard.Key.backspace)

2. 输入文本:

1
2
3
4
5
6
from pynput.keyboard import Controller, Key


keyboard = Controller()
# 输入文本 Hello, World!
keyboard.type("Hello, World!")

3. 组合键操作:

1
2
3
4
5
6
7
8
from pynput.keyboard import Controller, Key


keyboard = Controller()
# 模拟组合键操作(Ctrl+C)
with keyboard.pressed(Key.ctrl):
keyboard.press('c')
keyboard.release('c')

示例脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import time,pyperclip
from pynput import mouse, keyboard
from pynput.keyboard import Key, Controller
time.sleep(3)

ip=[]
for f in open(r'ip.txt', 'r'): # 打开文件
rs = f.replace('\n', '') # 替换换行符
ip.append(rs)

for i in range(len(ip)):
time.sleep(1)
m_mouse = mouse.Controller() #创建一个鼠标
m_keyboard = keyboard.Controller() #创建一个键盘
time.sleep(0.5)
m_mouse.position=(344,92) #将鼠标移动到指定位置 点击谷歌网址栏目
m_mouse.click(mouse.Button.left) #点击鼠标左键


with m_keyboard.pressed(Key.ctrl):
m_keyboard.press('a')
m_keyboard.release('a')

time.sleep(1)
m_keyboard.press(keyboard.Key.backspace)
m_keyboard.release(keyboard.Key.backspace)

time.sleep(0.5)
png_name = ip[i] # 复制IP到剪贴板
pyperclip.copy(png_name)
time.sleep(1)
with m_keyboard.pressed(keyboard.Key.ctrl): # 粘贴文本 以IP为图片名 更加直观容易辩证
m_keyboard.press('v')
m_keyboard.release('v')

time.sleep(0.5)
m_keyboard.type(":9200")
time.sleep(1)
m_keyboard.press(keyboard.Key.enter) #按下enter
m_keyboard.release(keyboard.Key.enter) #松开

time.sleep(4)
m_mouse.position=(1794,1126) #将鼠标移动到指定位置
m_mouse.click(mouse.Button.left) #点击鼠标左键 点击截图软件
time.sleep(1)

m_mouse.position=(1509,735) #将鼠标移动到指定位置
time.sleep(1)
m_mouse.click(mouse.Button.left) #点击鼠标左键 点击截图
time.sleep(1)

time.sleep(1)

with m_keyboard.pressed(Key.ctrl): # 保存图片
m_keyboard.press('s')
m_keyboard.release('s')


time.sleep(1)
with m_keyboard.pressed(keyboard.Key.ctrl): # 粘贴文本 以IP为图片名 更加直观容易辩证
m_keyboard.press('v')
m_keyboard.release('v')

time.sleep(1)
m_keyboard.press(keyboard.Key.enter) #按下enter
m_keyboard.release(keyboard.Key.enter) #松开


time.sleep(1)