python control mouse pynput code example
Example 1: pynput mouse click
from pynput.mouse import Button, Controller
mouse = Controller()
print('The current pointer position is {0}'.format(
mouse.position))
mouse.position = (10, 20)
print('Now we have moved it to {0}'.format(
mouse.position))
mouse.move(5, -5)
mouse.press(Button.left)
mouse.release(Button.left)
mouse.click(Button.left, 2)
mouse.scroll(0, 2)
Example 2: pynput mouse click
from pynput.mouse import Listener
def on_move(x, y):
print('Pointer moved to {0}'.format(
(x, y)))
def on_click(x, y, button, pressed):
print('{0} at {1}'.format(
'Pressed' if pressed else 'Released',
(x, y)))
if not pressed:
return False
def on_scroll(x, y, dx, dy):
print('Scrolled {0}'.format(
(x, y)))
with Listener(
on_move=on_move,
on_click=on_click,
on_scroll=on_scroll) as listener:
listener.join()