Jump to content
  • Advertisement
Sign in to follow this  
QQemka

Directinput - mouse centerd after losing window focus

This topic is 735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I got a problem with Directinput. All the keydowns, info about mouse xy movement are working fine. But there is one huge problem i cant fix:

When i focus game window the mouse is ALWAYS placed in the center of game window. When i unfocus window (lets say pressing start button) the mouse is relocated to the point where i last placed it before focusing game window. And again, if i select the game it will again center the cursor, and so on.

Other issue with focus/unfocus. when game is in background my UpdateScene function is constantly receiving info from last state. So if i unfocus the game while holding right button and moving the mouse, then for entire time in backgroudn the game is receiving this input, to the moment i focus window again.

This is sooo annoying, what can i do about it?

 

Greetz

Share this post


Link to post
Share on other sites
Advertisement
DirectInput is an older part of the DirectX API.

This is sooo annoying, what can i do about it?

Track your application state and fall out of your update method early. Edited by Goliath Forge

Share this post


Link to post
Share on other sites

DirectInput is an older part of the DirectX API.



This is sooo annoying, what can i do about it?

Track your application state and fall out of your update method early.

 

I understand that, but the issue has nothing to do with DirectInput.

Share this post


Link to post
Share on other sites

Could i suggest you show some code? Like ExErvus, this shouldn't have anything to do with direct input. I know we were talking privately about this, but i figured if you wanted to show some code someone else might be able to help if i don't get around to it.

 

I would suggest first showing your window procedure function (in my tutorials the function was named WndProc). It seems that the issue is happening when the window regains focus, which it then sets the cursor position. Other clicks after the window has already regained focus don't seem to cause a problem, so it's most likely you are catching a windows message when the window regains focus, then somewhere setting a variable or directly setting the cursor position.

 

Also, search your code base for "SetCursorPos", which will set the cursor position (if that wasn't obvious, haha).

 

Those should get you looking in the right direction, if you still have a problem you can post some of that code.

Share this post


Link to post
Share on other sites

Only first click (so focus) centers the mouse. Rest of the clicks are fine. I do not use SetCursorPos or anything mouse related from winapi.

 

Thats the wndproc, nothing special here

LRESULT Framework::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_KEYDOWN:
            if (wParam == VK_ESCAPE)
                DestroyWindow(hWnd);
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

After commenting pieces of code i found out that getting input causes the problem:

void Framework::GetInputState()
{
    DIKeyboard->Acquire();
    DIMouse->Acquire();
    DIMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseCurrState);
    DIKeyboard->GetDeviceState(sizeof(keyboardState), &keyboardState);
}

The DIMouse->Acquire(); part is causing the mouse to return to the middle on re focusing.

 

Parts from initialization:

hr = DirectInput->CreateDevice(GUID_SysMouse, &DIMouse, NULL);
hr = DIMouse->SetDataFormat(&c_dfDIMouse);
hr = DIMouse->SetCooperativeLevel(gameWindow.getHwnd(), DISCL_EXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND);

And the window init:

WNDCLASSEX wc;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = wndProc;
    wc.cbClsExtra = NULL;
    wc.cbWndExtra = NULL;
    wc.hInstance = hInst;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = L"SWindow";
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if (!RegisterClassEx(&wc)) throw std::runtime_error("Error registering class");
    hwnd = CreateWindowEx(NULL, L"SWindow", wndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, hInst, NULL);

    if (!hwnd) throw std::runtime_error("Error creating window");
        
    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);

Edit: Setting DISCL_NONEXCLUSIVE in mouse instead of exclusive fixed the problem. But is that the right solution?

And if we are talkin about input, i would like my fullscreen game to milimize to the taskbar instead of going into window mode when user presses "windows start" button. I see that setting DISCL_NOWINKEY ignores the start button, and i should catch the keydown for this one button in the wndproc, right?

Edited by QQemka

Share this post


Link to post
Share on other sites

It looks like I may have been wrong assuming it had nothing to do with directinput.

 

Could you try changing:

 

hr = DIMouse->SetCooperativeLevel(gameWindow.getHwnd(), DISCL_EXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND);

 

to this:

 

hr = DIMouse->SetCooperativeLevel(gameWindow.getHwnd(), DISCL_NONEXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND);

 

One other thing to note too before you get too in tied up with DirectInput, is that DirectInput is deprecated and Microsoft has suggested using Raw Input instead:

 

https://msdn.microsoft.com/en-us/library/windows/desktop/ms645536(v=vs.85).aspx

Edited by iedoc

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!