简体   繁体   English

在WPF应用程序中从SDL2获取接收键盘鼠标事件

[英]Get receive keyboard an mouse events from SDL2 in WPF application

I'm trying to capture mouse and keyboard events from SDL2 using the SDL2-CS binding library. 我正在尝试使用SDL2-CS绑定库捕获来自SDL2的鼠标和键盘事件。 The events are polled for but these events are never raised. 对事件进行了轮询,但从未引发这些事件。

I think this is because the polling needs to happen on the UI thread. 我认为这是因为轮询需要在UI线程上进行。 I tried initializing SDL from the UI thread by calling App.Current.Dispatcher.Invoke(Init) but no events are polled. 我尝试通过调用App.Current.Dispatcher.Invoke(Init)从UI线程初始化SDL,但是没有轮询任何事件。

Basic implementation of my class: 我班的基本实现:

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;

            ...
        }
    }
}

I'm wondring if I'm invoking the Init on the UI thread wrong, or if the SDL initialization is wrong. 如果我在UI线程上调用Init错误,或者SDL初始化错误,我会很想知道。

PS Hooking with user32.dll is not desired because this code will run on non windows environments as well. 不需要使用user32.dll进行PS挂钩,因为此代码也将在非Windows环境中运行。

Looking at your code I would say your UI is blocked because ListenForEvents is not running on a different thread and invoking the Init call will run the method - that never returns - on the UI thread. 查看您的代码,我会说您的UI被阻止,因为ListenForEvents没有在其他线程上运行,并且调用Init调用将在UI线程上运行该方法(永远不会返回)。

It might be a good idea to call Init invoked, but then you should start a new thread for polling. 调用Init可能是一个好主意,但是随后您应该启动一个新的线程进行轮询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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