项目作者: Elringus

项目描述 :
Windows Raw Input wrapper for Unity game engine
高级语言: C#
项目地址: git://github.com/Elringus/UnityRawInput.git
创建时间: 2018-04-12T18:52:50Z
项目社区:https://github.com/Elringus/UnityRawInput

开源协议:MIT License

下载


Description

Wrapper over Windows Raw Input API.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.

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.

Initialize the service to start processing input messages:

  1. 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):

  1. RawInput.WorkInBackground = true;
  2. RawInput.InterceptMessages = false;

Add listeners to handle input events:

  1. RawInput.OnKeyUp += HandleKeyUp;
  2. RawInput.OnKeyDown += HandleKeyDown;
  3. private void HandleKeyUp (RawKey key) { ... }
  4. private void HandleKeyDown (RawKey key) { ... }

Check whether specific key is currently pressed:

  1. if (RawInput.IsKeyDown(key)) { ... }

Stop the service:

  1. RawInput.Stop();

Don’t forget to remove listeners when you no longer need them:

  1. private void OnDisable ()
  2. {
  3. RawInput.OnKeyUp -= HandleKeyUp;
  4. RawInput.OnKeyDown -= HandleKeyDown;
  5. }

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.