Python's Automated Mouse movement

Source: Internet
Author: User

############################### #用GUI自动化控制键盘和鼠标 ###############################

‘‘‘

http://pyautogui.readthedocs.org/Introduction to Pyautogui functions

There are several ways to prevent or restore GUI automation problems:

1. Close the program by Unregistering: Ctrl-alt-del

2, pause and automatic failure prevention device:

(1), Pyautogui. pause=1.5 each Pyautogui function call waits a second and a half after the action is executed

Non-Pyautogui instructions do not stop

(2), Pyautogui has automatic fault-proof function, move the mouse to the upper left corner of the screen, automatic anti-failure function will stop the program.

You can set Pyautogui. Failsafe=false, prohibit this feature

‘‘‘

############################### #移动鼠标 ##############################################

# # # #pyautogui. MoveTo () function moves the mouse immediately to the specified position on the screen: x, y coordinates, duration specifies the required number of seconds

Import Pyautogui

For I in range (10):

Pyautogui.moveto (100,100,duration=0.25)

Pyautogui.moveto (200,100,duration=0.25)

Pyautogui.moveto (200,200,duration=0.25)

Pyautogui.moveto (100,200,duration=0.25)

# # # #pyautogui. Moverel () function moves the mouse relative to the current position

Import Pyautogui

For I in range (10):

Pyautogui.moverel (100,0,duration=0.25)

Pyautogui.moverel (0,100,duration=0.25)

Pyautogui.moverel ( -100,0,duration=0.25)

Pyautogui.moverel (0,-100,duration=0.25)

# # # #pyautogui. Position () function to determine the current position of the mouse

Pyautogui.position ()

############################### #控制鼠标交互 ##############################################

################################## #点击鼠标 ###############################################

Import Pyautogui

Pyautogui.click (100,150,button= ' left ') # # # #在相应位置点击鼠标左键

‘‘‘

Pyautogui.click (100,150,button= ' right ') # # # #将在相应位置点击鼠标右键

Pyautogui.mousedown () # # # #按下鼠标按键

Pyautogui.mousedown () # # # #释放鼠标按键

Pyautogui.doubleclick () # # # #执行双击鼠标左键

Pyautogui.rightclick () # # # #执行双击右键

Pyautogui.middleclick () # # # #执行双击中键

‘‘‘

################################## #拖放鼠标 ######################################################

‘‘‘

Pyautogui.dragto () and Pyautogui.dragrel () functions: Drag the mouse to a new position, or position relative to the current position

The parameters of Dragto () and Dragrel () are the same as MoveTo () and Moverel: x-coordinate/horizontal movement, y-coordinate/vertical movement, and optional time interval

‘‘‘

Import Pyautogui,time

Time.sleep (5)

Pyautogui.click ()

distance=200

While distance>0:

Pyautogui.dragrel (distance,0,duration=0.2)

Distance=distance-5

Pyautogui.dragrel (0,distance,duration=0.2)

Pyautogui.dragrel (-distance,0,duration=0.2)

Distance=distance-5

Pyautogui.dragrel (0,distance,duration=0.2)

################################## #滚动鼠标 ######################################################

Import Pyperclip

Numbers= "

For I in range (200):

Numbers=numbers+str (i) + ' \ n '

Pyperclip.copy (Numbers)

# # #得到文本

Import Time,pyautogui

Time.sleep (5);p Yautogui.scroll (100)

############################### #获取屏幕快照 #####################################################

Import Pyautogui

>>> Im=pyautogui.screenshot ()

>>> Im.save (R ' C:\Users\shenlu\Desktop\one.jpg ')

# # #im中包含一个屏幕快照的Image对象

############################### #分析屏幕快照 ##############################################

>>> Import Pyautogui

>>> Im=pyautogui.screenshot ()

>>> Im.getpixel ((50,200))

(255, 255, 255)

>>> Pyautogui.pixelmatchescolor (50,200, (255,255,254))

False

>>> Pyautogui.pixelmatchescolor (50,200, (255,255,255))

True

############################### #图像识别 ##############################################

