1.脚本用管理员运行。
2.使用PyDirectInput库。
安装
pip install pydirectinput
在python关于按键精灵得操作中常用的有PyAutoGUI,但在使用的过程中,针对一些游戏就直接失灵了,特别是一些以DirectX来开发的游戏或软件。我通过收索相关资料了解到之所以会这样,是因为PyAutoGUI使用的是 Virtual Key Codes (VKs),也就是模拟按键码。而对于这些游戏或软件来说需要使用Directinput,这样就需要直接从设备中获取数据,而不是模拟的。而这个由github上Ben Johnson开发的 PyDirectInput库就是针对此而设计的。
使用:
-
import pydirectinput
-
pydirectinput.moveTo(100, 150) # 移动鼠标至坐标100,150
-
pydirectinput.click() # 点击鼠标左键
-
pydirectinput.click(200, 220) # 移动鼠标至坐标200,220,并点击左键
-
pydirectinput.move(None, 10) # 鼠标移动相对y位置
-
pydirectinput.doubleClick() # 双击鼠标左键
-
pydirectinput.press('esc') # 按一下esc
-
pydirectinput.keyDown('shift')#按下shift
-
pydirectinput.keyUp('shift')#弹起shift
截至目前还未完成的功能:
1.鼠标滚轮操作
2.拖拽功能
3.热键功能
4.一些特殊shift搭配的按键比如字符(ie. '!', '@', '#'等)
5.鼠标速度设置,跟随,截图等功能
6.按键截图
# -*- coding: gbk -*-
from importlib.machinery import DEBUG_BYTECODE_SUFFIXES, BuiltinImporter
from pickle import TRUE
from turtle import Turtle
from typing import List
from unittest import TextTestRunner
import pyautogui
import time
import pydirectinput
#!/usr/bin/python
# -*- coding: UTF-8 -*-
pyautogui.PAUSE= 1
pyautogui.FAILSAFE =True
souche()
def souche():
while True:
coords = pyautogui.locateCenterOnScreen('G:/saoche/tupian1.png',confidence=0.8)
print(coords)
if coords is not None:
print("zhaodaole")
#x,y =pyautogui.center(coords)
x,y = coords
pydirectinput.moveTo(x,y)
#time.sleep(0.2)
# pydirectinput.click()
#time.sleep(0.2)
pydirectinput.press('enter')
pydirectinput.press('enter')
print('dianjile')
else:
print('meizhaodao')