BRE12
Keyboard.h
1 #pragma once
2 
3 #include <cstdint>
4 #define DIRECTINPUT_VERSION 0x0800
5 #include <dinput.h>
6 
7 namespace BRE {
8 class Keyboard {
9 public:
10  // Preconditions:
11  // - Create() must be called once
12  static Keyboard& Create(IDirectInput8& directInput,
13  const HWND windowHandle) noexcept;
14 
15  // Preconditions:
16  // - Create() method must be called before.
17  static Keyboard& Get() noexcept;
18 
19  ~Keyboard();
20  Keyboard(const Keyboard&) = delete;
21  const Keyboard& operator=(const Keyboard&) = delete;
22  Keyboard(Keyboard&&) = delete;
23  Keyboard& operator=(Keyboard&&) = delete;
24 
25  void Update() noexcept;
26 
27  __forceinline const std::uint8_t* GetKeysCurrentState() const noexcept
28  {
29  return mKeysCurrentState;
30  }
31  __forceinline const std::uint8_t* GetKeysLastState() const noexcept
32  {
33  return mKeysLastState;
34  }
35  __forceinline bool IsKeyUp(const std::uint8_t key) const noexcept
36  {
37  return (mKeysCurrentState[key] & 0x80) == 0U;
38  }
39  __forceinline bool IsKeyDown(const std::uint8_t key) const noexcept
40  {
41  return (mKeysCurrentState[key] & 0x80) != 0U;
42  }
43  __forceinline bool WasKeyUp(const std::uint8_t key) const noexcept
44  {
45  return (mKeysLastState[key] & 0x80) == 0U;
46  }
47  __forceinline bool WasKeyDown(const std::uint8_t key) const noexcept
48  {
49  return (mKeysLastState[key] & 0x80) != 0U;
50  }
51  __forceinline bool WasKeyPressedThisFrame(const std::uint8_t key) const noexcept
52  {
53  return IsKeyDown(key) && WasKeyUp(key);
54  }
55  __forceinline bool WasKeyReleasedThisFrame(const std::uint8_t key) const noexcept
56  {
57  return IsKeyUp(key) && WasKeyDown(key);
58  }
59  __forceinline bool IsKeyHeldDown(const std::uint8_t key) const noexcept
60  {
61  return IsKeyDown(key) && WasKeyDown(key);
62  }
63 
64 private:
65  explicit Keyboard(IDirectInput8& directInput, const HWND windowHandle);
66 
67  static const uint32_t sNumKeys = 256U;
68 
69  IDirectInput8& mDirectInput;
70  LPDIRECTINPUTDEVICE8 mDevice{ nullptr };
71  std::uint8_t mKeysCurrentState[sNumKeys] = {};
72  std::uint8_t mKeysLastState[sNumKeys] = {};
73 };
74 }
75 
Definition: Camera.cpp:8
Definition: Keyboard.h:8