programing

파이썬으로 마우스 제어

new-time 2020. 5. 15. 22:45
반응형

파이썬으로 마우스 제어


파이썬에서 마우스 커서를 어떻게 제어합니까, 즉 Windows에서 특정 위치로 이동하고 클릭합니까?


 

pywin32

(필자의 경우 pywin32-214.win32-py2.6.exe )를 설치 한 후 WinXP, Python 2.6 (3.x도 테스트 됨)에서 테스트되었습니다 .

import win32api, win32con
def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)

 

PyAutoGUI

모듈을 사용해보십시오 . 멀티 플랫폼입니다.

pip install pyautogui

그래서 :

import pyautogui
pyautogui.click(100, 100)

다른 기능들도 있습니다 :

import pyautogui
pyautogui.moveTo(100, 150)
pyautogui.moveRel(0, 10)  # move mouse 10 pixels down
pyautogui.dragTo(100, 150)
pyautogui.dragRel(0, 10)  # drag mouse 10 pixels down

이것은 모든 win32con 작업을 수행하는 것보다 훨씬 쉽습니다.


마우스 또는 모든 GUI를 제어하기 위해 win32 API를 사용

win32api

하거나

ctypes

모듈을 사용할 수 있습니다다음은 win32api를 사용하여 마우스를 제어하는 ​​재미있는 예입니다.

import win32api
import time
import math

for i in range(500):
    x = int(500+math.sin(math.pi*i/100)*500)
    y = int(500+math.cos(i)*100)
    win32api.SetCursorPos((x,y))
    time.sleep(.01)

ctypes를 사용하는 클릭 :

import ctypes

# see http://msdn.microsoft.com/en-us/library/ms646260(VS.85).aspx for details
ctypes.windll.user32.SetCursorPos(100, 20)
ctypes.windll.user32.mouse_event(2, 0, 0, 0,0) # left down
ctypes.windll.user32.mouse_event(4, 0, 0, 0,0) # left up

크로스 플랫폼 PyMouse를 확인하십시오 :

https://github.com/pepijndevos/PyMouse/


다른 옵션은 크로스 플랫폼

AutoPy 패키지

를 사용하는 것 입니다. 이 패키지에는 마우스 이동을위한 두 가지 옵션이 있습니다.이 코드 스 니펫은 커서를 (200,200) 위치로 즉시 이동시킵니다.

import autopy
autopy.mouse.move(200,200)

대신 커서를 화면에서 지정된 위치로 눈에 띄게 이동하려면 smooth_move 명령을 사용할 수 있습니다.

import autopy
autopy.mouse.smooth_move(200,200)

리눅스

from Xlib import X, display
d = display.Display()
s = d.screen()
root = s.root
root.warp_pointer(300,300)
d.sync()

출처 :

파이썬 마우스는 5 줄의 코드로 움직입니다 (Linux 전용)

.


Pynput

은 Windows와 Mac 모두에서 내가 찾은 최고의 솔루션입니다. 프로그래밍이 쉽고 매우 잘 작동합니다.예를 들어

from pynput.mouse import Button, Controller

mouse = Controller()

# Read pointer position
print('The current pointer position is {0}'.format(
    mouse.position))

# Set pointer position
mouse.position = (10, 20)
print('Now we have moved it to {0}'.format(
    mouse.position))

# Move pointer relative to current position
mouse.move(5, -5)

# Press and release
mouse.press(Button.left)
mouse.release(Button.left)

# Double click; this is different from pressing and releasing
# twice on Mac OSX
mouse.click(Button.left, 2)

# Scroll two steps down
mouse.scroll(0, 2)

라이브러리를

clicks

사용하여 Windows 7에서 언제 어디서나 왼쪽 클릭하는 빠르고 더러운 기능

ctypes

. 다운로드가 필요하지 않습니다.

import ctypes

SetCursorPos = ctypes.windll.user32.SetCursorPos
mouse_event = ctypes.windll.user32.mouse_event

def left_click(x, y, clicks=1):
  SetCursorPos(x, y)
  for i in xrange(clicks):
   mouse_event(2, 0, 0, 0, 0)
   mouse_event(4, 0, 0, 0, 0)

left_click(200, 200) #left clicks at 200, 200 on your screen. Was able to send 10k clicks instantly.

Another alternative would be mouse library, I personally use it as it is relatively simple and cross-platform.

Here is how you can use it:

import mouse
# move 100 right and 100 down with a duration of 0.5 seconds
mouse.move(100, 100, absolute=False, duration=0.5)
# left click
mouse.click('left')
# right click
mouse.click('right')

Here is the source: How to Control your Mouse in Python

참고URL : https://stackoverflow.com/questions/1181464/controlling-mouse-with-python

반응형