![](/img/trans.png)
[英]Is it possible to handle Keyboard/Mouse events application wide in WPF?
[英]Get receive keyboard an mouse events from SDL2 in WPF application
我正在尝试使用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初始化错误,我会很想知道。
不需要使用user32.dll进行PS挂钩,因为此代码也将在非Windows环境中运行。
查看您的代码,我会说您的UI被阻止,因为ListenForEvents没有在其他线程上运行,并且调用Init调用将在UI线程上运行该方法(永远不会返回)。
调用Init可能是一个好主意,但是随后您应该启动一个新的线程进行轮询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.