gpt4 book ai didi

c++ - 修改鼠标移动的问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:00:17 25 4
gpt4 key购买 nike

我现在正在玩 windows hooks。我设置了一个全局低级鼠标钩子(Hook)我想反转鼠标移动。为此我有几个方法(我希望这是正确的,我的英语不是很好,抱歉)。第一个是修改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);
}

但这一点作用都没有。有人知道为什么吗?
第二次尝试是使用修改后的鼠标坐标提供输入:

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 应用程序中这会导致奇怪的鼠标移动
而且我认为这种方式真的很难看,因为我阻止了其他应用程序安装的其他 Hook 。

那么您有什么建议可以以良好的工作和干净的方式解决这个问题(也许没有 windows hooks)?

最佳答案

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

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

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

关于c++ - 修改鼠标移动的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19588808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com