>>> Pyautogui.locateonscreen (R ' C:\Users\shenlu\Desktop\one.jpg ')

>>> A=pyautogui.locateonscreen (R ' C:\Users\shenlu\Desktop\one.jpg ')

>>> Print a

None

‘‘‘

1, Pyautogui.locateonscreen () not successfully identified, return none;

Successful recognition, returning the x, y coordinates, width, and height of the left when the image was first discovered

2. If the image can be found in multiple places on the screen, the Locateallonscreen () function returns a list

3, Pyautogui.center () returns the x, y coordinates of the center of the region

‘‘‘

>>>pyautogui.locateonscreen (' Submit.png ')

(643,745,70,29)

>>>pyautogui.center ((643,745,70,29))

(678,759)

>>>pyautogui.click ((678,759))

############################### #控制键盘 ##############################################

############################# #通过键盘发送一个字符串 ##################################

‘‘‘

Pyautogui.typewrite () function sends virtual keys to the computer

‘‘‘

>>> Import Pyautogui

>>> Pyautogui.click (500,500);p yautogui.typewrite (' Hello world! ')

################################## #键名 ###############################################

Import Pyautogui

Pyautogui.typewrite ([' A ', ' B ', ' left ', ' left ', ' X ', ' Y ']) # # # #以下的调用表示按a键, b key, two-time arrow, X and Y keys

>>> Pyautogui.typewrite ([' A ', ' B ', ' left ', ' left ', ' X ', ' Y '])

Xyab>>>

>>> Pyautogui.typewrite ([' A ', ' B ', ' left ', ' X ', ' Y '])

Axyb>>>

‘‘‘

Keyboard key string meaning

' A ', ' B ', ' A ', ' B ', ' C ', ' 1 ', ' 2 ', ' 3 ' single-character keys

'! ', ' @ ', ' # ', and so on

' Enter ' (or ' return ' or ' \ n ') enter

' ESC ' ESC key

......

‘‘‘

################################## #按下和释放键盘 ####################################

#‘‘‘

#pyautogui. KeyDown () and Pyautogui,keyup () will send virtual keystrokes and releases to the computer

#‘‘‘

Import Pyautogui

Pyautogui.keydown (' shift ');p yautogui.press (' 4 ');p yautogui.keyup (' Shift ')

##################################### #热键组合 #######################################

Import Pyautogui

Pyautogui.hotkey (' Ctrl ', ' C ') # #即Ctrl-C key

‘‘‘

Equivalent to

Pyautogui.keydown (' Ctrl ')

Pyautogui.keydown (' C ')

Pyautogui.keyup (' C ')

Pyautogui.keyup (' Ctrl ')

‘‘‘

Import Pyautogui,time

Def commentafterdelay ():

Pyautogui.click (500,500)

Time.sleep (2)

Pyautogui.hotkey (' Shift ', ' 3 ')

Pyautogui.typewrite (' in idle,alt-3-comments out a line. ')

Commentafterdelay ()

##################################### #现在鼠标在哪里 #######################################

# # # #显示鼠标坐标位置, give the RGB color of this pixel

Import Pyautogui

print ' Press Ctrl-c to quit '

Try

While True:

X,y=pyautogui.position ()

Positionstr= ' x: ' +str (x). Rjust (4) + ' Y: ' +str (y). Rjust (4)

Pixelcolor=pyautogui.screenshot (). GetPixel ((x, y))

Positionstr + = ' RGB: (' +str (Pixelcolor[0]). Rjust (3)

Positionstr + = ', ' +str (Pixelcolor[1]). Rjust (3)

Positionstr + = ', ' +str (Pixelcolor[2]). Rjust (3) + ') '

Print Positionstr,

print ' \b ' *len (positionstr),

Except Keyboardinterrupt:

print ' \ndone. '

########################## #每隔10秒移动下鼠标 ###########################

Import Pyautogui,time

Try

While True:

Time.sleep (10)

Pyautogui.moverel ( -100,100,duration=0.25)

Except Keyboardinterrupt:

print ' \ndone. '

Python's Automated Mouse movement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.