首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用PySDL2获取鼠标位置?

如何使用PySDL2获取鼠标位置?
EN

Stack Overflow用户
提问于 2014-12-05 18:16:29
回答 2查看 1.3K关注 0票数 2

除了监听事件之外,根本不知道如何获得鼠标的位置,但是在事件队列为空的情况下,如何实现呢?

吡咯烷酮的文档建议使用sdl2.mouse.SDL_GetMouseState() (医生在这里),但是这个函数实际上需要要询问的游标的x,y坐标。同时,调用sdl2.mouse.SDL_GetCursor()返回一个游标对象,但我无法从它获得它的坐标(即。它只是包装一个C对象,所以它有一个空的.__dict__属性)。

我一直在尝试我能想到的一切,但我从来没有用C编程过。我想要生成的简单包装函数只是:

代码语言:javascript
运行
复制
def mouse_pos(self):
            #  ideally, just return <some.path.to.mouse_x_y> 
            event_queue = sdl2.SDL_PumpEvents()
            state = sdl2.mouse.SDL_GetMouseState(None, None)  # just returns 0, so I tried the next few lines
            print state
            for event in event_queue:
                if event.type == sdl2.SDL_MOUSEMOTION:
            #  this works, except if the mouse hasn't moved yet, in which case it's none        
            return [event.x, event.y] 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-16 07:22:27

SDL_GetMouseState()是SDL2 C函数的包装器。因此,您必须使用ctype从它检索值。原始SDL2函数接收两个指针(x和y)来存储光标位置。

下面的片段将为您做正确的事情:

代码语言:javascript
运行
复制
import ctypes
...
x, y = ctypes.c_int(0), ctypes.c_int(0) # Create two ctypes values
# Pass x and y as references (pointers) to SDL_GetMouseState()
buttonstate = sdl2.mouse.SDL_GetMouseState(ctypes.byref(x), ctypes.byref(y))
# Print x and y as "native" ctypes values
print(x, y)
# Print x and y as Python values
print(x.value, y.value)

`

票数 4
EN

Stack Overflow用户

发布于 2020-10-24 00:25:27

您还可以检查运动事件:

代码语言:javascript
运行
复制
if event.type == sdl2.SDL_MOUSEMOTION:
    motion = event.motion
    print(motion.x, motion.xrel, motion.y, motion.yrel)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27322219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档