python随机鼠标移动

python随机鼠标移动,python,python-2.7,Python,Python 2.7,我想在指定的矩形区域内随机移动鼠标(受坐标x1、y1、x2、y2、x3、y3、x4、y4的限制)。 动作应平稳、随机,而不仅仅是直线,在规定的时间内随机上/下/左/右/等 你能给我一个可以学习的例子吗? 非常感谢将随机平滑移动限制在一个矩形内,我将尝试使用随机变化的系数。对于执行这些移动,有一个第三方软件包调用,允许您控制鼠标或键盘,它是跨平台的。使用pip安装它: $ pip install PyUserInput 为了使动作流畅,你可以尝试9000在他的回答中提出的方法。此代码仅适用于Wi

我想在指定的矩形区域内随机移动鼠标(受坐标x1、y1、x2、y2、x3、y3、x4、y4的限制)。 动作应平稳、随机,而不仅仅是直线,在规定的时间内随机上/下/左/右/等

你能给我一个可以学习的例子吗?
非常感谢

将随机平滑移动限制在一个矩形内,我将尝试使用随机变化的系数。

对于执行这些移动,有一个第三方软件包调用,允许您控制鼠标或键盘,它是跨平台的。使用pip安装它:

$ pip install PyUserInput

为了使动作流畅,你可以尝试9000在他的回答中提出的方法。

此代码仅适用于Windows。您可以尝试随机_移动函数中的参数,以获得更好的结果。祝你好运

import ctypes
import random
import time
import math

def move_mouse(pos):
    x_pos, y_pos = pos
    screen_size = ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1)
    x = 65536L * x_pos / screen_size[0] + 1
    y = 65536L * y_pos / screen_size[1] + 1
    return ctypes.windll.user32.mouse_event(32769, x, y, 0, 0)

def random_movement(top_left_corner, bottom_right_corner, min_speed=100, max_speed=200):
    '''speed is in pixels per second'''

    x_bound = top_left_corner[0], bottom_right_corner[0]
    y_bound = top_left_corner[1], bottom_right_corner[1]

    pos = (random.randrange(*x_bound),
                    random.randrange(*y_bound))

    speed = min_speed + random.random()*(max_speed-min_speed)
    direction = 2*math.pi*random.random()

    def get_new_val(min_val, max_val, val, delta=0.01):
        new_val = val + random.randrange(-1,2)*(max_val-min_val)*delta
        if new_val<min_val or new_val>max_val:
            return get_new_val(min_val, max_val, val, delta)
        return new_val

    steps_per_second = 35.0
    while True:
        move_mouse(pos)
        time.sleep(1.0/steps_per_second) 

        speed = get_new_val(min_speed, max_speed, speed)
        direction+=random.randrange(-1,2)*math.pi/5.0*random.random()

        new_pos = (int(round(pos[0]+speed*math.cos(direction)/steps_per_second)),
               int(round(pos[1]+speed*math.sin(direction)/steps_per_second)))

        while new_pos[0] not in xrange(*x_bound) or new_pos[1] not in xrange(*y_bound):
            direction  = 2*math.pi*random.random()
            new_pos = (int(round(pos[0]+speed*math.cos(direction)/steps_per_second)),
               int(round(pos[1]+speed*math.sin(direction)/steps_per_second)))
        pos=new_pos

控制鼠标指针是高度特定于平台的。你在用什么平台?谢谢,看起来不错。但我想随机移动鼠标(例如,当你在屏幕上移动鼠标时,你永远不会得到相同的移动。所以我不需要“漂亮”的曲线,它应该只是模拟用户的鼠标)。不幸的是,我不是python英雄,所以任何例子都值得高度赞赏。谢谢你(酷!)非常感谢你。我将从这个例子中学到一些东西。为什么在
x
y
赋值中
65536L
?uint16是从0到65535。0表示最左边的边,65535表示最右边的边。
random_movement((300,300),(600,600))