Skip to content

Instantly share code, notes, and snippets.

@itn3000
Last active April 28, 2024 21:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itn3000/0726f0aab1430cd4db55a986f4928a8a to your computer and use it in GitHub Desktop.
Save itn3000/0726f0aab1430cd4db55a986f4928a8a to your computer and use it in GitHub Desktop.
how to retrieve input event with R3 and Stride3d
using System;
using Stride.Engine;
using Stride.Input;
using R3;
namespace RetrieveInputKeyEventWithR3;
public class RetriveKeyEventScript: StartupScript
{
public override Start()
{
Observable.Create<KeyEvent>(obs =>
{
var listener = new MyKeyInputListener(obs, new WeakReference<InputManager>(Input));
Input.AddListener(listener);
return listener;
})
.Subscribe(keyEvent => Log.Info($"{keyEvent.Key}, {keyEvent.RepeatCount}"));
}
internal sealed class MyKeyInputListener(Observer<KeyEvent> observer, WeakReference<InputManager> inputManager) : IInputEventListener<KeyEvent>, IDisposable
{
bool disposed = false;
public void Dispose()
{
if(!disposed)
{
disposed = true;
observer.Dispose();
if(inputManager.TryGetTarget(out var input))
{
input.RemoveListener(this);
}
}
}
public void ProcessEvent(KeyEvent inputEvent)
{
if(!disposed)
{
observer.OnNext(inputEvent);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment