js鼠标移动到指定位置_Python: pyautogui模块之鼠标控制
文章背景:PyAutoGUI是一個純Python的GUI自動化工具,其目的是可以用程序自動控制鼠標和鍵盤操作,利用它可以實現自動化任務。pyautogui模塊中包含了一些函數,可以模擬鼠標移動、按鍵和滾動鼠標滾輪。本文對鼠標控制的相關函數進行介紹。
1 確定鼠標位置
1.1 坐標軸系統
??? pyautogui的鼠標函數使用x,y坐標,原點在屏幕左上角,向右x坐標增加,向下y坐標增加,所有坐標都是正整數,沒有負數坐標。
>>> import pyautogui>>> screenWidth, screenHeight = pyautogui.size() # Get the size of the primary monitor.>>> print(screenWidth, screenHeight)1366 768使用pyautogui.size()函數,獲得屏幕的分辨率。根據屏幕分辨率的不同,返回值可能不同。
1.2 確定鼠標位置
>>> currentMouseX, currentMouseY = pyautogui.position() # Get the XY position of the mouse.>>> print(currentMouseX, currentMouseY)350 465使用pyautogui.position()函數,確定鼠標當前的位置。
2 控制鼠標移動
pyautogui.moveTo(x,y[,duration = t]) 將鼠標移動到屏幕的指定位置
pyautogui.moveRel(x,y[,duration = t]) 相對于當前位置,移動鼠標。
duration為可選值,指定將鼠標移動到目標位置所需的秒數。
3 控制鼠標交互
3.1 點擊鼠標
pyautogui.mouseDown() ? #按下鼠標按鍵(左鍵)pyautogui.mouseUp() ? ? #釋放鼠標按鍵(左鍵)pyautogui.click() ? ? ? #在當前光標位置,使用鼠標左鍵點擊pyautogui.click([x,y,button='left/right/middle']) ?#在(x,y)處點擊鼠標左鍵/右鍵/中鍵 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #但不推薦使用這種方法,下面這種方法效果更好 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #pyautogui.moveTo(x,y,duration=t) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #pyautogui.click()pyautogui.doubleClick() #雙擊鼠標左鍵pyautogui.rightClick() ?#單擊鼠標右鍵pyautogui.middleClick() #單擊鼠標中鍵3.2 拖動鼠標
pyautogui.dragTo(x,y[,duration=t])?? ? ?#將鼠標拖動到指定位置pyautogui.dragRel(x,y[,duration=t]) ? ?#將鼠標拖動到相對當前位置的位置 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????#x,y:水平移動,垂直移動3.3 滾動鼠標
pyautogui.scroll(x) ? ? ? ?#控制窗口上下滾動(滾動發生在鼠標的當前位置) ? ? ? ? ? ? ? ? ? ? ? ? ?????????? ?? #正數表示向上滾動,負數表示向下滾動,x代表一個整型參數,說明向上或向下滾動多少單位。單位的意義在每個操作系統和應用上不一樣,需要自行嘗試。
4 函數匯總
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 應用示例
5.1 現在鼠標在哪里?
在鼠標移動時,隨時顯示x, y坐標。
# 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 繪制正方形旋轉圖案
在window10的畫圖軟件中,選中鉛筆,拖動鼠標,繪制一個正方形旋轉圖案。
# 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——圖形用戶界面自動化(https://zhuanlan.zhihu.com/p/41662890)
Automate the Boring Stuff with Python(https://ddz.red/y9qF5)
Python編程快速上手—讓繁瑣工作自動化(https://ddz.red/AFTmO)
總結
以上是生活随笔為你收集整理的js鼠标移动到指定位置_Python: pyautogui模块之鼠标控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 骁龙845相当于苹果a几
- 下一篇: 冰箱传感器坏了的症状是什么