还有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'文件->退出')

 

本文地址:https://blog.csdn.net/weixin_41452768/article/details/110940543