NpadKeyboard.cs 15 KB

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