日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

如何利用Python獲取鼠標的實時位置_python

作者:18歲小白想成大牛 ? 更新時間: 2022-04-09 編程語言

使用Python的第三方庫pyautogui,PyAutoGUI是一個純Python的GUI自動化工具,其目的是可以用程序自動控制鼠標和鍵盤操作,多平臺支持(Windows,OS X,Linux)。

安裝

pip install pyautogui

pyautogui鼠標操作樣例

import pyautogui

# 獲取當前屏幕分辨率
screenWidth, screenHeight = pyautogui.size()

# 獲取當前鼠標位置
currentMouseX, currentMouseY = pyautogui.position()

# 2秒鐘鼠標移動坐標為100,100位置  絕對移動
#pyautogui.moveTo(100, 100,2)
pyautogui.moveTo(x=100, y=100,duration=2, tween=pyautogui.linear)

#鼠標移到屏幕中央。
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)

# 鼠標左擊一次
#pyautogui.click()
# x 
# y 
# clicks 點擊次數
# interval點擊之間的間隔
# button 'left', 'middle', 'right' 對應鼠標 左 中 右或者取值(1, 2, or 3)
# tween 漸變函數
#
pyautogui.click(x=None, y=None, clicks=1, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)

# 鼠標相對移動 ,向下移動
#pyautogui.moveRel(None, 10)
pyautogui.moveRel(xOffset=None, yOffset=10,duration=0.0, tween=pyautogui.linear)


# 鼠標當前位置0間隔雙擊
#pyautogui.doubleClick()
pyautogui.doubleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)

# 鼠標當前位置3擊
#pyautogui.tripleClick()
pyautogui.tripleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)

#右擊
pyautogui.rightClick()

#中擊
pyautogui.middleClick()

#  用緩動/漸變函數讓鼠標2秒后移動到(500,500)位置
#  use tweening/easing function to move mouse over 2 seconds.
pyautogui.moveTo(x=500, y=500, duration=2, tween=pyautogui.easeInOutQuad)

#鼠標拖拽
pyautogui.dragTo(x=427, y=535, duration=3,button='left')

#鼠標相對拖拽
pyautogui.dragRel(xOffset=100,yOffset=100,duration=,button='left',mouseDownUp=False)

#鼠標移動到x=1796, y=778位置按下
pyautogui.mouseDown(x=1796, y=778, button='left')

#鼠標移動到x=2745, y=778位置松開(與mouseDown組合使用選中)
pyautogui.mouseUp(x=2745, y=778, button='left',duration=5)

#鼠標當前位置滾輪滾動
pyautogui.scroll()
#鼠標水平滾動(Linux)
pyautogui.hscroll()
#鼠標左右滾動(Linux)
pyautogui.vscroll()

Python獲取鼠標實時位置具體實現

import time
import pyautogui as pag
 
try:
    while True:
        #獲取屏幕分辨率
        screenWidth, screenHeight = pag.size()  
        #獲取鼠標位置
        x, y = pag.position()  
        #打印分辨率和鼠標位置
        print("Screen size: (%s %s),  Position : (%s, %s)\n" % (screenWidth, screenHeight, x, y))  
        #間隔一秒顯示位置
        time.sleep(1)  
except KeyboardInterrupt:
    print('end')

結果展示

屏幕的分辨率1920×1080,間隔1s移動鼠標后的結果:

總結

原文鏈接:https://blog.csdn.net/qq_36584673/article/details/117777658

欄目分類
最近更新