Receive keyboard input even when Unity.app is in the background with no focus on macOS.

Related tags

Miscellaneous unity
Overview

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}");
    }
}

Example Projects

You might also like...
A sample app demonstrates how to use Atlantis on Swift Playground (iOS/macOS)
A sample app demonstrates how to use Atlantis on Swift Playground (iOS/macOS)

Atlantis with Swift Playground A sample app demonstrates how to use Atlantis on

A macOS menu bar app that enables system-wide navigation functionality for side buttons on third-party mice.
A macOS menu bar app that enables system-wide navigation functionality for side buttons on third-party mice.

SaneSideButtons macOS mostly ignores the M4/M5 mouse buttons, commonly used for navigation. Third-party apps can bind them to ⌘+[ and ⌘+], but this on

Template repository for quickly creating boilerplate code for a SwiftUI macOS app.
Template repository for quickly creating boilerplate code for a SwiftUI macOS app.

Sidebar App This is a template repository for quickly creating boilerplate code for a SwiftUI macOS app. License This app is released into the public

This is a template repository for quickly creating boilerplate code for a SwiftUI macOS menu bar app.
This is a template repository for quickly creating boilerplate code for a SwiftUI macOS menu bar app.

Menu Bar App This is a template repository for quickly creating boilerplate code for a SwiftUI macOS menu bar app. License This app is released into t

🔬Tiny macOS app for pixel-perfect UI

SilkScreen.app tiny macOS app for pixel-perfect UI spiritual successor of GluePrint [RIP] ✦ install: download lastest version here unzip and move Silk

A macOS menubar app to temporarily prevent the Mac from sleeping.

Caffeinate A macOS menubar app to temporarily prevent the Mac from sleeping. How does it work Left-clicking the cup will toggle Caffeinate. If the cup

A macOS application for accessing the output of the SimpleAnalytics package on the desktop.
A macOS application for accessing the output of the SimpleAnalytics package on the desktop.

The SimpleAnalytics package allows you to collect data user interaction analytic data in iOS and macOS applications. This SimpleAnalytics Reader app project allows you to more easily make sense of that collected data by displaying it on your Mac.

A Swift wrapper around the CoreSymbolication private framework on macOS.

CoreSymbolication provides a very powerful system for looking up and extracting symbolic information from mach-o executables, dyld shared caches, and dSYMs.

PlayCover is a project that allows you to sideload iOS apps on macOS( currently arm, Intel support will be tested.

PlayCover is a project that allows you to sideload iOS apps on macOS( currently arm, Intel support will be tested.

Comments
  • create NativeExample

    create NativeExample


    name: Bug report about: Create a report to help us improve title: '' labels: '' assignees: ''


    Issue close #NN

    Description A summary of the changes.

    Type of Change

    • [ ] Bug Fix
    • [ ] New Feature

    Test

    • [ ] Tested the possible monitoring of the keyboard when unity.app is in the background.
    opened by fuziki 0
  • add future features

    add future features


    name: Bug report about: Create a report to help us improve title: '' labels: '' assignees: ''


    Issue close #NN

    Description A summary of the changes.

    Type of Change

    • [ ] Bug Fix
    • [ ] New Feature

    Test

    • [ ] Tested the possible monitoring of the keyboard when unity.app is in the background.
    opened by fuziki 0
  • Update README.md

    Update README.md


    name: Bug report about: Create a report to help us improve title: '' labels: '' assignees: ''


    Issue close #NN

    Description A summary of the changes.

    Type of Change

    • [ ] Bug Fix
    • [x] New Feature

    Test

    • [ ] Tested the possible monitoring of the keyboard when unity.app is in the background.
    opened by fuziki 0
Owner
fuziki
iOS, Swift, Metal, Go, Unity, etc.
fuziki
A simple framework to output to a file, url, the console, or even register notification using UserNotifications

o is a simple framework to output to a file, url, the console, or even register notification using UserNotifications. o can also get input from a file, url, or console.

OpenBytes 4 Mar 18, 2022
FocusSpace - A time-management tool to help you stay focus with your friends

FocusSpace ?? ElleHacks2022 - (Telus) First Place ?? Developers Manyi Cheng(@man

Manyi Cheng 1 Feb 13, 2022
Simple Background Fetch Tasks

Simple Background Fetch Tasks

samrez Ikram 0 Oct 30, 2021
Refresh your Live Activity with the "sound" background mode.

Live Activity Background Update Demo A demo about how to refresh Live Activity with the "sound" background mode. The Basics While in the background, L

Megabits_mzq 19 Dec 22, 2022
Type-based input validation.

Ensure Type-based input validation try Ensure<PackageIsCool>(wrappedValue: packages.ensure) Validators A Validator is a type that validates an input.

Build Passed 5 Jan 22, 2022
A ARM macOS Virtual Machine, using macOS 12's new Virtualization framework.

macOS Virtual Machine A ARM macOS Virtual Machine, using macOS 12's new Virtualization framework. I copied KhaosT's code from here, all I did is chang

Ming Chang 127 Nov 30, 2022
LinearMouse - A minimal app to get rid of mouse acceleration on macOS

LinearMouse A minimal app to get rid of mouse acceleration on macOS. Features Reverse the direction of scrolling. Linear scrolling (disable scrolling

Jiahao Lu 1.6k Jan 7, 2023
macOS status bar app to automatically fetch Git repositories.

Fetcher About macOS status bar app to automatically fetch Git repositories. License Project is released under the terms of the MIT License. Repository

JD Gadina 5 Jan 3, 2023
Swift sample app for running privileged operations on macOS using a helper tool

SwiftAuthorizationSample demonstrates how to run privileged operations on macOS using a helper tool managed by launchd. This sample was created with t

null 31 Dec 20, 2022
A native video wallpaper app for macOS

WallpapaerZ This is an wallpaper software that runs on macOS. It can bring video wallpaper to the desktop The project is coded in storyboard with swif

Celeglow 6 Dec 12, 2022