Python控制鼠标和键盘-PyAutoGUI用法详解
PyAutoGUI——讓所有GUI都自動(dòng)化
安裝命令:
pip install pyautogui
1.簡(jiǎn)介
1.1 目的
PyAutoGUI是一個(gè)純Python的GUI自動(dòng)化工具,其目的是可以用程序自動(dòng)控制鼠標(biāo)和鍵盤(pán)操作,多平臺(tái)支持(Windows,OS X,Linux)。可以用pip安裝,Github上有源代碼。
下面的代碼讓鼠標(biāo)移到屏幕中央。
import pyautogui screenWidth, screenHeight = pyautogui.size() pyautogui.moveTo(screenWidth / 2, screenHeight / 2)PyAutoGUI可以模擬鼠標(biāo)的移動(dòng)、點(diǎn)擊、拖拽,鍵盤(pán)按鍵輸入、按住操作,以及鼠標(biāo)+鍵盤(pán)的熱鍵同時(shí)按住等操作,可以說(shuō)手能動(dòng)的都可以。
pyautogui基礎(chǔ)操作樣例
import pyautogio # 獲取當(dāng)前屏幕分辨率 screenWidth, screenHeight = pyautogui.size()# 獲取當(dāng)前鼠標(biāo)位置 currentMouseX, currentMouseY = pyautogui.position()# 鼠標(biāo)移動(dòng)坐標(biāo)為100, 100位置 絕對(duì)移動(dòng) pyautogui.moveTo(100, 100)# 鼠標(biāo)左擊 pyautogui.click()# 鼠標(biāo)向下移動(dòng) 相對(duì)移動(dòng) pyautogui.moveRel(None, 10)# 鼠標(biāo)雙擊 pyautogui.doubleClick()# 用緩動(dòng)/漸變函數(shù)讓鼠標(biāo)2秒后移動(dòng)到(500, 500)位置 # use tweening/easing function to move mouse over 2 seconds. pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)# 在每次輸入之間暫停0.25秒 pyautogui.typewrite('Hello world!', interval=0.25)# 鍵盤(pán)點(diǎn)擊esc pyautogui.press('esc')# 按住shift鍵 pyautogui.keyDown('shift') pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])# 放開(kāi)shift鍵 pyautogui.keyUp('shift') pyautogui.hotkey('ctrl', 'c')PyAutoGUI鍵盤(pán)表:
| ‘enter’?(或‘return’?或?‘\n’) | 回車 |
| ‘esc’ | ESC鍵 |
| ‘shiftleft’,?‘shiftright’ | 左右SHIFT鍵 |
| ‘a(chǎn)ltleft’,?‘a(chǎn)ltright’ | 左右ALT鍵 |
| ‘ctrlleft’,?‘ctrlright’ | 左右CTRL鍵 |
| ‘tab’?(‘\t’) | TAB鍵 |
| ‘backspace’,?‘delete’ | BACKSPACE?、DELETE鍵 |
| ‘pageup’,?‘pagedown’ | PAGE UP?和?PAGE DOWN鍵 |
| ‘home’,?‘end’ | HOME?和?END鍵 |
| ‘up’,?‘down’,?‘left’,?‘right’ | 箭頭鍵 |
| ‘f1’,?‘f2’,?‘f3’…. | F1…….F12鍵 |
| ‘volumemute’,?‘volumedown’,?‘volumeup’ | 有些鍵盤(pán)沒(méi)有 |
| ‘pause’ | PAUSE鍵 |
| ‘capslock’,?‘numlock’,?‘scrolllock’ | CAPS LOCK,?NUM LOCK, 和?SCROLL?LOCK?鍵 |
| ‘insert’ | INS或INSERT鍵 |
| ‘printscreen’ | PRTSC?或?PRINT SCREEN鍵 |
| ‘winleft’,?‘winright’ | Win鍵 |
| ‘command’ | Mac OS X command鍵 |
文檔:
https://muxuezi.github.io/posts/doc-pyautogui.html
http://pyautogui.readthedocs.io/en/latest/introduction.html
http://blog.csdn.net/ibiao/article/details/54406803
http://www.chenxm.cc/post/633.html
?
總結(jié)
以上是生活随笔為你收集整理的Python控制鼠标和键盘-PyAutoGUI用法详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 监听localStorage变化(同页面
- 下一篇: 大数据挑战赛(大佬篇)