Python包介绍

PyAutoGui -- 模拟操控鼠标键盘

鼠标位置实时获取

import os
import time
import pyautogui as pag 
try:
    while True:
        print("Press Ctrl-C to end")
        screenwidth,screenheight = pag.size()
        print(screenwidth,screenheight)
        x,y = pag.position()
        posstr = "Position:" + str(x).rjust(4)+','+str(y).rjust(4)
        print(posstr)
        time.sleep(0.2)
        os.system('cls')
except KeyboardInterrupt:
    print("end......")

详细方法

1、安全设置
  • 保护措施,避免失控
    pyautogui.FAILSAFE = true

  • 为所有的PyAutoGUI函数增加延迟。默认延迟时间是0.1秒
    pyautogui.PAUSE = 0.5

  • True=>确定x,y在屏幕上
    print(pyautogui.onScreen(0, 0))

  • Size(width=1920, height=1080)=>确定屏幕尺寸
    print(pyautogui.size())

2、移动鼠标
  • 绝对移动
    pyautogui.moveTo(100, 200)

  • 相对移动
    pyautogui.moveRel(100, 200)

  • 绝对拖拽鼠标
    pyautogui.dragTo(100, 200, button='left')

  • 相对拖拽鼠标
    pyautogui.dragRel(30, 0, 2, button='right')

  • 鼠标双击--leftClic、rightClick、middleClick、tripleClick
    pyautogui.click(x=100, y=200, clicks=2, button=pyautogui.LEFT)
    pyautogui.doubleClick(x=100, y=200, button=pyautogui.LEFT)

  • 鼠标移动到(100,200),向上滚10次,点击
    pyautogui.scroll(10, x=100, y=100)

  • 鼠标按下和抬起
    pyautogui.mouseDown()
    pyautogui.mouseUp()

3、键盘输入
  • 输入字符
    pyautogui.typewrite('Hello world!')

  • 按下键,抬起键和press(可以传列表)

pyautogui.keyDown('shift')  -- hold down the shift key
pyautogui.press('left')  -- press the left arrow key
pyautogui.press('left')  -- press the left arrow key
pyautogui.press('left')  -- press the left arrow key
pyautogui.keyUp('shift')  -- release the shift key
  • 组合热键
    pyautogui.hotkey('ctrl', 'a')
    pyautogui.hotkey('ctrl', 'c')

其他

1、提示信息
  • 提示窗口
    pyautogui.alert(text='This is an alert box.', title='Test')

  • 选择窗口
    a = pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])

  • 密码输入窗口
    a = pyautogui.password('Enter password (text will be hidden)')

  • 文本输入窗口
    a = pyautogui.prompt('input message')

posted @ 2020-05-26 13:09  _别人家的孩子  阅读(80)  评论(0编辑  收藏  举报