UnityAppEventMonitor

Unity Native Plugin for NSEvent.addGlobalMonitorForEvents. Receive keyboard input even when Unity.app is in the background with no focus. This plugin supports macOS 12 or later.

helloworld

Installation

Download AppEventMonitor.unitypakcage from the release page and install it in your project. To allow monitoring of keyboard input in the background, you need to add Unity to Accessibility. Open System Preferences > Security & Privacy > Privacy > Accessibility and add Unity.app. (And Unity Hub.app if you use)

accessibility

Feature

Keyboard Events

  • key down
  • key up
  • modifier key

Mouse Events

  • left mouse down
  • left mouse dragged
  • left mouse up
  • right mouse down
  • right mouse dragged
  • right mouse up
  • mouse moved
  • scroll wheel

Touch Events

  • begin gesture
  • end gesture

Usage

Include package.

using AppEventMonitor;

Add action.

private void OnKeyDown(string key)
{
    Debug.Log($"OnKeyDown: {key}");
}
AppEventMonitorManager.OnKeyDown += OnKeyDown;

Start monitoring.

AppEventMonitorManager.Start();

Stop monitoring.

AppEventMonitorManager.Stop();

Example

using UnityEngine;
using AppEventMonitor;

public class Cube : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        AppEventMonitorManager.OnKeyDown += OnKeyDown;

        Debug.Log("Start!!");
        AppEventMonitorManager.Start();
    }

    void OnDestroy()
    {
        Debug.Log("Stop!!");
        AppEventMonitorManager.Stop();
    }

    private void OnKeyDown(string key)
    {
        Debug.Log($"OnKeyDown: {key}");
    }
}

GitHub

View Github