Keyboard.h 1.9 KB

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