unity-raw-input
unity-raw-input copied to clipboard
Windows Raw Input wrapper for Unity game engine
Description
Wrapper over Windows Raw Input API to hook for the native input events. Allows receiving input events even when the Unity application is in background (not in focus).
Will only work on Windows platform.
Only keyboard and mouse buttons (right, left and middle) input is detected.
Installation
Download and import the package: UnityRawInput.unitypackage.
Usage
Enable Run In Background
in project's player settings; if not enabled, expect severe mouse slowdown when the application is not in focus https://github.com/Elringus/UnityRawInput/issues/19#issuecomment-1227462101.
Initialize the input service to start processing input messages; boolean argument stands for whether input messages should be handled when the application is not in focus:
RawInput.Start(true);
Add listeners for the input events:
RawInput.OnKeyUp += HandleKeyUp;
RawInput.OnKeyDown += HandleKeyDown;
private void HandleKeyUp (RawKey key) { ... }
private void HandleKeyDown (RawKey key) { ... }
Check whether specific key is currently pressed:
if (RawInput.IsKeyDown(key)) { ... }
Stop the service:
RawInput.Stop();
Don't forget to remove listeners when you no longer need them:
private void OnDisable ()
{
RawInput.OnKeyUp -= HandleKeyUp;
RawInput.OnKeyDown -= HandleKeyDown;
}
Find usage example in the project: https://github.com/Elringus/UnityRawInput/blob/master/Assets/Scripts/LogRawInput.cs.
List of the raw keys with descriptions: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes.