Creating a mouse scroll event using python
Asked Answered
J

5

8

I found this, Controlling mouse with Python, question really helpful in creating a script to move the mouse and click the mouse using Python. Is it possible to create a mouse scroll event also? Also, what about the forward and back button?

Jeavons answered 3/3, 2012 at 4:16 Comment(0)
C
6

Just for "late" viewers, this would require you to change the 4th argument dwData... I think it would look like this:

import win32api
from win32con import *

#Scroll one up
win32api.mouse_event(MOUSEEVENTF_WHEEL, x, y, 1, 0)

#Scroll one down
win32api.mouse_event(MOUSEEVENTF_WHEEL, x, y, -1, 0)

#Scroll one to the right
win32api.mouse_event(MOUSEEVENTF_HWHEEL, x, y, 1, 0)

#Scroll one to the left
win32api.mouse_event(MOUSEEVENTF_HWHEEL, x, y, -1, 0)


More information? The win-api docs are really good:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646260%28v=vs.85%29.aspx
Corrupt answered 30/4, 2013 at 2:40 Comment(0)
M
3

I really struggled with this one, so I thought I'd post my solution for Windows.

After a quick pip install pywin32, I got access to the necessary win32api & win32con, among others.

NOTE: The last time I checked, pywin32 was only supported for:

  • Python :: 2.7
  • Python :: 3.5
  • Python :: 3.6
  • Python :: 3.7
import time
import win32api
import win32con


def scroll(clicks=0, delta_x=0, delta_y=0, delay_between_ticks=0):
    """
    Source: https://learn.microsoft.com/en-gb/windows/win32/api/winuser/nf-winuser-mouse_event?redirectedfrom=MSDN

    void mouse_event(
      DWORD     dwFlags,
      DWORD     dx,
      DWORD     dy,
      DWORD     dwData,
      ULONG_PTR dwExtraInfo
    );

    If dwFlags contains MOUSEEVENTF_WHEEL,
    then dwData specifies the amount of wheel movement.
    A positive value indicates that the wheel was rotated forward, away from the user;
    A negative value indicates that the wheel was rotated backward, toward the user.
    One wheel click is defined as WHEEL_DELTA, which is 120.

    :param delay_between_ticks: 
    :param delta_y: 
    :param delta_x:
    :param clicks:
    :return:
    """

    if clicks > 0:
        increment = win32con.WHEEL_DELTA
    else:
        increment = win32con.WHEEL_DELTA * -1

    for _ in range(abs(clicks)):
        win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, delta_x, delta_y, increment, 0)
        time.sleep(delay_between_ticks)

Then, after defining

click_point = x_position, y_position

and then using

pyautogui.moveTo(x=click_point[0], y=click_point[1], duration=0.25)

to make sure that my mouse is in the correct location. I just call the above scroll function:

scroll(-4, 0.1)

to scroll down 4 ticks with a 100ms delay between ticks.

Mighty answered 26/4, 2020 at 6:15 Comment(0)
A
2

This is done much more easily with the pygame package. You will need to ensure you have the pygame package installed in your python library. To download it click the link:

http://www.pygame.org/download.shtml

It should look something like this:

import pygame

pygame.init()

def main():
    While true:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    print ("You pressed the left mouse button.")

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    quit()

main()

When you run the above code it will tell you that you have pressed the left mouse button when the left mouse button is pressed. Press Esc to stop it when your done. This method stores a list of events and tests them in the order they took place. If it finds one that was pressed it will run the code that follows it in the if statement and then removes it from the list.

To use the scroll wheel just replace the number 1 on the line:

if event.button == 1:

Replace it with 4 for scrolling forward and 5 for scrolling backwards.

Ailurophobe answered 3/2, 2015 at 10:2 Comment(1)
I think the OP is trying to create a mouse scroll event, without actually touching the mouse, rather than log the events his mouse doesToggle
L
2

If you want to scroll your page use this script. try to test out yourself. Run in cmd.

from time import sleep
import pyautogui

speed = input('How fast should i scroll')
sleepTime = input('How long untill next scroll')

pyautogui.sleep(3)

 while 0 < 10:
     pyautogui.scroll(int(speed))
     pyautogui.time.sleep(int(sleepTime))
Leopoldine answered 20/9, 2021 at 10:24 Comment(0)
C
0

to generate scroll events, use the mouse_event method with MOUSEEVENTF_WHEEL. for other events, e.g. forward/back button, it depends on how the mouse is set up and which button will trigger it. see the msdn document on this for a list of possible values.

Condolence answered 8/3, 2012 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.