gpt4 book ai didi

c++ - 鼠标锁定水平/垂直移动

转载 作者:可可西里 更新时间:2023-11-01 10:46:00 26 4
gpt4 key购买 nike

我希望鼠标光标分别锁定在 X 或 Y 轴上的指定点上。我设法用低级鼠标 proc 和键盘 proc 实现了这一点(需要键盘 proc 作为用户希望鼠标跟随的移动的输入 - 垂直或水平)。但是,我的问题是 - 在锁定移动时,比如用 F7 水平 - 当我按下鼠标按钮或使用鼠标滚轮时,锁定移动会因为一些我无法理解的奇怪原因而被释放,我显然不想除非用户这么说(即按 F6),否则它将被释放。这是代码,您可以检查它并查看我正在谈论的问题:

#define _WIN32_WINNT 0x0501

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

bool block = false;
POINT p;
HHOOK hHook,hHook2;
unsigned int status = 0;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT key;
memcpy(&key,(void*)lParam,sizeof(KBDLLHOOKSTRUCT));

if( wParam == WM_KEYDOWN)
{
if(key.vkCode == VK_F7)
{
//Horizontal only
if (GetCursorPos(&p)) status = 1;
}
else if(key.vkCode == VK_F8)
{
//Vertical only
if (GetCursorPos(&p)) status = 2;
}
else if(key.vkCode == VK_F6)
{
//Normal
status = 0;
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}

LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
MSLLHOOKSTRUCT key;
memcpy(&key,(void*)lParam,sizeof(MSLLHOOKSTRUCT));

if(wParam == WM_MOUSEMOVE)
{
if(status == 1)
{
SetCursorPos(key.pt.x,p.y);
}
else if(status == 2)
{
SetCursorPos(p.x,key.pt.y);
}
else
{
return CallNextHookEx(hHook2, nCode, wParam, lParam);
}
}
else
{
return CallNextHookEx(hHook2, nCode, wParam, lParam);
}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
cout<<"F7 to allow Horizontal moving ONLY"<<endl;
cout<<"F8 to allow Vertical moving ONLY"<<endl;
cout<<"F6 to move freely"<<endl;
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0);
hHook2 = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, hInstance, 0);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

最佳答案

您的应用程序中可能有一个低级 Hook ,我认为它花费的时间超过 200 毫秒,或者您在调试器中设置了断点;这将导致 Windows 注销您的窗口 Hook 。

关于c++ - 鼠标锁定水平/垂直移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465382/

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