我希望鼠标光标分别锁定在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;
}
答案 0 :(得分:0)
你的应用程序中可能有一个低级钩子,它花费的时间比我认为的200ms还要长,或者你在调试器中设置了一个断点;这将导致Windows取消注册您的窗口挂钩。