Keyboard.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. /// Buffered keyboard input, returns 0 for none or one of the DIK_* constants
  25. int GetFirstKey();
  26. int GetNextKey();
  27. uint GetVKValue(); ///< Get VK_* constant value for last key returned by GetFirstKey or GetNextKey
  28. char GetASCIIValue(); ///< Get ASCII value for last key returned by GetFirstKey or GetNextKey
  29. private:
  30. void Reset();
  31. void ResetKeyboard();
  32. enum
  33. {
  34. BUFFERSIZE = 64, ///< Number of keys cached
  35. DCLICKTIME = 300 ///< Minimum time between key release and key down to make it a double click
  36. };
  37. // DirectInput part
  38. ComPtr<IDirectInput8> mDI;
  39. ComPtr<IDirectInputDevice8> mKeyboard;
  40. char mKeyPressed[256];
  41. int mKeyDoubleClicked[256];
  42. int mTimeKeyLastReleased[256];
  43. DIDEVICEOBJECTDATA mDOD[BUFFERSIZE];
  44. DWORD mDODLength;
  45. DWORD mCurrentPosition;
  46. // Windows User Interface part for translating DIK_* constants into VK_* constants and ASCII characters
  47. HKL mKeyboardLayout;
  48. BYTE mPreviousWUIState[256];
  49. BYTE mCurrentWUIState[256];
  50. BYTE mTrackedWUIState[256];
  51. };