def move_mouse_to(x, y):
"""Function to simulate realistic mouse movements in python. The objective of this
will be to take in coordinates x,y and move them in a realistic manner. We
will be passing in an x,y, that is already 'random' so this function will
move to the exact x,y"""
# takes current mouse location and stores it
while(True):
try:
curr_x, curr_y = pyautogui.position()
# calculates the distance from current position to target position
distance = int(((x - curr_x)**2 + (y - curr_y)**2)**0.5)
# calculates a random time to make the move take based on the distance
duration_of_move = (distance * random.random() / 2000) + 0.5
# move the mouse to our position and takes the time of our duration just
# calculated
pyautogui.moveTo(x, y, duration_of_move, pyautogui.easeInOutQuad)
#pyautogui.moveTo(x, y, duration_of_move, pyautogui.easeOutElastic)
break
except:
print('paused for 10 seconds')
time.sleep(10)
python类moveTo()的实例源码
def basic_api():
x, y = pg.size()
print x, y
'''
pg.moveTo(300,300,2)
pg.moveTo(300,400,2)
pg.moveTo(500,400,2)
pg.moveTo(500,300,2)
pg.moveTo(300,300,2)
#pg.moveTo(300,500,2)
'''
# pg.click(100,100)
word = [u'??', u'???']
pos = [452, 321]
pg.moveTo(pos[0], pos[1])
pg.click()
pg.typewrite(word[0])
def example():
screenWidth, screenHeight = pg.size()
currentMouseX, currentMouseY = pg.position()
pg.moveTo(500, 550)
pg.click()
pg.moveRel(None, 10) # move mouse 10 pixels down
pg.doubleClick()
# pg.moveTo(500, 500, duration=2, tween=pyautogui.tweens.easeInOutQuad) # use tweening/easing function to move mouse over 2 seconds.
pg.typewrite('Hello world!', interval=0.25) # type with quarter-second pause in between each key
pg.press('esc')
pg.keyDown('shift')
pg.press(['left', 'left', 'left', 'left', 'left', 'left'])
pg.keyUp('shift')
pg.hotkey('ctrl', 'c')
delta_y = 50
def test_check(self):
for i in range(2):
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)
# M_obj = MouseMove()
# M_obj.move_left()
# M_obj.move_up()
# M_obj.move_right()
# M_obj.move_down()
# M_obj.test_check() # Just for the debuggin purpose
# M_obj.on_command_move("left")
# M_obj.on_command_move("up")
# M_obj.on_command_move("right")
# M_obj.on_command_move("down")
# M_obj.on_command_move("click")
# M_obj.scroll_down()
def move(previous_position, a, b):
gui.moveTo((screenX - 1) - ((screenX - 1) * a / (frameX - 1)), (screenY - 1) * b / (frameY - 1), 0.02, pyautogui.easeInOutQuad)
#pass
def performAction( yp, rc, bc, perform):
if perform :
cursor[0] = 4*(yp[0]-110)
cursor[1] = 4*(yp[1]-120)
if yp[0]>110 and yp[0]<590 and yp[1]>120 and yp[1]<390:
pyautogui.moveTo(cursor[0],cursor[1])
elif yp[0]<110 and yp[1]>120 and yp[1]<390:
pyautogui.moveTo( 8 , cursor[1])
elif yp[0]>590 and yp[1]>120 and yp[1]<390:
pyautogui.moveTo(1912, cursor[1])
elif yp[0]>110 and yp[0]<590 and yp[1]<120:
pyautogui.moveTo(cursor[0] , 8)
elif yp[0]>110 and yp[0]<590 and yp[1]>390:
pyautogui.moveTo(cursor[0] , 1072)
elif yp[0]<110 and yp[1]<120:
pyautogui.moveTo(8, 8)
elif yp[0]<110 and yp[1]>390:
pyautogui.moveTo(8, 1072)
elif yp[0]>590 and yp[1]>390:
pyautogui.moveTo(1912, 1072)
else:
pyautogui.moveTo(1912, 8)
if rc[0]!=-1 and bc[0]!=-1:
if abs(rc[0]-bc[0])<35 and abs(rc[1]-bc[1])<20:
pyautogui.click(button = 'left')
def performAction( yp, rc, bc, perform):
if perform :
cursor[0] = 4*(yp[0]-110)
cursor[1] = 4*(yp[1]-120)
if yp[0]>110 and yp[0]<590 and yp[1]>120 and yp[1]<390:
pyautogui.moveTo(cursor[0],cursor[1])
elif yp[0]<110 and yp[1]>120 and yp[1]<390:
pyautogui.moveTo( 8 , cursor[1])
elif yp[0]>590 and yp[1]>120 and yp[1]<390:
pyautogui.moveTo(1912, cursor[1])
elif yp[0]>110 and yp[0]<590 and yp[1]<120:
pyautogui.moveTo(cursor[0] , 8)
elif yp[0]>110 and yp[0]<590 and yp[1]>390:
pyautogui.moveTo(cursor[0] , 1072)
elif yp[0]<110 and yp[1]<120:
pyautogui.moveTo(8, 8)
elif yp[0]<110 and yp[1]>390:
pyautogui.moveTo(8, 1072)
elif yp[0]>590 and yp[1]>390:
pyautogui.moveTo(1912, 1072)
else:
pyautogui.moveTo(1912, 8)
if rc[0]!=-1 and bc[0]!=-1:
if abs(rc[0]-bc[0])<35 and abs(rc[1]-bc[1])<20:
pyautogui.click(button = 'left')
def behaviour1() :
pyautogui.moveRel (56, 56, duration = 0.27)
pyautogui.moveRel (11, 11, duration = 0.56)
pyautogui.moveTo (21, 21, duration = 0.11)
def position():
while True:
event = pad.get_event()
position = event.get_tip()
x, y = position
pyautogui.moveTo(x, y)
if event.type == Event.SPOCK:
return { 'success': 'true' }
def click_pic(cls, picname):
coords = cls.get_coords_pic(picname)
if not coords:
raise Exception("Could not find button")
pa.moveTo(coords)
time.sleep(2)
pa.click(coords)
def click(self, button="LEFT", y=None, x=None):
pyautogui.moveTo(x, y)
pyautogui.click(button=self.mouse_buttons.get(button, "left"))
def drag_screen_region_to_screen_region(self, start_screen_region=None, end_screen_region=None, duration=1, offset=(0, 0), game=None):
start_screen_region_coordinates = self._extract_screen_region_coordinates(start_screen_region, game=game)
end_screen_region_coordinates = self._extract_screen_region_coordinates(end_screen_region, game=game)
pyautogui.moveTo(*start_screen_region_coordinates)
coordinates = (end_screen_region_coordinates[0] + offset[0], end_screen_region_coordinates[1] + offset[1])
pyautogui.dragTo(*coordinates, duration=duration)
def click_and_type(cls, x, y, text):
# pyautogui.moveTo(x, y, duration=0.1)
pyautogui.click(x, y, clicks=2, interval=cls.short_wait/2.0, duration=cls.short_wait)
sleep(cls.short_wait * 2.0)
pyautogui.typewrite(text, interval=cls.short_wait)
def startClick(self, event):
if self.running == 0 and not (self.leftclick == 0 and self.middleclick==0 and self.rightclick == 0):
self.running = 1
event.widget.config(text="Stop")
currentMouseX, currentMouseY = pyautogui.position()
pyautogui.moveTo(currentMouseX, currentMouseY+50)
threading.Thread(target=self.startLoop, args=()).start()
else:
self.running = 0
event.widget.config(text="Start")
time.sleep(0.2)
return
def handle_mouse_absolute_intent(self, message):
self.speak('moving mouse now')
#X = message.data.get('X')
#Y = message.data.get('Y')
#pyautogui.moveTo(X, Y)
def moveTo(x,y):
startCoords = (pyautogui.position())
endCoords = (x, y)
mouseCalc = MouseMovementCalculator(3, 3, mouseSpeed, 10 * mouseSpeed)
coordsAndDelay = mouseCalc.calcCoordsAndDelay(startCoords, endCoords)
pyautogui.moveTo(startCoords[0], startCoords[1])
for x, y, delay in coordsAndDelay:
delay = delay / 10000
delay += random.random()
pyautogui.moveTo(x, y)
#time.sleep(delay)
def moveClick(x,y, button=0):#moves to random X,Y of found match of template
moveTo(x,y)
RandTime.randTime(0,0,0,0,0,2)
if button != 0:
pyautogui.mouseDown(button='left')
RandTime.randTime(0,1,0,0,2,9)#time between click
pyautogui.mouseUp(button='left')
def randMove(x1,y1,x2,y2,button):
x, y = genCoords(x1,y1,x2,y2)
if button == 1:
moveClick(x,y,1)
elif button == 3:
moveClick(x,y,3)
else:
moveTo(x,y)
def close_app():
touch(**HOME_BUTTON)
touch(x=1300, y=700) # App Switcher
pyautogui.moveTo(1150, 400) # Move Cursor Over GO
pyautogui.dragTo(1150, 100, button='left')
wait(1)
def move_to(self, x, y):
ag.moveTo(x + self.x, y + self.y)
def move_left(self):
p_v = self.x
self.x -= 100
if self.x >= 0:
pyautogui.moveTo(self.x, self.y, duration=0.25) # Moving in the left area
print(self.x)
else:
self.x = p_v # Sets to zero
def move_right(self):
p_v = self.x # Previous value
self.x += 90 # Incrementing the value of x axis
if self.x >= 0:
pyautogui.moveTo(self.x, self.y, duration=0.25) # Moving in the left area
print(self.x) # Debugging
else:
self.x = p_v # Going back to the previous value
# Below Function will be responsible for the Moving of the cursor dup (positive y - axis)
def move_up(self):
p_v = self.y
self.y -= 90
if self.y >= 0:
pyautogui.moveTo(self.x, self.y, duration=0.25)
print(self.y) # Debugging purpose
else:
self.y = p_v # setting to the previous value of the y axis
# Below Function will be responsible for the Moving of the cursor down (negative y - axis)
def move_down(self):
p_v = self.y
self.y += 90
if self.y >= 0:
pyautogui.moveTo(self.x, self.y, duration=0.25)
print(self.y) # Debugging purpose
else:
self.y = p_v # setting to the previous value of the y axis