简体   繁体   中英

Locking mouse cursor in X11

I'm trying to move and lock a mouse cursor in place, to calculate it's delta position after it was moved by the user. I'm using X11 and the XWarpPointer() function. I found a similar problem in another thread and I followed it, but it didn't work. I even looked into SDL source code but still nothing. The main problem is that after I set the position of the mouse and then the user moves it, the mouse moves back to the original position (before using XWarpPointer ).

void sp::Mouse::getDeltaMousePosition(int *posX, int *posY)
{
    if(!m_firstEnter)
    {
         getMousePosition(&m_lastX, &m_lastY);
         m_firstEnter = true;
    }

    int tempX, tempY;
    getMousePosition(&tempX, &tempY);
    *posX = tempX - m_lastX;
    *posY = tempY - m_lastY;

    Window root_window = XRootWindow(ptr_display, 0);
    SelectInput(ptr_display, root_window, KeyReleaseMask);
    XWarpPointer(ptr_display, None, root_window, 0, 0, 0, 0, m_screenCenterX, m_screenCenterY);
    XFlush(ptr_display);
    m_lastX = m_screenCenterX;
    m_lastY = m_screenCenterY;
}

I tried to compile the code on a pc with a Linux system, and it worked. Before I was compiling it in a virtual machine, so I'm guessing that the base system has priority when it comes to the mouse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM