oblitum / Interception

The Interception API aims to build a portable programming interface that allows one to intercept and control a range of input devices.
http://oblita.com/interception
1.32k stars 271 forks source link

Mouse gets stucked #150

Closed DavidPetrasek closed 1 year ago

DavidPetrasek commented 1 year ago

OS: Win 11 IDE: VS 2022 Other libraries used in my app: GLFW, GLM, OpenGL, FreeType

I am able to press and release the right mouse button with no issues, but when I release the left mouse button for the first or second time, then mouse becomes stucked in this pressed state. Though, I can still move the cursor around, I cannot click on anything again. My application consists of the main loop which runs as a thread and on the separate thread there is the method _interceptmouse below, which is being started before the main loop like this:

std::thread t1( [this] { this->intercept_mouse(); } );
    t1.detach();
void Interception::intercept_mouse()
{
    SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);

    InterceptionContext context = interception_create_context();
    InterceptionDevice device;
    InterceptionStroke strokeReceived;

    interception_set_filter(context, interception_is_mouse,
        INTERCEPTION_FILTER_MOUSE_LEFT_BUTTON_DOWN | INTERCEPTION_FILTER_MOUSE_LEFT_BUTTON_UP |
        INTERCEPTION_FILTER_MOUSE_RIGHT_BUTTON_DOWN | INTERCEPTION_FILTER_MOUSE_RIGHT_BUTTON_UP
    );
    // I also tried INTERCEPTION_FILTER_MOUSE_ALL

    while ( interception_receive(context, device = interception_wait(context), &strokeReceived, 1) > 0 )
    {
        InterceptionMouseStroke& mouseStroke = *(InterceptionMouseStroke*)&strokeReceived;

        if (mouseStroke.state == INTERCEPTION_MOUSE_LEFT_BUTTON_DOWN)
        { 
            std::cout << "left is pressed" << std::endl;
        }

        else if (mouseStroke.state == INTERCEPTION_MOUSE_LEFT_BUTTON_UP)
        { 
           std::cout << "left is released" << std::endl;
        }

        else if (mouseStroke.state == INTERCEPTION_MOUSE_RIGHT_BUTTON_DOWN)
        { 
           std::cout << "right is pressed" << std::endl;
        }

        else if (mouseStroke.state == INTERCEPTION_MOUSE_RIGHT_BUTTON_UP)
        {
            std::cout << "right is released" << std::endl;
        }

        interception_send(context, device, &strokeReceived, 1);
    }

    interception_destroy_context(context);
}
DavidPetrasek commented 1 year ago

I have found a solution. Now I use only one flag like this:

interception_set_filter(context, interception_is_mouse, INTERCEPTION_FILTER_MOUSE_LEFT_BUTTON_DOWN);

And to listen to 3 remaining mouse states, for each state/flag I set up a new loop on a separate thread. I do not understand why it works now.