KeyboardController.cs 15 KB

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