修改鼠标移动的问题

时间:2013-10-25 11:38:51

标签: c++ winapi

我正在玩Windows钩子。我设置了一个全局低级鼠标钩子 我想要颠倒鼠标运动。为此,我有几个方法(我希望这是对的,我的英语不是很好,对不起)。第一个是修改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);            
}

但这根本没有效果。有人知道为什么吗? 第二个尝试是提供修改的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);
}           

使用g_artificialma变量我想阻止无限循环,因为当我调用mouse_event时会调用hookproc。
但是这样我就把鼠标卡在了屏幕的左上角。

最后一次尝试只是调用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);            
}

这很好用,但在3d应用程序中,例如这导致了一个奇怪的鼠标移动 我认为这种方式真的很难看,因为我阻止了其他应用程序安装的其他钩子。

那么你是否有任何建议以一种干净利落的方式解决这个问题(也许没有Windows钩子)?

1 个答案:

答案 0 :(得分:0)

我的“最后一次尝试”代码在我的测试中完全没有效果:

#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;
}

你能举出一个只使用你的那个函数并给出你描述的结果的应用程序(比如我的)的一个小例子吗?