简体   繁体   English

使用mouse_event在C ++中无加速地移动鼠标

[英]Moving the mouse without acceleration in C++ using mouse_event

Right now when I try a loop that contains something like: 现在当我尝试一个包含以下内容的循环:

mouse_event(MOUSEEVENTF_MOVE,dx,dy,0,0);

The mouse tends to move more than (dx,dy). 鼠标往往移动超过(dx,dy)。 Researching this online, I think it's because of the acceleration applied by the operating system. 在线研究,我认为这是因为操作系统加速了。 How can I move the mouse an absolute amount? 如何将鼠标移动绝对量?

MOUSEEVENTF_ABSOLUTE seems to maybe be what I'm looking for, but I can't see how to use it. MOUSEEVENTF_ABSOLUTE似乎可能正是我正在寻找的,但我看不出如何使用它。

I've tried: 我试过了:

mouse_event(MOUSEEVENTF_ABSOLUTE || MOUSEEVENTF_MOVE,dx,dy,0,0);

but that doesn't work either. 但这也不起作用。 I'd prefer to use mouse_event rather than SetCursorPos or other methods, what should I do? 我更喜欢使用mouse_event而不是SetCursorPos或其他方法,我该怎么办? Thanks. 谢谢。

The coordinates are not pixel coordinates and must be normalized. 坐标不是像素坐标,必须进行标准化。

#include <Windows.h>
#include <tchar.h>

int WINAPI _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow ) {
  INPUT input = {0};

  int screenX = GetSystemMetrics( SM_CXVIRTUALSCREEN );
  int screenY = GetSystemMetrics( SM_CYVIRTUALSCREEN );

  for( unsigned int i = 0; i < 10; i++ ) {
    input.mi.dx = i*10 * ( 65535/screenX );
    input.mi.dy = i*10 * ( 65535/screenY );
    input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_MOVE;
    input.type = INPUT_MOUSE;

    SendInput( 1, &input, sizeof INPUT );
    Sleep( 1000 );
  }

  return ERROR_SUCCESS;
}

I have used SendInput() here instead of mouse_event() because the latter has been superseded according to the docs. 我在这里使用了SendInput()而不是mouse_event(),因为后者已根据文档被取代。 If you do want to convert it back, the parameters should be much the same. 如果您确实想要将其转换回来,那么参数应该大致相同。

from winuser.h 来自winuser.h

#define MOUSEEVENTF_MOVE        0x0001
#define MOUSEEVENTF_ABSOLUTE    0x8000

MOUSEEVENTF_MOVE || MOUSEEVENTF_ABSOLUTE MOUSEEVENTF_MOVE || MOUSEEVENTF_ABSOLUTE is the same thing as 0x0001 || 0x8001 MOUSEEVENTF_MOVE || MOUSEEVENTF_ABSOLUTE0x0001 || 0x8001相同 0x0001 || 0x8001 which evaluates to true , which just happens to be 1! 0x0001 || 0x8001 ,其值为true ,恰好为1!

Try it again with MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE 使用MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE再试一次 MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE and it will probably work. MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE它可能会起作用。

Edit: after looking at the docs a bit, it appears that either you want MOUSEEVENTF_ABSOLUTE all by itself. 编辑:稍微查看文档后,看起来要么你想要MOUSEEVENTF_ABSOLUTE本身。 or you need to account for the fact that the range of values it is looking for is 0-65535 scaled over the entire display. 或者你需要考虑到它所寻找的值范围是0-65535在整个显示器上缩放的事实。

If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates between 0 and 65,535. 如果指定了MOUSEEVENTF_ABSOLUTE值,则dx和dy包含0到65,535之间的标准化绝对坐标。 The event procedure maps these coordinates onto the display surface. 事件过程将这些坐标映射到显示表面上。 Coordinate (0,0) maps onto the upper-left corner of the display surface, (65535,65535) maps onto the lower-right corner. 坐标(0,0)映射到显示表面的左上角,(65535,65535)映射到右下角。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM