Keyboard.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Input/Keyboard.h>
  6. #include <Renderer/Renderer.h>
  7. #include <Jolt/Core/Profiler.h>
  8. class Renderer;
  9. Keyboard::Keyboard()
  10. {
  11. Reset();
  12. }
  13. Keyboard::~Keyboard()
  14. {
  15. Shutdown();
  16. }
  17. void Keyboard::Reset()
  18. {
  19. mDI = nullptr;
  20. mKeyboard = nullptr;
  21. ResetKeyboard();
  22. }
  23. void Keyboard::ResetKeyboard()
  24. {
  25. memset(&mKeyPressed, 0, sizeof(mKeyPressed));
  26. memset(&mTimeKeyLastReleased, 0, sizeof(mTimeKeyLastReleased));
  27. memset(&mKeyDoubleClicked, 0, sizeof(mKeyDoubleClicked));
  28. memset(&mDOD, 0, sizeof(mDOD));
  29. mDODLength = 0;
  30. mCurrentPosition = 0;
  31. GetKeyboardState(mCurrentWUIState);
  32. memcpy(mPreviousWUIState, mCurrentWUIState, sizeof(mPreviousWUIState));
  33. }
  34. bool Keyboard::Initialize(Renderer *inRenderer)
  35. #ifdef JPH_COMPILER_CLANG
  36. // DIPROP_BUFFERSIZE is a pointer to 1 which causes UBSan: runtime error: reference binding to misaligned address 0x000000000001
  37. __attribute__((no_sanitize("alignment")))
  38. #endif
  39. {
  40. // Create direct input interface
  41. if (FAILED(CoCreateInstance(CLSID_DirectInput8, nullptr, CLSCTX_INPROC_SERVER, IID_IDirectInput8W, (void **)&mDI)))
  42. {
  43. Trace("Unable to create DirectInput interface, DirectX 8.0 is required");
  44. return false;
  45. }
  46. // Initialize direct input interface
  47. if (FAILED(mDI->Initialize((HINSTANCE)GetModuleHandle(nullptr), DIRECTINPUT_VERSION)))
  48. {
  49. Trace("Unable to initialize DirectInput interface, DirectX 8.0 is required");
  50. return false;
  51. }
  52. // Create keyboard device
  53. if (FAILED(mDI->CreateDevice(GUID_SysKeyboard, &mKeyboard, nullptr)))
  54. {
  55. Trace("Unable to get DirectInputDevice interface, DirectX 8.0 is required");
  56. return false;
  57. }
  58. // Set cooperative level for keyboard
  59. if (FAILED(mKeyboard->SetCooperativeLevel(inRenderer->GetWindowHandle(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND)))
  60. {
  61. Trace("Unable to set cooperative level for keyboard");
  62. return false;
  63. }
  64. // Set data format
  65. if (FAILED(mKeyboard->SetDataFormat(&c_dfDIKeyboard)))
  66. {
  67. Trace("Unable to set data format to keyboard");
  68. return false;
  69. }
  70. // Create a keyboard buffer
  71. DIPROPDWORD dipdw;
  72. dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  73. dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  74. dipdw.diph.dwObj = 0;
  75. dipdw.diph.dwHow = DIPH_DEVICE;
  76. dipdw.dwData = BUFFERSIZE;
  77. if (FAILED(mKeyboard->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
  78. {
  79. Trace("Unable to set keyboard buffer size");
  80. return false;
  81. }
  82. // Get keyboard layout
  83. mKeyboardLayout = GetKeyboardLayout(0);
  84. return true;
  85. }
  86. void Keyboard::Shutdown()
  87. {
  88. if (mKeyboard)
  89. {
  90. mKeyboard->Unacquire();
  91. mKeyboard = nullptr;
  92. }
  93. mDI = nullptr;
  94. Reset();
  95. }
  96. void Keyboard::Poll()
  97. {
  98. JPH_PROFILE_FUNCTION();
  99. // Get the state of the keyboard
  100. if (FAILED(mKeyboard->GetDeviceState(sizeof(mKeyPressed), mKeyPressed)))
  101. {
  102. mKeyboard->Acquire();
  103. if (FAILED(mKeyboard->GetDeviceState(sizeof(mKeyPressed), mKeyPressed)))
  104. {
  105. ResetKeyboard();
  106. return;
  107. }
  108. }
  109. // Get the state in a buffer
  110. mDODLength = BUFFERSIZE;
  111. mCurrentPosition = 0;
  112. if (FAILED(mKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  113. {
  114. mKeyboard->Acquire();
  115. if (FAILED(mKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), mDOD, &mDODLength, 0)))
  116. {
  117. ResetKeyboard();
  118. return;
  119. }
  120. }
  121. // Check double clicks
  122. for (DWORD d = 0; d < mDODLength; d++)
  123. {
  124. // Check if this means a button is pressed
  125. if (mDOD[d].dwData & 0x80)
  126. {
  127. if (mDOD[d].dwTimeStamp - mTimeKeyLastReleased[mDOD[d].dwOfs] <= DCLICKTIME)
  128. {
  129. // This is a double click
  130. mTimeKeyLastReleased[mDOD[d].dwOfs] = 0;
  131. mKeyDoubleClicked[mDOD[d].dwOfs] = TRUE;
  132. }
  133. }
  134. else // Remember last time this key was released
  135. mTimeKeyLastReleased[mDOD[d].dwOfs] = mDOD[d].dwTimeStamp;
  136. }
  137. // Get Windows User Interface state, copy current state to previous state and get new current state
  138. memcpy(mPreviousWUIState, mCurrentWUIState, sizeof(mPreviousWUIState));
  139. GetKeyboardState(mCurrentWUIState);
  140. }
  141. int Keyboard::GetFirstKey()
  142. {
  143. mCurrentPosition = 0;
  144. memcpy(mTrackedWUIState, mPreviousWUIState, sizeof(mTrackedWUIState));
  145. return GetNextKey();
  146. }
  147. static void sPress(BYTE &ioValue)
  148. {
  149. ioValue |= 0x80;
  150. ioValue ^= 0x01;
  151. }
  152. static void sRelease(BYTE &ioValue)
  153. {
  154. ioValue &= 0x7f;
  155. }
  156. int
  157. Keyboard::GetNextKey()
  158. {
  159. while (mCurrentPosition < mDODLength)
  160. {
  161. // Get next key
  162. const DIDEVICEOBJECTDATA &current = mDOD[mCurrentPosition];
  163. mCurrentPosition++;
  164. // Check special keys and update current state accordingly
  165. if (current.dwData & 0x80)
  166. switch (current.dwOfs)
  167. {
  168. case DIK_LSHIFT: sPress(mTrackedWUIState[VK_LSHIFT]); sPress(mTrackedWUIState[VK_SHIFT]); break;
  169. case DIK_RSHIFT: sPress(mTrackedWUIState[VK_RSHIFT]); sPress(mTrackedWUIState[VK_SHIFT]); break;
  170. case DIK_LCONTROL: sPress(mTrackedWUIState[VK_LCONTROL]); sPress(mTrackedWUIState[VK_CONTROL]); break;
  171. case DIK_RCONTROL: sPress(mTrackedWUIState[VK_RCONTROL]); sPress(mTrackedWUIState[VK_CONTROL]); break;
  172. case DIK_LALT: sPress(mTrackedWUIState[VK_LMENU]); sPress(mTrackedWUIState[VK_MENU]); break;
  173. case DIK_RALT: sPress(mTrackedWUIState[VK_RMENU]); sPress(mTrackedWUIState[VK_MENU]); break;
  174. case DIK_CAPSLOCK: sPress(mTrackedWUIState[VK_CAPITAL]); break;
  175. }
  176. else
  177. switch (current.dwOfs)
  178. {
  179. case DIK_LSHIFT: sRelease(mTrackedWUIState[VK_LSHIFT]); sRelease(mTrackedWUIState[VK_SHIFT]); break;
  180. case DIK_RSHIFT: sRelease(mTrackedWUIState[VK_RSHIFT]); sRelease(mTrackedWUIState[VK_SHIFT]); break;
  181. case DIK_LCONTROL: sRelease(mTrackedWUIState[VK_LCONTROL]); sRelease(mTrackedWUIState[VK_CONTROL]); break;
  182. case DIK_RCONTROL: sRelease(mTrackedWUIState[VK_RCONTROL]); sRelease(mTrackedWUIState[VK_CONTROL]); break;
  183. case DIK_LALT: sRelease(mTrackedWUIState[VK_LMENU]); sRelease(mTrackedWUIState[VK_MENU]); break;
  184. case DIK_RALT: sRelease(mTrackedWUIState[VK_RMENU]); sRelease(mTrackedWUIState[VK_MENU]); break;
  185. case DIK_CAPSLOCK: sRelease(mTrackedWUIState[VK_CAPITAL]); break;
  186. }
  187. // Return it
  188. if (current.dwData & 0x80)
  189. return current.dwOfs;
  190. }
  191. return 0;
  192. }
  193. uint Keyboard::GetVKValue()
  194. {
  195. JPH_ASSERT(mCurrentPosition > 0, "First call GetFirstKey() to get the first key, then you can convert it to a VK code");
  196. int key = mDOD[mCurrentPosition - 1].dwOfs;
  197. return MapVirtualKeyEx(key, 1, mKeyboardLayout);
  198. }
  199. char Keyboard::GetASCIIValue()
  200. {
  201. WORD result;
  202. JPH_ASSERT(mCurrentPosition > 0, "First call GetFirstKey() to get the first key, then you can convert it to an ASCII code");
  203. int key = mDOD[mCurrentPosition - 1].dwOfs;
  204. if (ToAsciiEx(GetVKValue(), key, mTrackedWUIState, &result, 0, mKeyboardLayout) != 1)
  205. return 0;
  206. return (char)result;
  207. }