/// <summary>
        /// Set <see cref="Keyboard.keyboardLayout"/> of the given keyboard.
        /// </summary>
        /// <param name="name">Name of the keyboard layout to switch to.</param>
        /// <param name="keyboard">Keyboard to switch layout on. If <c>null</c>, <see cref="Keyboard.current"/> is used.</param>
        /// <exception cref="ArgumentException"><paramref name="keyboard"/> and <see cref="Keyboard.current"/> are both <c>null</c>.</exception>
        /// <remarks>
        /// Also queues and immediately processes an <see cref="DeviceConfigurationEvent"/> for the keyboard.
        /// </remarks>
        public unsafe void SetKeyboardLayout(string name, Keyboard keyboard = null)
        {
            if (keyboard == null)
            {
                keyboard = Keyboard.current;
                if (keyboard == null)
                {
                    throw new ArgumentException("No keyboard has been created and no keyboard has been given", nameof(keyboard));
                }
            }

            runtime.SetDeviceCommandCallback(keyboard, (id, command) =>
            {
                if (id == QueryKeyboardLayoutCommand.Type)
                {
                    var commandPtr = (QueryKeyboardLayoutCommand *)command;
                    commandPtr->WriteLayoutName(name);
                    return(InputDeviceCommand.GenericSuccess);
                }
                return(InputDeviceCommand.GenericFailure);
            });

            // Make sure caches on keys are flushed.
            InputSystem.QueueConfigChangeEvent(Keyboard.current);
            InputSystem.Update();
        }
Beispiel #2
0
        /// <summary>
        /// Set the <see cref="InputControl.displayName"/> of <paramref name="key"/> on the current
        /// <see cref="Keyboard"/> to be <paramref name="displayName"/>.
        /// </summary>
        /// <param name="key">Key to set the display name for.</param>
        /// <param name="displayName">Display name for the key.</param>
        /// <param name="scanCode">Optional <see cref="KeyControl.scanCode"/> to report for the key.</param>
        /// <remarks>
        /// Automatically adds a <see cref="Keyboard"/> if none has been added yet.
        /// </remarks>
        public unsafe void SetKeyInfo(Key key, string displayName, int scanCode = 0)
        {
            if (Keyboard.current == null)
            {
                InputSystem.AddDevice <Keyboard>();
            }

            if (m_KeyInfos == null)
            {
                m_KeyInfos = new Dictionary <Key, Tuple <string, int> >();

                runtime.SetDeviceCommandCallback(Keyboard.current,
                                                 (id, commandPtr) =>
                {
                    if (commandPtr->type == QueryKeyNameCommand.Type)
                    {
                        var keyNameCommand = (QueryKeyNameCommand *)commandPtr;

                        if (m_KeyInfos.TryGetValue((Key)keyNameCommand->scanOrKeyCode, out var info))
                        {
                            keyNameCommand->scanOrKeyCode = info.Item2;
                            StringHelpers.WriteStringToBuffer(info.Item1, (IntPtr)keyNameCommand->nameBuffer,
                                                              QueryKeyNameCommand.kMaxNameLength);
                        }

                        return(QueryKeyNameCommand.kSize);
                    }

                    return(InputDeviceCommand.GenericFailure);
                });
            }

            m_KeyInfos[key] = new Tuple <string, int>(displayName, scanCode);

            // Make sure caches on keys are flushed.
            InputSystem.QueueConfigChangeEvent(Keyboard.current);
            InputSystem.Update();
        }