我正在尝试使用SDL2-CS绑定库从SDL2捕获鼠标和键盘事件。对这些事件进行了调查,但这些事件从未被提出过。
我认为这是因为轮询需要在UI线程上进行。我尝试通过调用App.Current.Dispatcher.Invoke(Init)
从UI线程初始化SDL,但没有轮询事件。
我班级的基本实施:
public override void Initialize()
{
if (hooked)
{
return;
}
App.Current.Dispatcher.Invoke(Init); //Run on the UI thread
}
private void Init()
{
var init = SDL.SDL_Init(SDL.SDL_INIT_VIDEO);
if (init != 0)
{
throw new Exception("Could not initialize SDL");
}
hooked = true;
ListenForEvents();
}
private void ListenForEvents()
{
SDL.SDL_Event ev;
while (true)
{
if (SDL.SDL_PollEvent(out ev) != 1) //This is continuously trigged
{
continue;
}
switch (ev.type) //This is never reached
{
case SDL.SDL_EventType.SDL_MOUSEMOTION:
if (MouseMoved != null) { MouseMoved(this, ev.motion); }
break;
...
}
}
}
如果我在UI线程上调用Init
错误,或者SDL初始化错误,我很想知道。
P.S。不希望使用user32.dll挂钩,因为此代码也可以在非Windows环境中运行。
答案 0 :(得分:0)
查看你的代码我会说你的UI被阻止了,因为ListenForEvents没有在另一个线程上运行,并且调用Init调用将在UI线程上运行该方法 - 它永远不会返回。
调用Init调用可能是个好主意,但是你应该开始一个新的线程进行轮询。