About Magic & Smart Keyboard support for iPad & iPhone
The built-in UnityEngine.Input system does not recognize external keyboards for iPad & iPhone. This asset allows you to get key input from Magic & Smart Keyboard, as well as from other external bluetooth keyboards.
DISCLAIMER:
Unity somehow supports external keyboards for old iOS versions. For example, UnityEngine.Input works on my iPhone 5 running on iOS 12, but does not work on iPad running on iOS 15. Maybe it is a bug and it will be resolved soon. I hope.
System requirements:
- Any version of Unity.
- iOS/iPadOS 13.4+ (plugin code will be ignored on old devices).
Usage:
- Check Demo.scene and Demo.cs script first.
- Add ExternalInput script to your scene (it should be active).
- Use UniversalInput instead of UnityEngine.Input (or use ExternalInput in addition to UnityEngine.Input).
- Both UniversalInput and ExternalInput have the same syntax as UnityEngine.Input (GetKeyDown, GetKey, GetKeyUp, anyKey)
- Alternatively you can subscribe to ExternalInput.OnKeyDown and ExternalInput.OnKeyUp events to get UIKey objects representation from iOS.
- For any class description please refer to XML-comments in code.
Additional setup in Xcode:
- After building your Xcode project, find ExternalInput.xib, navigate to Target Membership section and check Unity-iPhone.
Notes:
- Modifier keys (like Shift, Control, Alt and other) may be received both from UIKey.keyCode (UIKeyboardHIDUsage) and UIKey.modifierFlags (UIKeyModifierFlags).
- Secondary keys (like !, @, #, $, %, ^, &, * and other) may be received from UIKey.characters (string). At the same time, UIKey.keyCode will be a primary key with a modifier in UIKey.modifierFlags.
- If you need to change key mapping, please edit KeyCodeMapping.KeyCodeDictionary.
References:
KeyCode
UIKey
UIKeyboardHIDUsage
UIKeyModifierFlags
DISCLAIMER:
Unity somehow supports external keyboards for old iOS versions. For example, UnityEngine.Input works on my iPhone 5 running on iOS 12, but does not work on iPad running on iOS 15. Maybe it is a bug and it will be resolved soon. I hope.
System requirements:
- Any version of Unity.
- iOS/iPadOS 13.4+ (plugin code will be ignored on old devices).
Usage:
- Check Demo.scene and Demo.cs script first.
- Add ExternalInput script to your scene (it should be active).
- Use UniversalInput instead of UnityEngine.Input (or use ExternalInput in addition to UnityEngine.Input).
- Both UniversalInput and ExternalInput have the same syntax as UnityEngine.Input (GetKeyDown, GetKey, GetKeyUp, anyKey)
- Alternatively you can subscribe to ExternalInput.OnKeyDown and ExternalInput.OnKeyUp events to get UIKey objects representation from iOS.
- For any class description please refer to XML-comments in code.
Additional setup in Xcode:
- After building your Xcode project, find ExternalInput.xib, navigate to Target Membership section and check Unity-iPhone.
Notes:
- Modifier keys (like Shift, Control, Alt and other) may be received both from UIKey.keyCode (UIKeyboardHIDUsage) and UIKey.modifierFlags (UIKeyModifierFlags).
- Secondary keys (like !, @, #, $, %, ^, &, * and other) may be received from UIKey.characters (string). At the same time, UIKey.keyCode will be a primary key with a modifier in UIKey.modifierFlags.
- If you need to change key mapping, please edit KeyCodeMapping.KeyCodeDictionary.
References:
KeyCode
UIKey
UIKeyboardHIDUsage
UIKeyModifierFlags