暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

KeyControl.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Globalization;
  2. using UnityEngine.InputSystem.LowLevel;
  3. using UnityEngine.Scripting;
  4. namespace UnityEngine.InputSystem.Controls
  5. {
  6. /// <summary>
  7. /// A key on a <see cref="Keyboard"/>.
  8. /// </summary>
  9. /// <remarks>
  10. /// This is an extended button control which adds various features to account for the fact that keys
  11. /// have symbols associated with them which may change depending on keyboard layout as well as in combination
  12. /// with other keys.
  13. ///
  14. /// Note:
  15. /// Unity input system key codes and input manager key codes are designed with game controls in mind.
  16. ///
  17. /// This means the way they are assigned is intended to preserve the location of keys on keyboards,
  18. /// so that pressing a key in the same location on different keyboards should result in the same action
  19. /// regardless of what is printed on a key or what current system language is set.
  20. ///
  21. /// This means, for example, that <see cref="Key.A"/> is always the key to the right of <see cref="Key.CapsLock"/>,
  22. /// regardless of which key (if any) produces the "a" character on the current keyboard layout.
  23. ///
  24. /// Unity relies on physical hardware in the keyboards to report same USB HID "usage" for the keys in
  25. /// the same location.This puts a practical limit on what can be achieved, because different keyboards
  26. /// might report different data, and this is outside of Unity's control.
  27. ///
  28. /// For this reason, you should not use key codes to read text input.
  29. /// Instead, you should use the <see cref="Keyboard.onTextInput"/> callback.
  30. /// The `onTextInput` callback provides you with the actual text characters which correspond
  31. /// to the symbols printed on a keyboard, based on the end user's current system language layout.
  32. ///
  33. /// To find the text character (if any) generated by a key according to the currently active keyboard
  34. /// layout, use the <see cref="InputControl.displayName"/> property of <see cref="KeyControl"/>.
  35. /// </remarks>
  36. public class KeyControl : ButtonControl
  37. {
  38. /// <summary>
  39. /// The code used in Unity to identify the key.
  40. /// </summary>
  41. /// <remarks>
  42. /// This property must be initialized by <see cref="InputControl.FinishSetup"/> of
  43. /// the device owning the control.
  44. /// You should not use `keyCode` to read text input. For more information, <see cref="KeyControl"/>
  45. /// </remarks>
  46. public Key keyCode { get; set; }
  47. ////REVIEW: rename this to something like platformKeyCode? We're not really dealing with scan code here.
  48. /// <summary>
  49. /// The code that the underlying platform uses to identify the key.
  50. /// </summary>
  51. public int scanCode
  52. {
  53. get
  54. {
  55. RefreshConfigurationIfNeeded();
  56. return m_ScanCode;
  57. }
  58. }
  59. protected override void RefreshConfiguration()
  60. {
  61. // Wipe our last cached set of data (if any).
  62. displayName = null;
  63. m_ScanCode = 0;
  64. var command = QueryKeyNameCommand.Create(keyCode);
  65. if (device.ExecuteCommand(ref command) > 0)
  66. {
  67. m_ScanCode = command.scanOrKeyCode;
  68. var rawKeyName = command.ReadKeyName();
  69. if (string.IsNullOrEmpty(rawKeyName))
  70. {
  71. displayName = rawKeyName;
  72. return;
  73. }
  74. var textInfo = CultureInfo.InvariantCulture.TextInfo;
  75. // We need to lower case first because ToTitleCase preserves upper casing.
  76. // For example on Swedish Windows layout right shift display name is "HÖGER SKIFT".
  77. // Just passing it to ToTitleCase won't change anything. But passing "höger skift" will return "Höger Skift".
  78. var keyNameLowerCase = textInfo.ToLower(rawKeyName);
  79. if (string.IsNullOrEmpty(keyNameLowerCase))
  80. {
  81. displayName = rawKeyName;
  82. return;
  83. }
  84. displayName = textInfo.ToTitleCase(keyNameLowerCase);
  85. }
  86. }
  87. // Cached configuration data for the key. We fetch this from the
  88. // device on demand.
  89. private int m_ScanCode;
  90. }
  91. }