Unity Native Plugin for NSEvent.addGlobalMonitorForEvents.
Receive keyboard input even when Unity.app is in the background with no focus.
This Plugin works only on macOS.
This GIF shows that unity can detect keys entered into TextEdit.
Select "Add package from git URL" in the Unity Package Manager and enter the following path. https://github.com/fuziki/UnityAppEventMonitor.git?path=Examples/UnityAppEventMonitor/Assets/Plugins/AppEventMonitor
Copy and paste Examples/UnityAppEventMonitor/Assets/Plugins/AppEventMonitor into your Unity 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 asrc="https://raw.github.com/fuziki/UnityAppEventMonitor/develop/nity.app`.
(And Unity Hub.app
if you use)
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}");
}
}
link |
Stars: 9 |
Last commit: 1 year ago |
support unity package manager!
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics