简体   繁体   English

修改鼠标移动的问题

[英]Problems with modifying mouse movement

i'm playing around with windows hooks at the moment. 我现在正在玩窗户挂钩。 I set up a global low level mouse hook and i want to invert the mousemovement. 我设置了一个全局低级鼠标钩,我想反转鼠标运动。 For this i had several aproaches (i hope this is right, my english isn't very good, sorry). 为此,我有几个建议(我希望这是对的,对不起我的英语不好)。 The first one was to modify the lParam parameter and forward it with the CallNextHookEx function: 第一个是修改lParam参数,并使用CallNextHookEx函数将其转发:

LRESULT CALLBACK MouseInvertProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    MSLLHOOKSTRUCT* ms = NULL;
    int dx = 0; //delta x
    int dy = 0; //delta y
    if(nCode == HC_ACTION)
    {
        if(wParam == WM_MOUSEMOVE)
        {
            ms = (MSLLHOOKSTRUCT*)lParam;
            dx = ms->pt.x - g_oldMousePos.x;
            dy = ms->pt.y - g_oldMousePos.y;
            ms->pt.x = ms->pt.x - 2 * dx;
            ms->pt.y = ms->pt.y - 2 * dy;
            GetCursorPos(&g_oldMousePos);

            return CallNextHookEx(g_MouseInvertHook,nCode,wParam, (LPARAM)ms);          
        }
        else
            return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);
    }
    return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);            
}

But this has no effect at all. 但这根本没有效果。 Does someone know why? 有人知道为什么吗?
The second try was to provide an input with the modified mousecoordinates: 第二次尝试是为输入提供修改后的鼠标坐标:

LRESULT CALLBACK MouseInvertProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    MSLLHOOKSTRUCT* ms = NULL;
    int dx = 0; //delta x
    int dy = 0; //delta y
    if(nCode == HC_ACTION)
    {
        if(wParam == WM_MOUSEMOVE)
        {
            ms = (MSLLHOOKSTRUCT*)lParam;
            dx = ms->pt.x - g_oldMousePos.x;
            dy = ms->pt.y - g_oldMousePos.y;
            ms->pt.x = ms->pt.x - 2 * dx;
            ms->pt.y = ms->pt.y - 2 * dy;
            GetCursorPos(&g_oldMousePos);

            if(!g_artificialma) //g_artificialma is true when i created a artificial mouse input.
            {
                g_artificialma = true;
                mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, ms->pt.x, ms->pt.y, NULL, NULL);
                return 1;                           
            }
            else
            {               
                g_artificialma = false;
                return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);
            }           
        }
        else
            return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);
    }
    return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);
}           

With the g_artificialma variable i wanted to prevent an endless loop, because the hookproc is called when i call mouse_event. 我想使用g_artificialma变量来防止无限循环,因为在我调用mouse_event时将调用hookproc。
But this way i made my mouse stuck in the top left corner of my screen. 但是通过这种方式,我将鼠标卡在了屏幕的左上角。

The last try was to simply call the SetCursorPos function: 最后的尝试是简单地调用SetCursorPos函数:

LRESULT CALLBACK MouseInvertProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    MSLLHOOKSTRUCT* ms = NULL;
    int dx = 0; //delta x
    int dy = 0; //delta y
    if(nCode == HC_ACTION)
    {
        if(wParam == WM_MOUSEMOVE)
        {
            ms = (MSLLHOOKSTRUCT*)lParam;
            dx = ms->pt.x - g_oldMousePos.x;
            dy = ms->pt.y - g_oldMousePos.y;
            ms->pt.x = ms->pt.x - 2 * dx;
            ms->pt.y = ms->pt.y - 2 * dy;
            GetCursorPos(&g_oldMousePos);

            SetCursorPos(ms->pt.x, ms->pt.y);                       
        }
        else
            return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);
    }
    return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);            
}

This worked fine, but in 3d applications for example this caused a weird mouse movement 效果很好,但是例如在3D应用程序中,这引起了奇怪的鼠标移动
and i think this way is really ugly, because i block other hooks installed by other applications. 而且我认为这种方式确实很丑陋,因为我阻止了其他应用程序安装的其他挂钩。

So do you have any suggestions to solve that in a well working and clean way (maybe without windows hooks)? 那么,您是否有任何建议以一种良好且干净的方式解决此问题(也许没有Windows钩子)?

Your 'last try' code has no effect at all in my test : 您的“最后尝试”代码在我的测试中完全没有效果:

#define _WIN32_WINNT 0x0501

#include <iostream>
#include <windows.h>
using namespace std;

HHOOK g_MouseInvertHook;
POINT g_oldMousePos;
LRESULT CALLBACK MouseInvertProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    MSLLHOOKSTRUCT* ms = NULL;
    int dx = 0; //delta x
    int dy = 0; //delta y
    if(nCode == HC_ACTION)
    {
        if(wParam == WM_MOUSEMOVE)
        {
            ms = (MSLLHOOKSTRUCT*)lParam;
            dx = ms->pt.x - g_oldMousePos.x;
            dy = ms->pt.y - g_oldMousePos.y;
            ms->pt.x = ms->pt.x - 2 * dx;
            ms->pt.y = ms->pt.y - 2 * dy;
            GetCursorPos(&g_oldMousePos);

            SetCursorPos(ms->pt.x, ms->pt.y);                       
        }
        else
            return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);
    }
    return CallNextHookEx(g_MouseInvertHook, nCode, wParam, lParam);            
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  GetCursorPos(&g_oldMousePos);
  g_MouseInvertHook = SetWindowsHookEx(WH_MOUSE_LL, MouseInvertProc, hInstance, 0);
  MSG msg;
  while(GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}

Could you make a small example of an application (like mine) that uses just that function of yours and give the result you are describing ? 您能否举一个仅使用您的功能并给出您要描述的结果的应用程序(例如我的应用程序)的小例子?

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

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