【问题标题】:Can i track my mouse pos outside a SDL window?我可以在 SDL 窗口外跟踪我的鼠标位置吗?
【发布时间】:2022-05-04 22:44:45
【问题描述】:

我用 SDL 构建了一个时钟,它是一个没有边框的窗口。 现在我仍然希望能够在我的屏幕上移动我的时钟,所以我写了一个函数来移动它。基本上它等待鼠标按下输入,然后计算移动的距离,直到你释放鼠标。然后它移动窗口。问题是它只能在我的时钟窗口内获取鼠标位置,所以如果我单击左上角然后滑动到时钟窗口的右下角,我只能勉强移动它。

sPos moveClock(int event){
    if(event==-1&&mPos.x==0&&mPos.y==0){
        mPos = setPos(gvMousePos.x,gvMousePos.y);
        cout << "down" << endl;
    }
    if(event==-65){
        mPos = setPos(gvMousePos.x-mPos.x,gvMousePos.y-mPos.y);
        cout << "up" << endl;
        sPos temPos = mPos;
        mPos = setPos(0,0);
        return temPos;
    }
    return setPos(0,0);
}

我希望能够将我的时钟移动到屏幕上的任何位置,因此我需要一种方法来让我的鼠标位置即使在窗外。或者一种在鼠标按下时计算距离的方法,即使我移到 SDL 创建的窗口之外。

【问题讨论】:

  • 通过使用为您的操作系统提供的 API 而不是 SDL,您的要求肯定是可能的。 (我们需要知道您使用的是什么操作系统)另外,您是否确定要移动窗口以响应全局鼠标事件?这将产生在用户做其他事情时移动窗口的副作用,例如拖动文件或其他窗口!
  • 我想我刚刚回答了我自己的问题,因为我想移动我的窗口,我真的不需要离开我的窗口,我只需要检查鼠标是否在任何方向上移动了 1 个像素并且立即移动我的窗口位置。这样我的鼠标永远不会离开窗口,但窗口会移动。

标签: c++ sdl mouse


【解决方案1】:

SDL_CaptureMouse():

\brief 捕获鼠标,以跟踪 SDL 窗口外的输入。

\param enabled 是否开启捕获

捕获使您的应用能够全局获取鼠标事件,而不是 就在您的窗口内。 并非所有视频目标都支持此功能。 启用捕获后,当前窗口将获取所有鼠标事件, 但与相对模式不同,光标没有变化,它是 不拘泥于你的窗户。

这个函数也可以拒绝鼠标输入到其他窗口——在 你的应用程序和系统上的其他人——所以你应该使用它 功能谨慎,并在小范围内。例如,您可能想要 在用户拖动某些东西时跟踪鼠标,直到用户 释放鼠标按钮。不建议您捕获鼠标 很长一段时间,例如您的应用程序运行的整个时间。

在捕获时,鼠标事件仍会报告相对于 当前(前景)窗口,但这些坐标可能在 窗口的边界(包括负值)。捕获只是 允许用于前景窗口。如果窗口失去焦点,而 捕获,捕获将自动禁用。

启用捕获后,当前窗口将具有 SDL_WINDOW_MOUSE_CAPTURE 标志集。

\return 0 表示成功,或 -1 如果不支持。

extern DECLSPEC int SDLCALL SDL_CaptureMouse(SDL_bool enabled);

【讨论】:

    【解决方案2】:

    SDL_GetGlobalMouseState() 可能是您需要的:

    /**
     * Get the current state of the mouse in relation to the desktop.
     *
     * This works similarly to SDL_GetMouseState(), but the coordinates will be
     * reported relative to the top-left of the desktop. This can be useful if you
     * need to track the mouse outside of a specific window and SDL_CaptureMouse()
     * doesn't fit your needs. For example, it could be useful if you need to
     * track the mouse while dragging a window, where coordinates relative to a
     * window might not be in sync at all times.
     *
     * Note: SDL_GetMouseState() returns the mouse position as SDL understands it
     * from the last pump of the event queue. This function, however, queries the
     * OS for the current mouse position, and as such, might be a slightly less
     * efficient function. Unless you know what you're doing and have a good
     * reason to use this function, you probably want SDL_GetMouseState() instead.
     *
     * \param x filled in with the current X coord relative to the desktop; can be
     *          NULL
     * \param y filled in with the current Y coord relative to the desktop; can be
     *          NULL
     * \returns the current button state as a bitmask which can be tested using
     *          the SDL_BUTTON(X) macros.
     *
     * \since This function is available since SDL 2.0.4.
     *
     * \sa SDL_CaptureMouse
     */
    extern DECLSPEC Uint32 SDLCALL SDL_GetGlobalMouseState(int *x, int *y);
    

    它用全局鼠标位置填充 x 和 y 并返回当前鼠标按钮状态。

    【讨论】:

    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    相关资源
    最近更新 更多