SavinaRoja / PyUserInput

A module for cross-platform control of the mouse and keyboard in python that is simple to install and use.
GNU General Public License v3.0
1.07k stars 244 forks source link

How do I specify that I want to click where the mouse pointer is, and not move the mouse anwhere? #127

Open NSC9 opened 2 years ago

NSC9 commented 2 years ago

Currently this script moves the mouse to the top left corner of my screen.

I am trying to write a script that continuously sends left mouse clicks at the current mouse cursor position while the left mouse button is held down and stops clicking on release of the left mouse button.

from pynput.mouse import Listener as MouseListener
from pynput.keyboard import Listener as KeyboardListener

def on_press(key):
    print("Key pressed: {0}".format(key))

def on_release(key):
    print("Key released: {0}".format(key))

def on_move(x, y):
    print("Mouse moved to ({0}, {1})".format(x, y))
from pymouse import PyMouse
import time
sleep_time = 2
def on_click(x, y, button, pressed):
    if pressed:
        m = PyMouse()
        print('Mouse clicked at ({0}, {0}) with {2}'.format(x, y, button))
        m.click(0, 0, 1)
        time.sleep(sleep_time)
        m.click(0, 0, 1)

        print('Mouse clicked at ({0}, {0}) with {2}'.format(x, y, button))
    else:
        print('Mouse released at ({0}, {0}) with {2}'.format(x, y, button))

def on_scroll(x, y, dx, dy):
    print('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))

# Setup the listener threads
keyboard_listener = KeyboardListener(on_press=on_press, on_release=on_release)
mouse_listener = MouseListener(on_move=on_move, on_click=on_click, on_scroll=on_scroll)

# Start the threads and join them so the script doesn't end early
keyboard_listener.start()
mouse_listener.start()
keyboard_listener.join()
mouse_listener.join()

I also plan to try while pressed: instead of if pressed: