Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elringus/unity-raw-input
Windows Raw Input wrapper for Unity game engine
https://github.com/elringus/unity-raw-input
background event-driven input native unity3d unity3d-plugin
Last synced: about 2 months ago
JSON representation
Windows Raw Input wrapper for Unity game engine
- Host: GitHub
- URL: https://github.com/elringus/unity-raw-input
- Owner: elringus
- License: mit
- Created: 2018-04-12T18:52:50.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-01-19T17:17:16.000Z (8 months ago)
- Last Synced: 2024-05-23T07:54:50.298Z (4 months ago)
- Topics: background, event-driven, input, native, unity3d, unity3d-plugin
- Language: C#
- Homepage: https://elringus.me/using-windows-rawinput-api-in-unity/
- Size: 94.7 KB
- Stars: 210
- Watchers: 9
- Forks: 15
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
## Description
Wrapper over [Windows Raw Input API](https://msdn.microsoft.com/en-us/library/windows/desktop/ms645536(v=vs.85).aspx) 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 input is detected.
## Installation
Download and import the package: [UnityRawInput.unitypackage](https://github.com/elringus/unity-raw-input/raw/main/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/unity-raw-input/issues/19#issuecomment-1227462101.
![](https://i.gyazo.com/9737f66dafa9c705601521b82f40fc5a.png)
Initialize the service to start processing input messages:
```csharp
RawInput.Start();
```Optionally, configure whether input messages should be handled when the application is not in focus and whether handled messages should be intercepted (both disabled by default):
```csharp
RawInput.WorkInBackground = true;
RawInput.InterceptMessages = false;
```Add listeners to handle input events:
```csharp
RawInput.OnKeyUp += HandleKeyUp;
RawInput.OnKeyDown += HandleKeyDown;private void HandleKeyUp (RawKey key) { ... }
private void HandleKeyDown (RawKey key) { ... }
```Check whether specific key is currently pressed:
```csharp
if (RawInput.IsKeyDown(key)) { ... }
```Stop the service:
```csharp
RawInput.Stop();
```Don't forget to remove listeners when you no longer need them:
```csharp
private void OnDisable ()
{
RawInput.OnKeyUp -= HandleKeyUp;
RawInput.OnKeyDown -= HandleKeyDown;
}
```Find usage example in the project: https://github.com/elringus/unity-raw-input/blob/main/Assets/Runtime/LogRawInput.cs.
List of the raw keys with descriptions: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes.