External Input for iOS

External Input for iOS External Input for iOS on the Asset Store

Introduction

External Input for iOS is designed to allow Unity iOS/tvOS developers to quickly add support for external Bluetooth devices like keyboards, accessible switches, force sensors, and more. This asset is provided free of charge and is open source. If you use the asset in your project, please acknowledge so in the credits and consider leaving a positive review on the Asset Store.

The asset provides a barebones example scene and prefab ExternalInputController object. Building the example scene and deploying to an iOS device with a connected Bluetooth accessory capable of transmitting keystrokes will log all inputted keystrokes to the console via the plugin's ExternalInputController class. How you respond to the received keystroke events is up to you!

To add the asset's capabilities to your project, simply import the asset's Plugins and Prefabs folders into your project, and drop in the ExternalInputController prefab into your scene.

Once imported into your project, you can register an event listener in any MonoBehaviour to respond to KeyCodes as they arrive, using the following convention:

void Start() {
    ExternalInputController.instance.OnExternalInputDidReceiveKey += HandleExternalInput;
}

void HandleExternalInput(KeyCode receivedKeyCode)
{
    // handle received KeyCodes here
}

void OnDestroy() {
    ExternalInputController.instance.OnExternalInputDidReceiveKey -= HandleExternalInput;
}

The plugin's native code also offers a log function to help with further expanding the supported set of key codes / characters for non-English characters and other symbols.

API Definitions

Setup

ExternalInputInterface has just one purpose - to construct the native view controller and listener which forwards KeyCode values as strings over a native-to-managed bridge to the receiving iOSExternalInputController class. Every project that uses External Input for iOS must call the setup function before doing anything else:

ExternalInputInterface.SetupExternalInput() - Initial setup function which constructs the native view controller architecture for transmitting incoming KeyCode values to the managed C# environment.

Receiving data

DidReceiveKeystroke(string receivedKey) - a simple callback for a KeyCode which doesn't include key state.

DidBeginPressForKey(string receivedKey) - a key down event occurred for the receivedKey.

DidEndPressForKey(string receivedKey) - a key up event occurred for the receivedKey.

Force values

DidChangePressForKey(string receivedKey) - a callback indicating which key is transmitting the subsequent force value.

DidReceiveForceValue(float forceValue) - a callback indicating the value of the force being applied to the receivedKey from DidChangePressForKey().

Handling system interruptions

DidCancelPressForKey(string receivedKey) - the key press was cancelled by an iOS system event ex: low memory, removal of view handling the press, app becoming inactive, etc. and is an opportunity to do some clean up if necessary.