KeyControl.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using UnityEngine.InputSystem.LowLevel;
  2. using UnityEngine.Scripting;
  3. namespace UnityEngine.InputSystem.Controls
  4. {
  5. /// <summary>
  6. /// A key on a <see cref="Keyboard"/>.
  7. /// </summary>
  8. /// <remarks>
  9. /// This is an extended button control which adds various features to account for the fact that keys
  10. /// have symbols associated with them which may change depending on keyboard layout as well as in combination
  11. /// with other keys.
  12. ///
  13. /// Note that there is no text input associated with individual keys as text composition is highly
  14. /// layout specific and does not need to be key-by-key. For general text input, see <see cref="Keyboard.onTextInput"/>.
  15. /// To find the text displayed on a key, use <see cref="KeyControl.displayName"/>.
  16. /// </remarks>
  17. [Preserve]
  18. public class KeyControl : ButtonControl
  19. {
  20. /// <summary>
  21. /// The code used in Unity to identify the key.
  22. /// </summary>
  23. /// <remarks>
  24. /// This property must be initialized by <see cref="InputControl.FinishSetup"/> of
  25. /// the device owning the control.
  26. /// </remarks>
  27. public Key keyCode { get; set; }
  28. ////REVIEW: rename this to something like platformKeyCode? We're not really dealing with scan code here.
  29. /// <summary>
  30. /// The code that the underlying platform uses to identify the key.
  31. /// </summary>
  32. public int scanCode
  33. {
  34. get
  35. {
  36. RefreshConfigurationIfNeeded();
  37. return m_ScanCode;
  38. }
  39. }
  40. protected override void RefreshConfiguration()
  41. {
  42. // Wipe our last cached set of data (if any).
  43. displayName = null;
  44. m_ScanCode = 0;
  45. var command = QueryKeyNameCommand.Create(keyCode);
  46. if (device.ExecuteCommand(ref command) > 0)
  47. {
  48. m_ScanCode = command.scanOrKeyCode;
  49. displayName = command.ReadKeyName();
  50. }
  51. }
  52. // Cached configuration data for the key. We fetch this from the
  53. // device on demand.
  54. private int m_ScanCode;
  55. }
  56. }