Keyboard.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. // We're using DX8's DirectInput API
  6. #define DIRECTINPUT_VERSION 0x0800
  7. #include <dinput.h>
  8. class Renderer;
  9. /// Keyboard interface class which keeps track on the status of all keys and keeps track of the list of keys pressed.
  10. class Keyboard
  11. {
  12. public:
  13. /// Constructor
  14. Keyboard();
  15. ~Keyboard();
  16. /// Initialization / shutdown
  17. bool Initialize(Renderer *inRenderer);
  18. void Shutdown();
  19. /// Update the keyboard state
  20. void Poll();
  21. /// Checks if a key is pressed or not, use one of the DIK_* constants
  22. bool IsKeyPressed(int inKey) const { return mKeyPressed[inKey] != 0; }
  23. bool IsKeyDoubleClicked(int inKey) const { return mKeyDoubleClicked[inKey] != 0; }
  24. /// Checks if a key is pressed and was not pressed the last time this function was called (state is stored in ioPrevState)
  25. bool IsKeyPressedAndTriggered(int inKey, bool &ioPrevState) const
  26. {
  27. bool prev_state = ioPrevState;
  28. ioPrevState = IsKeyPressed(inKey);
  29. return ioPrevState && !prev_state;
  30. }
  31. /// Buffered keyboard input, returns 0 for none or one of the DIK_* constants
  32. int GetFirstKey();
  33. int GetNextKey();
  34. uint GetVKValue(); ///< Get VK_* constant value for last key returned by GetFirstKey or GetNextKey
  35. char GetASCIIValue(); ///< Get ASCII value for last key returned by GetFirstKey or GetNextKey
  36. private:
  37. void Reset();
  38. void ResetKeyboard();
  39. enum
  40. {
  41. BUFFERSIZE = 64, ///< Number of keys cached
  42. DCLICKTIME = 300 ///< Minimum time between key release and key down to make it a double click
  43. };
  44. // DirectInput part
  45. ComPtr<IDirectInput8> mDI;
  46. ComPtr<IDirectInputDevice8> mKeyboard;
  47. char mKeyPressed[256];
  48. int mKeyDoubleClicked[256];
  49. int mTimeKeyLastReleased[256];
  50. DIDEVICEOBJECTDATA mDOD[BUFFERSIZE];
  51. DWORD mDODLength;
  52. DWORD mCurrentPosition;
  53. // Windows User Interface part for translating DIK_* constants into VK_* constants and ASCII characters
  54. HKL mKeyboardLayout;
  55. BYTE mPreviousWUIState[256];
  56. BYTE mCurrentWUIState[256];
  57. BYTE mTrackedWUIState[256];
  58. };