KeyboardWin.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Input/Keyboard.h>
  6. // We're using DX8's DirectInput API
  7. #define DIRECTINPUT_VERSION 0x0800
  8. #include <dinput.h>
  9. /// Keyboard interface class which keeps track on the status of all keys and keeps track of the list of keys pressed.
  10. class KeyboardWin : public Keyboard
  11. {
  12. public:
  13. /// Constructor
  14. KeyboardWin();
  15. virtual ~KeyboardWin() override;
  16. /// Initialization / shutdown
  17. virtual bool Initialize(ApplicationWindow *inWindow) override;
  18. virtual void Shutdown() override;
  19. /// Update the keyboard state
  20. virtual void Poll() override;
  21. /// Checks if a key is pressed or not
  22. virtual bool IsKeyPressed(EKey inKey) const override { return mKeyPressed[FromKey(inKey)] != 0; }
  23. /// Buffered keyboard input, returns EKey::Invalid for none
  24. virtual EKey GetFirstKey() override;
  25. virtual EKey GetNextKey() override;
  26. private:
  27. void Reset();
  28. void ResetKeyboard();
  29. EKey ToKey(int inKey) const;
  30. int FromKey(EKey inKey) const;
  31. enum
  32. {
  33. BUFFERSIZE = 64, ///< Number of keys cached
  34. };
  35. // DirectInput part
  36. ComPtr<IDirectInput8> mDI;
  37. ComPtr<IDirectInputDevice8> mKeyboard;
  38. char mKeyPressed[256];
  39. DIDEVICEOBJECTDATA mDOD[BUFFERSIZE];
  40. DWORD mDODLength;
  41. DWORD mCurrentPosition;
  42. };