2

I want the mouse cursor to lock on a specified point on X or Y axis respectively. I managed to accomplish this with low level mouse proc and keyboard proc (need the keyboard proc as input for what movement the user desires the mouse to follow - vertical or horizontal). However, my problem is that -while in locked movement, say horizontal with F7- when I hit a mouse button or use the mouse wheel, the locked movement is released for some weird reason I am unable to understand and I obviously don't want it to be released unless the user says so (ie by hitting F6). Here is the code you can check it and see the problem I am talking about :

#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;
}
Hallion
  • 53
  • 5

1 Answers1

0

You probably have a low-level hook in your application which takes longer than i believe 200ms or you have a breakpoint set in debugger; this will cause Windows to unregister your window hook.

Siana
  • 145
  • 4
  • The above code is all the code included in my application, so I don't have any other hook than the ones mentioned. Neither do I run it with breakpoints. Weirdest part is that for example the command SetCursorPos(key.pt.x,p.y); is still executed when the horizontal lock is released (by a mouse click or mouse wheel etc). From my little experience with winapi, I would guess that something messes up in the hook chain, but I'm not sure what or why. BTW, the above code is functional, you can compile and run it to see the problem at hand if you are interested. – Hallion Aug 24 '14 at 08:26