views:

248

answers:

2

Hello. I have lost many hours trying to grab exclusively the mouse in my application and re-releasing it.

Right now, I am grabbing it correctly : the mouse cursor disappears from screen and I can read its properties fine.

However, I can't release it correctly! The mouse cursor reappears on screen but no other application is receiving any mouse clicks any more; except mine.

Here is the problematic code :

IDirectInputDevice8* mMouse;

void Win32Mouse::grab(bool grab)
{
    if (mGrabMouse == grab)
        return;

    mMouse->Unacquire();

    if (grab)
    {
        // grab = true; seems to work fine
        coopSetting &= ~DISCL_BACKGROUND;
        coopSetting &= ~DISCL_NONEXCLUSIVE;
        coopSetting |= DISCL_FOREGROUND | DISCL_EXCLUSIVE;
    }
    else
    {
        // grab = false; this surely isn't working as it should
        coopSetting &= ~DISCL_FOREGROUND;
        coopSetting &= ~DISCL_EXCLUSIVE;
        coopSetting |= DISCL_BACKGROUND | DISCL_NONEXCLUSIVE;
    }

    if( FAILED(mMouse->SetCooperativeLevel(mHwnd, coopSetting)) ) {
     std::cout << "Failed to set coop level\n";
    }

    HRESULT hr = mMouse->Acquire();
    if (FAILED(hr) && hr != DIERR_OTHERAPPHASPRIO) {
     std::cout << "Failed to aquire mouse!\n";
    }

    mGrabMouse = grab;
}

Could the problem be that I have Windows7?!

Many thanks in advance,

Bill Kotsias

A: 

You could try to use plain API calls:

HMODULE hDll = LoadLibrary("magicfuncdll.dll");
HOOKPROC hookLowLevelMouseFilter = (HOOKPROC)GetProcAddress(hDll, "_MagicFuncDLL_LowLevelMouseFilterProc@12");

// capture mouse events
HHOOK hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, hookLowLevelMouseFilter, hDll, 0);

... do your thing

// release mouse
UnhookWindowsHookEx(hMouseHook);
fmuecke
+2  A: 

Possibly it because of this
http://doc.51windows.net/Directx9%5FSDK/?url=/directx9%5Fsdk/input/ref/ifaces/idirectinputdevice9/setcooperativelevel.htm
""An application that acquires the mouse or keyboard device in exclusive mode should always unacquire the devices when it receives WM_ENTERSIZEMOVE and WM_ENTERMENULOOP messages.
Otherwise, the user cannot manipulate the menu or move and resize the window.""

May be its not totally answer
But try also to check if may be you not unacquire mouse on some other messeges.
For example on WM_LOSTFOCUS, etc.