全局鼠标挂钩 +模拟鼠标输入

发布于 2024-08-17 00:56:58 字数 824 浏览 3 评论 0原文

我正在寻找创建一个在 XP/Vista/7 中工作的全局鼠标钩子,这将允许我访问鼠标正在输入的 X,Y 值,并在它们击中 Windows 之前修改这些值...

我也想能够在实际鼠标输入之间模拟鼠标输入...

例如,假设我们的输入如下所示:

1: 1,0 2: 2,0 3: 3,0 4:?,?

我希望能够拦截输入数字 4 并通过“if”语句运行它:如果输入的值与某些参数匹配,我想通过添加或减去它来修改该值...

USB 鼠标有一个默认轮询率为 125Hz,最大轮询率为 1000hz...

如果可能(特别是当轮询率设置小于 1000hz 时)我想在实际鼠标更新之间“注入”输入(至关重要),例如:(

在毫秒)

0008 - 1,0 0016 - 1,0 0032 - 2,0

所以鼠标正在以 125Hz 更新...我可以检测输入的频率,并每隔一个输入发送一个输入吗???例如,我可以将鼠标频率从 125hz“加倍”到 250hz,并根据我设置的任何规则模拟我自己的“中间”更新……

这些看起来合理吗?现在我正在使用 C# 工作,并且正在使用本教程中的“Gma.UserActivityMonitor”: http://www.codeproject.com/KB/cs/globalhook.aspx 但由于某种原因,我的性能受到了影响,这使得在全屏游戏中使用此代码会导致鼠标输入出现无法使用的延迟到钩子...

也许我需要用 C++ 写一些东西?任何帮助将不胜感激。谢谢!

I'm looking to create a global mouse hook that works in XP/Vista/7 which would allow me to access the X,Y values that the mouse is inputting, and modify those values before they hit Windows...

I also want to be able to simulate mouse inputs in between actual mouse inputs...

For example, lets say our inputs looked like this:

1: 1,0
2: 2,0
3: 3,0
4: ?,?

I want to be able to intercept input number 4 and run it through an 'if' statement: If the value of the input matches certain parameters, I want to modify the value by adding or subtracting from it...

A USB Mouse has a default polling rate of 125Hz and a maximum of 1000hz...

If possible (especially when the polling rate is set less than 1000hz) I would like to 'inject' inputs 'between' actual mouse updates (critical) so for example:

(in milliseconds)

0008 - 1,0
0016 - 1,0
0032 - 2,0

So the mouse is updating at 125Hz... Could I detect the frequency of inputs, and send an input every other input??? So for example I could 'double' the mouse frequency from 125hz to 250hz, and simulate my own 'in between' updates based on whatever rules I set...

Does any of these seem reasonable? Right now I am working in C# and I was working with the "Gma.UserActivityMonitor" from this tutorial here: http://www.codeproject.com/KB/cs/globalhook.aspx but for some reason I get a performance hit which makes utilizing this code with fullscreen games have an unusable lag on the mouse inputs due to the hook...

Perhaps I need to write something in C++? Any help would be greatly appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怼怹恏 2024-08-24 00:56:59

我已经构建了一个可以帮助您的库,它是一个简单的 C 库,可以在常见 Windows api 无法运行的游戏上运行< /a>.

以下示例展示了如何使用此库反转鼠标移动,它基本上只是将垂直轴上的位移乘以 -1,以便它们发生在相反的方向:

#include <interception.h>
#include "utils.h" // for process priority control

enum ScanCode
{
    SCANCODE_ESC = 0x01
};

int main()
{
    InterceptionContext context;
    InterceptionDevice device;
    InterceptionStroke stroke;

    raise_process_priority();

    context = interception_create_context();

    interception_set_filter(context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP);
    interception_set_filter(context, interception_is_mouse, INTERCEPTION_FILTER_MOUSE_MOVE);

    while(interception_receive(context, device = interception_wait(context), &stroke, 1) > 0)
    {
        if(interception_is_mouse(device))
        {
            InterceptionMouseStroke &mstroke = *(InterceptionMouseStroke *) &stroke;

            if(!(mstroke.flags & INTERCEPTION_MOUSE_MOVE_ABSOLUTE)) mstroke.y *= -1;

            interception_send(context, device, &stroke, 1);
        }

        if(interception_is_keyboard(device))
        {
            InterceptionKeyStroke &kstroke = *(InterceptionKeyStroke *) &stroke;

            interception_send(context, device, &stroke, 1);

            if(kstroke.code == SCANCODE_ESC) break;
        }
    }

    interception_destroy_context(context);

    return 0;
}    

您可能会看到在反转垂直位移之前检查了 INTERCEPTION_MOUSE_MOVE_ABSOLUTE 标志。通常操作系统使用相对坐标,但我经历过在虚拟机内鼠标坐标以绝对形式而不是相对形式工作。为简单起见,该示例仅反转相对位移。

您可以在 http://oblita.com/Interception 查看更多文档。

I've build a library that can help you with that, it's a simple c library and can work on games where common windows api would not.

The following sample shows how to invert mouse movements with this library, it basically just multiplies displacements at the vertical axis by -1 so they happen at the contrary direction:

#include <interception.h>
#include "utils.h" // for process priority control

enum ScanCode
{
    SCANCODE_ESC = 0x01
};

int main()
{
    InterceptionContext context;
    InterceptionDevice device;
    InterceptionStroke stroke;

    raise_process_priority();

    context = interception_create_context();

    interception_set_filter(context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP);
    interception_set_filter(context, interception_is_mouse, INTERCEPTION_FILTER_MOUSE_MOVE);

    while(interception_receive(context, device = interception_wait(context), &stroke, 1) > 0)
    {
        if(interception_is_mouse(device))
        {
            InterceptionMouseStroke &mstroke = *(InterceptionMouseStroke *) &stroke;

            if(!(mstroke.flags & INTERCEPTION_MOUSE_MOVE_ABSOLUTE)) mstroke.y *= -1;

            interception_send(context, device, &stroke, 1);
        }

        if(interception_is_keyboard(device))
        {
            InterceptionKeyStroke &kstroke = *(InterceptionKeyStroke *) &stroke;

            interception_send(context, device, &stroke, 1);

            if(kstroke.code == SCANCODE_ESC) break;
        }
    }

    interception_destroy_context(context);

    return 0;
}    

You may see there's a check of the INTERCEPTION_MOUSE_MOVE_ABSOLUTE flag before inverting the vertical displacements. Normally the operating system works with relative coordinates but I have experienced that inside virtual machines the mouse coordinates works in absolute form, not relative. For simplicity this sample is inverting relative displacements only.

You can check more documentation at http://oblita.com/Interception.

当爱已成负担 2024-08-24 00:56:59

希望有关获取原始鼠标输入的链接会有所帮助,它包含一个为 C# 编写的库以及 C++ 版本。它的目的是允许在 Windows 中使用多个鼠标,但希望您可以模拟它的用途来实现您想要完成的任务。

Hopefully this link on grabbing raw mouse input will be helpful, it includes a library written for C# as well as a C++ version. It is meant to enable the use of multiple mice in Windows but hopefully you can emulate what it's using for what you want to accomplish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文