已解决
python读取控制鼠标键盘
来自网友在路上 150850提问 提问时间:2023-09-24 12:11:39阅读次数: 50
最佳答案 问答题库508位专家为你答疑解惑
目录
一,工具
二,鼠标
1,实时显示鼠标位置
2,控制移动鼠标
3,控制点击鼠标
三,键盘
1,单键输入
2,组合键输入
四,实用demo
demo1
demo2
demo3
demo4
一,工具
pyautogui库
命令:pip3 install pyautogui==0.9.50
如果不指定版本,可能会在使用时报错: module 'pyscreeze' has no attribute 'locateOnWindow'
二,鼠标
1,实时显示鼠标位置
import pyautogui
while True:print(pyautogui.position())
例如,我定位到一个关键的复制按钮的位置:Point(x=830, y=200)
2,控制移动鼠标
pyautogui.moveTo(x=830,y=200)
3,控制点击鼠标
pyautogui.click(1477,77,button='left')
pyautogui.click(830,200,button='left')
三,键盘
1,单键输入
pyautogui.press('a')
pyautogui.press('b')
2,组合键输入
pyautogui.hotkey('ctrl', 'v')
四,实用demo
demo1
假设现在打开了很多网页,每个网页的固定位置有个工具按钮,单击之后在固定位置有个复制按钮,我们要依次遍历每个网页,点击工具和复制按钮,并把文本收集汇总起来。
import pyperclip
import time
import pyautoguidef clickFirstPage():pyautogui.click(600, 25, button='left') #第1个网页页签的坐标def closeFirstPage():clickFirstPage()pyautogui.hotkey('ctrl', 'w')pageNum = 5 #网页的数量
clickFirstPage()
for i in range(0,pageNum):pyautogui.click(600, 77, button='left') # url的坐标pyautogui.hotkey('ctrl', 'c')pyautogui.click(5400, 1400, button='left') # 记事本的坐标pyautogui.hotkey('ctrl', 'v')pyautogui.press('enter')pyautogui.click(1477,77,button='left') #工具按钮的坐标time.sleep(0.1)pyautogui.click(830,200,button='left') #复制按钮的坐标time.sleep(0.1)pyautogui.click(5400,1400,button='left') #记事本的坐标pyautogui.hotkey('ctrl', 'v')pyautogui.press('enter')closeFirstPage()
demo2
场景和demo1相同。如果获取系统的剪切板,则不需要记事本,直接print
import pyperclip
import time
import pyautoguidef clickFirstPage():pyautogui.click(20, 20, button='left') #第1个网页页签的坐标def closeFirstPage():clickFirstPage()pyautogui.hotkey('ctrl', 'w')stopPage = 'edge://favorites/?id=337'clickFirstPage()
for i in range(0, 30):pyautogui.click(600, 77, button='left') # url的坐标pyautogui.hotkey('ctrl', 'c')time.sleep(0.1)txt = pyperclip.paste()if txt == stopPage:breakprint(txt)time.sleep(0.1)pyautogui.click(1477,77,button='left') #工具按钮的坐标time.sleep(0.1)pyautogui.click(830,200,button='left') #复制按钮的坐标time.sleep(0.1)txt = pyperclip.paste()print(txt)closeFirstPage()
demo3
在demo1的基础上,新增预处理功能,以及用固定页面作为停止条件的功能。
预处理:先把所有网页刷几遍,让资源加载出来。
import pyperclip
import time
import pyautoguidef clickFirstPage():pyautogui.click(20, 20, button='left') #第1个网页页签的坐标def closeFirstPage():clickFirstPage()pyautogui.hotkey('ctrl', 'w')def copyOnePage(stopPage):pyautogui.click(600, 77, button='left') # url的坐标pyautogui.hotkey('ctrl', 'c')time.sleep(0.05)txt = pyperclip.paste()if txt == stopPage:return Falsepyautogui.click(1477, 77, button='left') # 工具按钮的坐标time.sleep(0.05)pyautogui.click(830, 200, button='left') # 复制按钮的坐标time.sleep(0.05)txt2 = pyperclip.paste()if txt2 != '' and 'm3u8' not in txt and txt !=txt2:print(txt)print(txt2)closeFirstPage()return Truedef init():clickFirstPage()for i in range(0, 60):pyautogui.hotkey('ctrl', 'tab')time.sleep(0.3)clickFirstPage()stopPage = 'edge://favorites/?id=337'
init()
for i in range(0, 30):if copyOnePage(stopPage) ==False:break
demo4
在demo3的基础上,新增对于没有加载出来的网页,额外等待的功能
import pyperclip
import time
import pyautoguidef clickFirstPage():pyautogui.click(20, 20, button='left') #第1个网页页签的坐标def closeFirstPage():clickFirstPage()pyautogui.hotkey('ctrl', 'w')def copyOnePage(stopPage):pyautogui.click(600, 77, button='left') # url的坐标pyautogui.hotkey('ctrl', 'c')time.sleep(0.05)txt = pyperclip.paste()if txt == stopPage:return 0pyautogui.click(1477, 77, button='left') # 工具按钮的坐标time.sleep(0.05)pyautogui.click(830, 200, button='left') # 复制按钮的坐标time.sleep(0.05)txt2 = pyperclip.paste()if txt2 != '' and 'm3u8' not in txt and txt !=txt2:print(txt)print(txt2)return 1return 2def init():clickFirstPage()for i in range(0, 60):pyautogui.hotkey('ctrl', 'tab')time.sleep(0.3)clickFirstPage()stopPage = 'edge://favorites/?id=337'
init()
for i in range(0, 30):ret = copyOnePage(stopPage)if ret == 0:breakelif ret == 1:closeFirstPage()else:time.sleep(1)copyOnePage(stopPage)closeFirstPage()
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"python读取控制鼠标键盘":http://eshow365.cn/6-12767-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: 子比主题v7.4绕授权接口源码
- 下一篇: 【数据集标注】上古软件LabelImg的保姆级使用教程