Global Mouse Hook + Simulate Mouse Inputs

安稳与你 提交于 2019-11-30 10:31:14

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.

pepper_chico

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!