basic programming python: Python realize the mouse function automatically move randomly on the screen

This article describes the Python implementation of automatic random mouse movement on the screen function, a good reference value, we want to help. What are you waiting for? Xiao Bian together to follow up to see it
had wanted to control the mouse automatically move to prevent the company's computer automatically hibernate strategy, however, did not realize what the eggs with, or will sleep. But still share out of it. win10 systems.

First of all you want to install a few third-party packages: pymouse, pyhook, pywin32, pyuserinput. Do not look at the code values ​​quoted pymouse and win32api, if these are not installed, the error will be running.

Look at the code below it.

from pymouse import PyMouse
from win32api import GetSystemMetrics
import random
import time
 
m = PyMouse()
m.position()
 
width = GetSystemMetrics(0)
heigth = GetSystemMetrics(1)
 
m.move(100, 100)
while True:
 x = random.randint(0, width)
 y = random.randint(0, heigth)
 m.move(x, y)
 time.sleep(random.randint(3, 5))

Supplementary information: python control mouse movement pyautogui || PyMouse automation

METHOD 1 pyautogui

Installation · pip install pyautogui

File

Basic operation instruction

gui.PAUSE=0.5 #每次函数调用后暂停0.5秒
gui.FAILSAFE=True #启动自动防故障功能

Note that this will pyautogui library rename gui, it is ease of use. This library can handle the computer screen, mouse, keyboard and other computer hardware resources. Here, we start processing the computer screen began to introduce:

Obtained by the screen size W, H = gui.size (), W, H are represented on the screen width, height; screenshot can be im = gui.screenshot (), im screenshot image is taken; if the screen to be obtained the (x, y) color coordinate values, may be RGB = GetScreenShow () getpixel ((x, y)); to determine whether the color coordinate of a given value, with the following code:

def is_screen_locate_color(x,y,r,b,g):
im=GetScreenShow()
return gui.pixelMatchesColor(x,y,(r,b,g))

If there is a picture on the screen, how do we get the picture size and position on the screen it? We have the following code

def get_png_all_location(PNGfile):#
return list(gui.locateAllOnScreen(PNGfile))

Returns such a list: [(x1, y1, w1, h1), (x2, y2, w2, h2), (x3, y3, w3, h3).], The screen is only one such picture, this list only one [(x1, y1, w1, h1)], this image represents the position (x1, y1) and size (w1, h1).

Secondly, we look at how the mouse is accused. We can () returns the mouse position (x, y) with gui.position; click with analog:

def click_position(x,y,buttonkey='left'):#模拟点击(默认左键)
gui.click(x,y,button=buttonkey)

Double-click with the left mouse button simulation:

def double_click(x,y):
gui.doubleClick(x,y)

Simulation pressing the left mouse drag (relative to the original position) by:

def drag_rel(dx,dy):
gui.dragRel(dx,dy,duration=0.2)

Simulate pressing the left mouse button and drag (absolute position) with:

def drag_to(x,y):
gui.dragTo(x,y,duration=0.2)

Simulated moving the mouse to the X, Y coordinates:

def move_to(x,y):
gui.moveTo(x,y)

Simulation window scroll by:

def scroll_window(n):
gui.scroll(n)#n为正表示向上滚动,为负表示向下滚动

Simulated moving the mouse to the X, Y coordinates:

def move_to(x,y):
gui.moveTo(x,y)

Simulation window scroll by:

def scroll_window(n):
gui.scroll(n)#n为正表示向上滚动,为负表示向下滚动

The third is an analog keyboard, below are definitions of some common keys on the keyboard:

'a', 'A', '1','!', '@', '#',等等 单个字符的键
'enter'(or 'return' or '\n') 回车键
'esc' Esc 键
'shiftleft', 'shiftright' 左右 Shift 键
'altleft', 'altright' 左右 Alt 键
'ctrlleft', 'ctrlright' 左右Ctrl 键
'tab'(or '\t') Tab 键
'backspace', 'delete' Backspace 和 Delete 键
'pageup', 'pagedown' Page Up 和 Page Down 键
'home', 'end' Home 和 End 键
'up', 'down', 'left', 'right' 上下左右箭头键
'f1', 'f2', 'f3',等等 F1 至 F12 键

Send strings gui.typewrite (textstr),

按下 - key gui.keyDown (keyname),

释放 - key gui.keyUp (keyname),

Key gui.press (char), #

The hotkey combination: ctrl + c with gui.hotkey (keyname, char).

A plurality of hot key combination can be sequentially input a plurality of parameters, such as: Ctrl-alt-shift-s

You can enter

gui.hotkey('ctrl','alt','shift','s')。

鼠标移动import pyautogui
pyautogui.moveRel(50,50,durtion=1) //根据当前位置, 相对移动鼠标指针 durtion移动时间
pyautogui.position() //获取当前鼠标位置

Content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data every day to explain the timing of Python programmers technology, and share some learning methods need to pay attention to small details
Here Insert Picture Description
than this Python realize the full contents of the mouse on the screen automatically feature is the random movement of small series for everyone to share the

Published 20 original articles · won praise 0 · Views 3608

Guess you like

Origin blog.csdn.net/chengxun02/article/details/104999165