Python控制鼠标和键盘-PyAutoGUI用法详解
PyAutoGUI——讓所有GUI都自動化
安裝命令:
pip install pyautogui
1.簡介
1.1 目的
PyAutoGUI是一個純Python的GUI自動化工具,其目的是可以用程序自動控制鼠標和鍵盤操作,多平臺支持(Windows,OS X,Linux)。可以用pip安裝,Github上有源代碼。
下面的代碼讓鼠標移到屏幕中央。
import pyautogui screenWidth, screenHeight = pyautogui.size() pyautogui.moveTo(screenWidth / 2, screenHeight / 2)PyAutoGUI可以模擬鼠標的移動、點擊、拖拽,鍵盤按鍵輸入、按住操作,以及鼠標+鍵盤的熱鍵同時按住等操作,可以說手能動的都可以。
pyautogui基礎操作樣例
import pyautogio # 獲取當前屏幕分辨率 screenWidth, screenHeight = pyautogui.size()# 獲取當前鼠標位置 currentMouseX, currentMouseY = pyautogui.position()# 鼠標移動坐標為100, 100位置 絕對移動 pyautogui.moveTo(100, 100)# 鼠標左擊 pyautogui.click()# 鼠標向下移動 相對移動 pyautogui.moveRel(None, 10)# 鼠標雙擊 pyautogui.doubleClick()# 用緩動/漸變函數讓鼠標2秒后移動到(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)# 鍵盤點擊esc pyautogui.press('esc')# 按住shift鍵 pyautogui.keyDown('shift') pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])# 放開shift鍵 pyautogui.keyUp('shift') pyautogui.hotkey('ctrl', 'c')PyAutoGUI鍵盤表:
| ‘enter’?(或‘return’?或?‘\n’) | 回車 |
| ‘esc’ | ESC鍵 |
| ‘shiftleft’,?‘shiftright’ | 左右SHIFT鍵 |
| ‘altleft’,?‘altright’ | 左右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’ | 有些鍵盤沒有 |
| ‘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
?
總結
以上是生活随笔為你收集整理的Python控制鼠标和键盘-PyAutoGUI用法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 监听localStorage变化(同页面
- 下一篇: 大数据挑战赛(大佬篇)