【问题标题】:Controlling mouse wheel by code通过代码控制鼠标滚轮
【发布时间】:2017-11-19 01:23:28
【问题描述】:

我正在尝试开发一个滚动文本页面的程序。 我需要通过代码控制鼠标滚轮。我该怎么做?

【问题讨论】:

  • 在 Windows 或 Linux 中?什么类型的控制?
  • 在 Windows 中。我只需要向下和向上滚动鼠标滚轮。
  • 您是否尝试在用户滚动滚轮时做出反应?或者你想滚动就像使用了滚轮但实际上没有使用它?回答者在这个细节上似乎对你有不同的理解。
  • 为什么要模拟鼠标呢?考虑到您可以通过绕过目标程序中的鼠标处理并产生最终结果来使其更加可靠,发送 WM_VSCROLL 消息。

标签: c windows scroll mouse mousewheel


【解决方案1】:

您可以使用 WINAPI (user32.dll) 中的 SendInput 函数。

UINT ScrollMouse(int scroll)
{
   INPUT input;
   POINT pos;
   GetCursorPos(&pos);

   input.type = INPUT_MOUSE;
   input.mi.dwFlags = MOUSEEVENTF_WHEEL;
   input.mi.time = NULL; //Windows will do the timestamp
   input.mi.mouseData = (DWORD)scroll; //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.
   input.mi.dx = pos.x;
   input.mi.dy = pos.y;
   input.mi.dwExtraInfo = GetMessageExtraInfo();

   return SendInput(1, &input, sizeof(INPUT));
}

【讨论】:

    【解决方案2】:

    安装pyautogui:

    pip install pyautogui
    

    示例:

    import pyautogui
    x = 100
    y = 100
    pyautogui.click(x, y)
    

    另一个示例:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 2012-06-25
      相关资源
      最近更新 更多