使用python创建鼠标滚动事件

问题描述 投票:2回答:4

我发现了这个。用Python控制鼠标,问题真的很有帮助,在创建一个脚本来移动鼠标和点击鼠标使用Python。 是否也可以创建一个鼠标滚动事件? 另外,前进和后退按钮怎么样?

python windows mouse scrollbar mouseevent
4个回答
4
投票

对于 "迟到 "的观众来说,这需要你改变第四个参数。dwData... 我想应该是这样的

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)


更多信息?win-api的文档真的很好。http:/msdn.microsoft.comen-uslibrarywindowsdesktopms646260%28v=vs.85%29.aspx。

2
投票

使用 pygame 软件包可以更方便地完成这项工作。你需要确保你的pygame包已经安装在你的python库中。要下载它,请点击链接。

http:/www.pygame.orgdownload.shtml

它看起来应该是这样的。

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()

当你运行上面的代码时 当你按下鼠标左键时,它会告诉你,你已经按下了鼠标左键。完成后按Esc键停止。这个方法存储一个事件列表,并按照事件发生的顺序进行测试。如果它发现有一个事件被按下,它将在if语句中运行后面的代码,然后从列表中删除它。

要使用滚轮,只需替换行中的数字1。

if event.button == 1:

替换成4表示向前滚动,5表示向后滚动。


0
投票

要生成滚动事件,请使用MOUSEEVENTF_WHEEL的mouse_event方法。对于其他事件,例如向前向后的按钮,这取决于鼠标是如何设置的,哪个按钮会触发它。msdn文件中的可能值列表。.


0
投票

我真的很纠结这个问题,所以我想把我的解决方法贴出来。Windows

经过快速的 pip install pywin32,我得到了必要的访问 win32api &amp。win32con,等等。

注。 我最后一次检查时, pywin32 只支持。

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


def scroll(clicks=0, delta_x=0, delta_y=0, delay_between_ticks=0):
    """
    Source: https://docs.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)

然后,在定义了

click_point = x_position, y_position

然后用

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

以确保我的鼠标在正确的位置。 我只是调用上面的 scroll 功能。

scroll(-4, 0.1)

向下滚动4个刻度,刻度之间有100ms的延迟。

© www.soinside.com 2019 - 2024. All rights reserved.