程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python tutorial 99-- control mouse and keyboard module pyautogui

編輯:Python

1、 install :

pip install pyautogui

Official website :
pyautoguihttp://pyautogui.readthedocs.io/en/latest/

2、 Introduce :

PyAutoGUI Can simulate moving the mouse , Click the mouse , Drag with mouse , Key , Press and hold the key , You can also press the keyboard hotkey combination .

3、 Syntax set :

mouse :

single click :pyautogui.click()

Right click :pyautogui.click(button='right')

double-click :pyautogui.doubleClick()

Move the mouse :pg.moveTo(100,200,2)

Drag and drop the mouse :pg.dragTo(300, 400, 2, button='left')

keyboard :

enter :pyautogui.press('enter')

Press the left key :pyautogui.press('left')

Press down CTRL:pyautogui.press('ctrl') 

Shortcut keys can be used hotkey Method , Press down Ctrl + Shift + T 
pyautogui.hotkey('ctrl', 'shift', 't')

pyautogui.hotkey('ctrl', 'c')

>>> import pyautogui
# Get screen size
>>> screenWidth, screenHeight = pyautogui.size()
# Get the current coordinate position
>>> currentMouseX, currentMouseY = pyautogui.position()
# Move the mouse to the coordinate of 100,150 The location of
>>> pyautogui.moveTo(100, 150)
# Left click
>>> pyautogui.click()
# Mouse movement 10 Pixel
>>> pyautogui.moveRel(None, 10) # move mouse 10 pixels down
# Mouse click
>>> pyautogui.doubleClick()
>>> pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad) # use tweening/easing function to move mouse over 2 seconds.
# Keyboard entry Hello world! The interval is 0.25 second
>>> pyautogui.typewrite('Hello world!', interval=0.25) # type with quarter-second pause in between each key
# Key esc
>>> pyautogui.press('esc')
# shift Press down
>>> pyautogui.keyDown('shift')
# Key left direction key
>>> pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
# shift Spring up
>>> pyautogui.keyUp('shift')
# Assembly ctrl +c
>>> pyautogui.hotkey('ctrl', 'c')

4、 Other attributes :

PyAutoGUI The function increases the delay to 2.5 second :

import pyautogui
pyautogui.PAUSE = 2.5

The mouse operation :

Move

import pyautogui
width, hight = pyautogui.size()
pyautogui.moveTo(width/2, hight/2) # Basic mobility
pyautogui.moveTo(200, 200, duration=2) # The movement process continues 2s complete
pyautogui.moveTo(None, 100) # X Direction unchanged ,Y Move in the direction of 100
pyautogui.moveRel(-40, 500) # Move relative position 

PyAutoGUI Keyboard table :

‘enter’ ( or ‘return’  or  ‘\n’)

enter

‘esc’

ESC key

‘shiftleft’, ‘shiftright’

about SHIFT key

‘altleft’, ‘altright’

about ALT key

‘ctrlleft’, ‘ctrlright’

about CTRL key

‘tab’ (‘\t’)

TAB key

‘backspace’, ‘delete’

BACKSPACE 、DELETE key

‘pageup’, ‘pagedown’

PAGE UP  and  PAGE DOWN key

‘home’, ‘end’

HOME  and  END key

‘up’, ‘down’, ‘left’, ‘right’

Arrow keys

‘f1’, ‘f2’, ‘f3’….

F1…….F12 key

‘volumemute’, ‘volumedown’, ‘volumeup’

Some keyboards don't have

‘pause’

PAUSE key

‘capslock’, ‘numlock’, ‘scrolllock’

CAPS LOCK, NUM LOCK, and  SCROLL LOCK  key

‘insert’

INS or INSERT key

‘printscreen’

PRTSC  or  PRINT SCREEN key

‘winleft’, ‘winright’

Win key

‘command’

Mac OS X command key

5、 actual combat :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved