![](/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.