Python自动操作鼠标
最近在B站上看到一个游戏挂,是基于图像画面进行处理的,不需要修改内存数据。感觉这个方法非常可行,我就也想写个这种东西,自然需要用到鼠标的操作,简单记录一下。
鼠标操作:Pyautogui库
安装通过pip
安装即可:pip install pyautogui
基本用法:
# 获取屏幕高宽
screen_width, screen_height = pyautogui.size()
print(screen_width,screen_height)
# 获取鼠标位置与移动
current_mouse_x, current_mouse_y = pyautogui.position()
print(current_mouse_x,current_mouse_y)
pyautogui.moveTo(1000,1000,duration=1,) # duration是间隔
pyautogui.moveTo(screen_width/2,screen_height/2)
pyautogui.moveTo(1100,20)
# 鼠标点击
pyautogui.click(567,27,clicks=1,button='left',duration=0.0)
pyautogui.moveRel(xOffset=0,yOffset=500,duration=1.0,tween=pyautogui.linear,)
pyautogui.click(1472,35,interval=0.1,button='left',duration=1.0)
time.sleep(0.5)
pyautogui.doubleClick(1472,35,interval=0.1,button='left',duration=0.0)
pyautogui.moveTo(1523,627)
pyautogui.leftClick()
# 键盘操作
pyautogui.typewrite(message='hello world!',interval=0.1)
pyautogui.press('enter',_pause=0.5)
pyautogui.keyDown('shift')
pyautogui.keyUp('shift')
# 用户界面交互
pyautogui.alert(text='this is an alert box',title='test')
pyautogui.confirm('enter option',buttons=['a','b','c'],title='test')
pyautogui.password('enter password')
pyautogui.prompt('prompt message')
pyautogui.position()
pyautogui.moveTo(2147,1559,0.5)
pyautogui.click()
pyautogui.dragTo(2390,1559,duration=1)
# 这个比较有意思,可以直接截图指定像素方框内的内容并保存到numpy数组中,非常方便!
img1=pyautogui.screenshot(region=[171,784,1000,1000])
# img1.save('screenshot.png')
键鼠事件监听:pynput
pynput
中有mouse
和keyboard
两个包,分别负责鼠标和键盘的监听。我用了multiprocessing
库,多进程运行监听,使得它可以同时监听键盘和鼠标。
import os
from multiprocessing import Process
import pyautogui
from pynput import mouse, keyboard
width, height = pyautogui.size()
cx, cy = width / 2, height / 2
down = False # 判断是否处于按下未抬起的状态
count = 0
def grab(x, y):
global count # 使用全局变量,共享数据
count += 1
img1 = pyautogui.screenshot(region=[x - 500, y - 500, 1000, 1000])
img1.save(f'screenshot{count}.png')
def on_press(key): # 键盘按下时的操作
global down # 如果按下某个键没有抬起,这个函数就会不停地被调用
if not down:
down = True
if isinstance(key, keyboard.KeyCode):
print(f'pressed: {key.char}')
if key.char == 'q': # 如果按下q,则将指针移动到屏幕中间
print('q pressed', end=' ')
print(pyautogui.position())
pyautogui.moveTo(cx, cy, duration=0)
elif isinstance(key, keyboard.Key):
print(f'pressed: {key}')
else:
print('unknown pressed') # ks
def on_release(key): # 键盘弹起的动作
global down
down = False
print('releasing', key)
if key == keyboard.Key.esc:
return False
def on_move(x, y): # 鼠标移动的动作
# print('position:',x,y)
return
def on_click(x, y, button, pressed): # 鼠标点击的动作
if pressed:
if button == mouse.Button.left:
button_name = 'Button_left'
elif button == mouse.Button.middle:
button_name = 'Button_middle'
grab(x, y) # 如果是鼠标中键,就在这个位置截图
elif button == mouse.Button.right:
button_name = 'Button_right'
# return False
elif button == mouse.Button.x1:
button_name = 'x1'
elif button == mouse.Button.x2:
button_name = 'x2'
else:
button_name = 'Unknown'
print('{0} Pressed at {1}'.format(button_name, (x, y), ))
def keylistening(): # 键盘监听
print('starting key listening')
with keyboard.Listener(on_press=on_press, on_release=on_release) as keylisten:
keylisten.join()
def mouselistening(): # 鼠标监听
print('starting mouse listening')
with mouse.Listener(on_click=on_click, on_move=on_move) as mouselisten:
mouselisten.join()
if __name__ == '__main__':
grab(0, 0)
p1 = Process(target=keylistening, )
p2 = Process(target=mouselistening, )
p1.daemon = True
p2.daemon = True
p1.start()
p2.start()
p1.join()
p2.join()
在游戏中操作鼠标:pydirectinput
我发现自己写的程序在游戏中没法操作鼠标,搜了一下发现是raw_input
和direct_input
的区别。游戏中应该需要采用direct_input
才能实现键鼠操作。(但经过实践发现在LOL中也没法用)
这里需要引入pydirectinput
包,安装也是直接pip
下载就可以。
用法和pyautogui
差不多:
import pydirectinput as pyd
width, height = pyd.size()
print(pyd.position())
然而,就算用这个替换了pyautogui
作为键鼠输入,在游戏界面中还是没有反应...
写挂的道路暂时告一段落..