using System; using System.Runtime.InteropServices; using UnityEngine.InputSystem.Utilities; namespace UnityEngine.InputSystem.LowLevel { /// /// A single character text input event. /// /// /// Text input does not fit the control-based input model well and thus is /// represented as its own form of input. A device that is capable of receiving /// text input (such as ) receives text input events /// and should implement in order for the /// input system to be able to relay these events to the device. /// [StructLayout(LayoutKind.Explicit, Size = InputEvent.kBaseEventSize + 4)] public struct TextEvent : IInputEventTypeInfo { public const int Type = 0x54455854; [FieldOffset(0)] public InputEvent baseEvent; /// /// Character in UTF-32 encoding. /// [FieldOffset(InputEvent.kBaseEventSize)] public int character; public FourCC typeStatic => Type; public static unsafe TextEvent* From(InputEventPtr eventPtr) { if (!eventPtr.valid) throw new ArgumentNullException(nameof(eventPtr)); if (!eventPtr.IsA()) throw new InvalidCastException(string.Format("Cannot cast event with type '{0}' into TextEvent", eventPtr.type)); return (TextEvent*)eventPtr.data; } public static TextEvent Create(int deviceId, char character, double time = -1) { ////TODO: detect and throw when if character is surrogate var inputEvent = new TextEvent { baseEvent = new InputEvent(Type, InputEvent.kBaseEventSize + 4, deviceId, time), character = character }; return inputEvent; } public static TextEvent Create(int deviceId, int character, double time = -1) { var inputEvent = new TextEvent { baseEvent = new InputEvent(Type, InputEvent.kBaseEventSize + 4, deviceId, time), character = character }; return inputEvent; } } }