python pyautogui 控制键盘鼠标 识别图片 颜色

还有pynput库可以监听键盘鼠标

# 参考文档 https://www.pianshen.com/article/7555171409/  https://blog.csdn.net/commentq/article/details/106004115
#其他库 pynput pwinauto pywin32
import pyautogui
def run():

    pyautogui.FAILSAFE = True# 鼠标移动左上角终止程序
    size = pyautogui.size()# 获取屏幕分辨率
    print('分辨率{}'.format(size))
    now_pos = pyautogui.position()# 获取鼠标当前的位置
    print('当前位置{}'.format(now_pos))
    color = pyautogui.pixel(100, 400) #获取指定位置的色值
    print('色值{}'.format(color))
    matchColor = pyautogui.pixelMatchesColor(100, 200, (255, 255, 255), tolerance=10) #检测指定位置是否指定颜色 误差范围3
    print('色值是否匹配{}'.format(matchColor))

    pyautogui.screenshot("my_screenshot.png",region=(0,0,111, 111))# 截取指定范围的图片
    #pyautogui.click(130, 30)

    imgDetection = pyautogui.locateOnScreen('my_screenshot.png', grayscale=True)# 获取图片所在的位置范围 灰度
    print('范围{}'.format(imgDetection))
    imgCenter = pyautogui.locateCenterOnScreen('my_screenshot.png')# 获取图片所在的中心位置
    print('中心位置{}'.format(imgCenter))
    if imgCenter:
        x,y = imgCenter
        pyautogui.moveTo(x, y,2, pyautogui.easeOutQuad) # 移动鼠标 耗时2秒 开始很快,不断减速(easeInQuad、easeInOutQuad、、、)
    #pyautogui.typewrite('Hello world!', interval=0)  # 输入文本 在每次输入之间暂停0.25秒
    pyautogui.press('esc')  # 点击按键
    pyautogui.keyDown('shift') #按下按键
    pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left']) #点击一组按键
    pyautogui.keyUp('shift') #弹起按键
    pyautogui.hotkey('ctrl', 'c') #点击组合键
# Press the green button in the gutter to run the script.Helloworld!Hello world!
if __name__ == '__main__':
    run()

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
#自动打开记事本,并输入内容
# 参考文档 https://www.cnblogs.com/nmb-musen/p/11646741.html
from pywinauto.application import Application
import time
# Run a target application
app = Application().start("notepad.exe")
time.sleep(1)
# Select a menu item
app[u'无标题-记事本'].menu_select(u"帮助->关于记事本")
time.sleep(2)
app[u'关于"记事本"'][u'确定'].click()
time.sleep(1)
# inter something
app[u'无标题-记事本'].Edit.type_keys(u"测试!", with_spaces = True)
time.sleep(1)
app[u'无标题-记事本'].MenuSelect(u'文件->另存为...')
time.sleep(1)
app[u'另存为']['edit'].TypeKeys(str(time.time()) + ".txt")
time.sleep(1)
app[u'另存为'][u'保存'].click()
time.sleep(1)
app.Notepad.MenuSelect(u'文件->退出')

 

  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值