js鼠标移动到指定位置_Python: pyautogui模块之鼠标控制
文章背景:PyAutoGUI是一個(gè)純Python的GUI自動(dòng)化工具,其目的是可以用程序自動(dòng)控制鼠標(biāo)和鍵盤操作,利用它可以實(shí)現(xiàn)自動(dòng)化任務(wù)。pyautogui模塊中包含了一些函數(shù),可以模擬鼠標(biāo)移動(dòng)、按鍵和滾動(dòng)鼠標(biāo)滾輪。本文對(duì)鼠標(biāo)控制的相關(guān)函數(shù)進(jìn)行介紹。
1 確定鼠標(biāo)位置
1.1 坐標(biāo)軸系統(tǒng)
??? pyautogui的鼠標(biāo)函數(shù)使用x,y坐標(biāo),原點(diǎn)在屏幕左上角,向右x坐標(biāo)增加,向下y坐標(biāo)增加,所有坐標(biāo)都是正整數(shù),沒有負(fù)數(shù)坐標(biāo)。
>>> import pyautogui>>> screenWidth, screenHeight = pyautogui.size() # Get the size of the primary monitor.>>> print(screenWidth, screenHeight)1366 768使用pyautogui.size()函數(shù),獲得屏幕的分辨率。根據(jù)屏幕分辨率的不同,返回值可能不同。
1.2 確定鼠標(biāo)位置
>>> currentMouseX, currentMouseY = pyautogui.position() # Get the XY position of the mouse.>>> print(currentMouseX, currentMouseY)350 465使用pyautogui.position()函數(shù),確定鼠標(biāo)當(dāng)前的位置。
2 控制鼠標(biāo)移動(dòng)
pyautogui.moveTo(x,y[,duration = t]) 將鼠標(biāo)移動(dòng)到屏幕的指定位置
pyautogui.moveRel(x,y[,duration = t]) 相對(duì)于當(dāng)前位置,移動(dòng)鼠標(biāo)。
duration為可選值,指定將鼠標(biāo)移動(dòng)到目標(biāo)位置所需的秒數(shù)。
3 控制鼠標(biāo)交互
3.1 點(diǎn)擊鼠標(biāo)
pyautogui.mouseDown() ? #按下鼠標(biāo)按鍵(左鍵)pyautogui.mouseUp() ? ? #釋放鼠標(biāo)按鍵(左鍵)pyautogui.click() ? ? ? #在當(dāng)前光標(biāo)位置,使用鼠標(biāo)左鍵點(diǎn)擊pyautogui.click([x,y,button='left/right/middle']) ?#在(x,y)處點(diǎn)擊鼠標(biāo)左鍵/右鍵/中鍵 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #但不推薦使用這種方法,下面這種方法效果更好 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #pyautogui.moveTo(x,y,duration=t) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #pyautogui.click()pyautogui.doubleClick() #雙擊鼠標(biāo)左鍵pyautogui.rightClick() ?#單擊鼠標(biāo)右鍵pyautogui.middleClick() #單擊鼠標(biāo)中鍵3.2 拖動(dòng)鼠標(biāo)
pyautogui.dragTo(x,y[,duration=t])?? ? ?#將鼠標(biāo)拖動(dòng)到指定位置pyautogui.dragRel(x,y[,duration=t]) ? ?#將鼠標(biāo)拖動(dòng)到相對(duì)當(dāng)前位置的位置 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????#x,y:水平移動(dòng),垂直移動(dòng)3.3 滾動(dòng)鼠標(biāo)
pyautogui.scroll(x) ? ? ? ?#控制窗口上下滾動(dòng)(滾動(dòng)發(fā)生在鼠標(biāo)的當(dāng)前位置) ? ? ? ? ? ? ? ? ? ? ? ? ?????????? ?? #正數(shù)表示向上滾動(dòng),負(fù)數(shù)表示向下滾動(dòng),x代表一個(gè)整型參數(shù),說明向上或向下滾動(dòng)多少單位。單位的意義在每個(gè)操作系統(tǒng)和應(yīng)用上不一樣,需要自行嘗試。
4 函數(shù)匯總
pyautogui.size() ?# Get the size of the primary monitor.pyautogui.position() ?# Get the XY position of the mouse.moveTo(x, y) ?# Moves the mouse cursor to the given x and y coordinates.moveRel(xOffset, yOffset) # Moves the mouse cursor relative to its current position.mouseDown(x, y, button) ? # Simulates pressing down the given button at the position x, y.mouseUp(x, y, button) ? ? # Simulates releasing the given button at the position x, y.click(x, y, button) ?# Simulates a click (left button by default).doubleClick() ?# Simulates a double left-button click.rightClick() ?# Simulates a right-button click.middleClick() ?# Simulates a middle-button click.dragTo(x, y) ?# Moves the mouse cursor while the left button is held down.dragRel(xOffset, yOffset) # Moves the mouse cursor relative to its current position while the left button is held down.scroll(units) ?# Simulates the scroll wheel. A positive argument scrolls up; a negative argument scrolls down.5 應(yīng)用示例
5.1 現(xiàn)在鼠標(biāo)在哪里?
在鼠標(biāo)移動(dòng)時(shí),隨時(shí)顯示x, y坐標(biāo)。
# from the book: Automate the Boring Stuff with Pythonprint('Press Ctrl-C to quit.')try: ? ?while True: ? ? ? ?# Get and print the mouse coordinates. ? ? ? ?x, y = pyautogui.position() ? ? ? ?positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4) ? ? ? ?print(positionStr,end = '') ? ? ? ?print('\b'*len(positionStr),end='',flush = True)except KeyboardInterrupt:????print('\nDone.')5.2 繪制正方形旋轉(zhuǎn)圖案
在window10的畫圖軟件中,選中鉛筆,拖動(dòng)鼠標(biāo),繪制一個(gè)正方形旋轉(zhuǎn)圖案。
# Adapted from the book: Automate the Boring Stuff with Pythonimport pyautogui, timetime.sleep(5)pyautogui.click()distance = 100while distance >0: ? ?pyautogui.dragRel(distance,0,duration=0.2) ?#move right ? ?distance = distance - 5 ? ?pyautogui.dragRel(0,distance,duration=0.2) ?#move down ? ?pyautogui.dragRel(-distance,0,duration=0.2) #move left ? ?distance = distance - 5 ? ?pyautogui.dragRel(0,-distance,duration=0.2) #move up ? ?print("Done!")參考資料:
PyAutoGUI使用(https://ddz.red/wROQJ)
PyAutoGUI——圖形用戶界面自動(dòng)化(https://zhuanlan.zhihu.com/p/41662890)
Automate the Boring Stuff with Python(https://ddz.red/y9qF5)
Python編程快速上手—讓繁瑣工作自動(dòng)化(https://ddz.red/AFTmO)
總結(jié)
以上是生活随笔為你收集整理的js鼠标移动到指定位置_Python: pyautogui模块之鼠标控制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 骁龙845相当于苹果a几
- 下一篇: astype函数_从Excel到Pyth