KeyboardController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using OpenTK;
  3. using OpenTK.Input;
  4. using Ryujinx.Common.Configuration.Hid;
  5. using Ryujinx.Configuration;
  6. using Ryujinx.HLE.HOS.Services.Hid;
  7. namespace Ryujinx.Ui
  8. {
  9. [Flags]
  10. public enum HotkeyButtons
  11. {
  12. ToggleVSync = 1 << 0,
  13. }
  14. public class KeyboardController
  15. {
  16. private readonly KeyboardConfig _config;
  17. public KeyboardController(KeyboardConfig config)
  18. {
  19. _config = config;
  20. }
  21. public static KeyboardState GetKeyboardState(int index)
  22. {
  23. if (index == KeyboardConfig.AllKeyboardsIndex || index < 0)
  24. {
  25. return Keyboard.GetState();
  26. }
  27. return Keyboard.GetState(index - 1);
  28. }
  29. public ControllerKeys GetButtons()
  30. {
  31. KeyboardState keyboard = GetKeyboardState(_config.Index);
  32. ControllerKeys buttons = 0;
  33. if (keyboard[(Key)_config.LeftJoycon.StickButton]) buttons |= ControllerKeys.LStick;
  34. if (keyboard[(Key)_config.LeftJoycon.DPadUp]) buttons |= ControllerKeys.DpadUp;
  35. if (keyboard[(Key)_config.LeftJoycon.DPadDown]) buttons |= ControllerKeys.DpadDown;
  36. if (keyboard[(Key)_config.LeftJoycon.DPadLeft]) buttons |= ControllerKeys.DpadLeft;
  37. if (keyboard[(Key)_config.LeftJoycon.DPadRight]) buttons |= ControllerKeys.DpadRight;
  38. if (keyboard[(Key)_config.LeftJoycon.ButtonMinus]) buttons |= ControllerKeys.Minus;
  39. if (keyboard[(Key)_config.LeftJoycon.ButtonL]) buttons |= ControllerKeys.L;
  40. if (keyboard[(Key)_config.LeftJoycon.ButtonZl]) buttons |= ControllerKeys.Zl;
  41. if (keyboard[(Key)_config.LeftJoycon.ButtonSl]) buttons |= ControllerKeys.SlLeft;
  42. if (keyboard[(Key)_config.LeftJoycon.ButtonSr]) buttons |= ControllerKeys.SrLeft;
  43. if (keyboard[(Key)_config.RightJoycon.StickButton]) buttons |= ControllerKeys.RStick;
  44. if (keyboard[(Key)_config.RightJoycon.ButtonA]) buttons |= ControllerKeys.A;
  45. if (keyboard[(Key)_config.RightJoycon.ButtonB]) buttons |= ControllerKeys.B;
  46. if (keyboard[(Key)_config.RightJoycon.ButtonX]) buttons |= ControllerKeys.X;
  47. if (keyboard[(Key)_config.RightJoycon.ButtonY]) buttons |= ControllerKeys.Y;
  48. if (keyboard[(Key)_config.RightJoycon.ButtonPlus]) buttons |= ControllerKeys.Plus;
  49. if (keyboard[(Key)_config.RightJoycon.ButtonR]) buttons |= ControllerKeys.R;
  50. if (keyboard[(Key)_config.RightJoycon.ButtonZr]) buttons |= ControllerKeys.Zr;
  51. if (keyboard[(Key)_config.RightJoycon.ButtonSl]) buttons |= ControllerKeys.SlRight;
  52. if (keyboard[(Key)_config.RightJoycon.ButtonSr]) buttons |= ControllerKeys.SrRight;
  53. return buttons;
  54. }
  55. public (short, short) GetLeftStick()
  56. {
  57. KeyboardState keyboard = GetKeyboardState(_config.Index);
  58. short dx = 0;
  59. short dy = 0;
  60. if (keyboard[(Key)_config.LeftJoycon.StickUp]) dy += 1;
  61. if (keyboard[(Key)_config.LeftJoycon.StickDown]) dy += -1;
  62. if (keyboard[(Key)_config.LeftJoycon.StickLeft]) dx += -1;
  63. if (keyboard[(Key)_config.LeftJoycon.StickRight]) dx += 1;
  64. Vector2 stick = new Vector2(dx, dy);
  65. stick.NormalizeFast();
  66. return ((short)(stick.X * short.MaxValue), (short)(stick.Y * short.MaxValue));
  67. }
  68. public (short, short) GetRightStick()
  69. {
  70. KeyboardState keyboard = GetKeyboardState(_config.Index);
  71. short dx = 0;
  72. short dy = 0;
  73. if (keyboard[(Key)_config.RightJoycon.StickUp]) dy += 1;
  74. if (keyboard[(Key)_config.RightJoycon.StickDown]) dy += -1;
  75. if (keyboard[(Key)_config.RightJoycon.StickLeft]) dx += -1;
  76. if (keyboard[(Key)_config.RightJoycon.StickRight]) dx += 1;
  77. Vector2 stick = new Vector2(dx, dy);
  78. stick.NormalizeFast();
  79. return ((short)(stick.X * short.MaxValue), (short)(stick.Y * short.MaxValue));
  80. }
  81. public static HotkeyButtons GetHotkeyButtons(KeyboardState keyboard)
  82. {
  83. HotkeyButtons buttons = 0;
  84. if (keyboard[(Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ToggleVsync])
  85. {
  86. buttons |= HotkeyButtons.ToggleVSync;
  87. }
  88. return buttons;
  89. }
  90. class KeyMappingEntry
  91. {
  92. public Key TargetKey;
  93. public byte Target;
  94. }
  95. private static readonly KeyMappingEntry[] KeyMapping = new KeyMappingEntry[]
  96. {
  97. new KeyMappingEntry { TargetKey = Key.A, Target = 0x4 },
  98. new KeyMappingEntry { TargetKey = Key.B, Target = 0x5 },
  99. new KeyMappingEntry { TargetKey = Key.C, Target = 0x6 },
  100. new KeyMappingEntry { TargetKey = Key.D, Target = 0x7 },
  101. new KeyMappingEntry { TargetKey = Key.E, Target = 0x8 },
  102. new KeyMappingEntry { TargetKey = Key.F, Target = 0x9 },
  103. new KeyMappingEntry { TargetKey = Key.G, Target = 0xA },
  104. new KeyMappingEntry { TargetKey = Key.H, Target = 0xB },
  105. new KeyMappingEntry { TargetKey = Key.I, Target = 0xC },
  106. new KeyMappingEntry { TargetKey = Key.J, Target = 0xD },
  107. new KeyMappingEntry { TargetKey = Key.K, Target = 0xE },
  108. new KeyMappingEntry { TargetKey = Key.L, Target = 0xF },
  109. new KeyMappingEntry { TargetKey = Key.M, Target = 0x10 },
  110. new KeyMappingEntry { TargetKey = Key.N, Target = 0x11 },
  111. new KeyMappingEntry { TargetKey = Key.O, Target = 0x12 },
  112. new KeyMappingEntry { TargetKey = Key.P, Target = 0x13 },
  113. new KeyMappingEntry { TargetKey = Key.Q, Target = 0x14 },
  114. new KeyMappingEntry { TargetKey = Key.R, Target = 0x15 },
  115. new KeyMappingEntry { TargetKey = Key.S, Target = 0x16 },
  116. new KeyMappingEntry { TargetKey = Key.T, Target = 0x17 },
  117. new KeyMappingEntry { TargetKey = Key.U, Target = 0x18 },
  118. new KeyMappingEntry { TargetKey = Key.V, Target = 0x19 },
  119. new KeyMappingEntry { TargetKey = Key.W, Target = 0x1A },
  120. new KeyMappingEntry { TargetKey = Key.X, Target = 0x1B },
  121. new KeyMappingEntry { TargetKey = Key.Y, Target = 0x1C },
  122. new KeyMappingEntry { TargetKey = Key.Z, Target = 0x1D },
  123. new KeyMappingEntry { TargetKey = Key.Number1, Target = 0x1E },
  124. new KeyMappingEntry { TargetKey = Key.Number2, Target = 0x1F },
  125. new KeyMappingEntry { TargetKey = Key.Number3, Target = 0x20 },
  126. new KeyMappingEntry { TargetKey = Key.Number4, Target = 0x21 },
  127. new KeyMappingEntry { TargetKey = Key.Number5, Target = 0x22 },
  128. new KeyMappingEntry { TargetKey = Key.Number6, Target = 0x23 },
  129. new KeyMappingEntry { TargetKey = Key.Number7, Target = 0x24 },
  130. new KeyMappingEntry { TargetKey = Key.Number8, Target = 0x25 },
  131. new KeyMappingEntry { TargetKey = Key.Number9, Target = 0x26 },
  132. new KeyMappingEntry { TargetKey = Key.Number0, Target = 0x27 },
  133. new KeyMappingEntry { TargetKey = Key.Enter, Target = 0x28 },
  134. new KeyMappingEntry { TargetKey = Key.Escape, Target = 0x29 },
  135. new KeyMappingEntry { TargetKey = Key.BackSpace, Target = 0x2A },
  136. new KeyMappingEntry { TargetKey = Key.Tab, Target = 0x2B },
  137. new KeyMappingEntry { TargetKey = Key.Space, Target = 0x2C },
  138. new KeyMappingEntry { TargetKey = Key.Minus, Target = 0x2D },
  139. new KeyMappingEntry { TargetKey = Key.Plus, Target = 0x2E },
  140. new KeyMappingEntry { TargetKey = Key.BracketLeft, Target = 0x2F },
  141. new KeyMappingEntry { TargetKey = Key.BracketRight, Target = 0x30 },
  142. new KeyMappingEntry { TargetKey = Key.BackSlash, Target = 0x31 },
  143. new KeyMappingEntry { TargetKey = Key.Tilde, Target = 0x32 },
  144. new KeyMappingEntry { TargetKey = Key.Semicolon, Target = 0x33 },
  145. new KeyMappingEntry { TargetKey = Key.Quote, Target = 0x34 },
  146. new KeyMappingEntry { TargetKey = Key.Grave, Target = 0x35 },
  147. new KeyMappingEntry { TargetKey = Key.Comma, Target = 0x36 },
  148. new KeyMappingEntry { TargetKey = Key.Period, Target = 0x37 },
  149. new KeyMappingEntry { TargetKey = Key.Slash, Target = 0x38 },
  150. new KeyMappingEntry { TargetKey = Key.CapsLock, Target = 0x39 },
  151. new KeyMappingEntry { TargetKey = Key.F1, Target = 0x3a },
  152. new KeyMappingEntry { TargetKey = Key.F2, Target = 0x3b },
  153. new KeyMappingEntry { TargetKey = Key.F3, Target = 0x3c },
  154. new KeyMappingEntry { TargetKey = Key.F4, Target = 0x3d },
  155. new KeyMappingEntry { TargetKey = Key.F5, Target = 0x3e },
  156. new KeyMappingEntry { TargetKey = Key.F6, Target = 0x3f },
  157. new KeyMappingEntry { TargetKey = Key.F7, Target = 0x40 },
  158. new KeyMappingEntry { TargetKey = Key.F8, Target = 0x41 },
  159. new KeyMappingEntry { TargetKey = Key.F9, Target = 0x42 },
  160. new KeyMappingEntry { TargetKey = Key.F10, Target = 0x43 },
  161. new KeyMappingEntry { TargetKey = Key.F11, Target = 0x44 },
  162. new KeyMappingEntry { TargetKey = Key.F12, Target = 0x45 },
  163. new KeyMappingEntry { TargetKey = Key.PrintScreen, Target = 0x46 },
  164. new KeyMappingEntry { TargetKey = Key.ScrollLock, Target = 0x47 },
  165. new KeyMappingEntry { TargetKey = Key.Pause, Target = 0x48 },
  166. new KeyMappingEntry { TargetKey = Key.Insert, Target = 0x49 },
  167. new KeyMappingEntry { TargetKey = Key.Home, Target = 0x4A },
  168. new KeyMappingEntry { TargetKey = Key.PageUp, Target = 0x4B },
  169. new KeyMappingEntry { TargetKey = Key.Delete, Target = 0x4C },
  170. new KeyMappingEntry { TargetKey = Key.End, Target = 0x4D },
  171. new KeyMappingEntry { TargetKey = Key.PageDown, Target = 0x4E },
  172. new KeyMappingEntry { TargetKey = Key.Right, Target = 0x4F },
  173. new KeyMappingEntry { TargetKey = Key.Left, Target = 0x50 },
  174. new KeyMappingEntry { TargetKey = Key.Down, Target = 0x51 },
  175. new KeyMappingEntry { TargetKey = Key.Up, Target = 0x52 },
  176. new KeyMappingEntry { TargetKey = Key.NumLock, Target = 0x53 },
  177. new KeyMappingEntry { TargetKey = Key.KeypadDivide, Target = 0x54 },
  178. new KeyMappingEntry { TargetKey = Key.KeypadMultiply, Target = 0x55 },
  179. new KeyMappingEntry { TargetKey = Key.KeypadMinus, Target = 0x56 },
  180. new KeyMappingEntry { TargetKey = Key.KeypadPlus, Target = 0x57 },
  181. new KeyMappingEntry { TargetKey = Key.KeypadEnter, Target = 0x58 },
  182. new KeyMappingEntry { TargetKey = Key.Keypad1, Target = 0x59 },
  183. new KeyMappingEntry { TargetKey = Key.Keypad2, Target = 0x5A },
  184. new KeyMappingEntry { TargetKey = Key.Keypad3, Target = 0x5B },
  185. new KeyMappingEntry { TargetKey = Key.Keypad4, Target = 0x5C },
  186. new KeyMappingEntry { TargetKey = Key.Keypad5, Target = 0x5D },
  187. new KeyMappingEntry { TargetKey = Key.Keypad6, Target = 0x5E },
  188. new KeyMappingEntry { TargetKey = Key.Keypad7, Target = 0x5F },
  189. new KeyMappingEntry { TargetKey = Key.Keypad8, Target = 0x60 },
  190. new KeyMappingEntry { TargetKey = Key.Keypad9, Target = 0x61 },
  191. new KeyMappingEntry { TargetKey = Key.Keypad0, Target = 0x62 },
  192. new KeyMappingEntry { TargetKey = Key.KeypadPeriod, Target = 0x63 },
  193. new KeyMappingEntry { TargetKey = Key.NonUSBackSlash, Target = 0x64 },
  194. new KeyMappingEntry { TargetKey = Key.F13, Target = 0x68 },
  195. new KeyMappingEntry { TargetKey = Key.F14, Target = 0x69 },
  196. new KeyMappingEntry { TargetKey = Key.F15, Target = 0x6A },
  197. new KeyMappingEntry { TargetKey = Key.F16, Target = 0x6B },
  198. new KeyMappingEntry { TargetKey = Key.F17, Target = 0x6C },
  199. new KeyMappingEntry { TargetKey = Key.F18, Target = 0x6D },
  200. new KeyMappingEntry { TargetKey = Key.F19, Target = 0x6E },
  201. new KeyMappingEntry { TargetKey = Key.F20, Target = 0x6F },
  202. new KeyMappingEntry { TargetKey = Key.F21, Target = 0x70 },
  203. new KeyMappingEntry { TargetKey = Key.F22, Target = 0x71 },
  204. new KeyMappingEntry { TargetKey = Key.F23, Target = 0x72 },
  205. new KeyMappingEntry { TargetKey = Key.F24, Target = 0x73 },
  206. new KeyMappingEntry { TargetKey = Key.ControlLeft, Target = 0xE0 },
  207. new KeyMappingEntry { TargetKey = Key.ShiftLeft, Target = 0xE1 },
  208. new KeyMappingEntry { TargetKey = Key.AltLeft, Target = 0xE2 },
  209. new KeyMappingEntry { TargetKey = Key.WinLeft, Target = 0xE3 },
  210. new KeyMappingEntry { TargetKey = Key.ControlRight, Target = 0xE4 },
  211. new KeyMappingEntry { TargetKey = Key.ShiftRight, Target = 0xE5 },
  212. new KeyMappingEntry { TargetKey = Key.AltRight, Target = 0xE6 },
  213. new KeyMappingEntry { TargetKey = Key.WinRight, Target = 0xE7 },
  214. };
  215. private static readonly KeyMappingEntry[] KeyModifierMapping = new KeyMappingEntry[]
  216. {
  217. new KeyMappingEntry { TargetKey = Key.ControlLeft, Target = 0 },
  218. new KeyMappingEntry { TargetKey = Key.ShiftLeft, Target = 1 },
  219. new KeyMappingEntry { TargetKey = Key.AltLeft, Target = 2 },
  220. new KeyMappingEntry { TargetKey = Key.WinLeft, Target = 3 },
  221. new KeyMappingEntry { TargetKey = Key.ControlRight, Target = 4 },
  222. new KeyMappingEntry { TargetKey = Key.ShiftRight, Target = 5 },
  223. new KeyMappingEntry { TargetKey = Key.AltRight, Target = 6 },
  224. new KeyMappingEntry { TargetKey = Key.WinRight, Target = 7 },
  225. new KeyMappingEntry { TargetKey = Key.CapsLock, Target = 8 },
  226. new KeyMappingEntry { TargetKey = Key.ScrollLock, Target = 9 },
  227. new KeyMappingEntry { TargetKey = Key.NumLock, Target = 10 },
  228. };
  229. public KeyboardInput GetKeysDown()
  230. {
  231. KeyboardState keyboard = GetKeyboardState(_config.Index);
  232. KeyboardInput hidKeyboard = new KeyboardInput
  233. {
  234. Modifier = 0,
  235. Keys = new int[0x8]
  236. };
  237. foreach (KeyMappingEntry entry in KeyMapping)
  238. {
  239. int value = keyboard[entry.TargetKey] ? 1 : 0;
  240. hidKeyboard.Keys[entry.Target / 0x20] |= (value << (entry.Target % 0x20));
  241. }
  242. foreach (KeyMappingEntry entry in KeyModifierMapping)
  243. {
  244. int value = keyboard[entry.TargetKey] ? 1 : 0;
  245. hidKeyboard.Modifier |= value << entry.Target;
  246. }
  247. return hidKeyboard;
  248. }
  249. }
  250. }