Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cInputKeyboard.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cInputKeyboard_H
7 #define _INC_cInputKeyboard_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cInputKey.h"
15 
16 namespace GrayLib
17 {
18  UNITTEST2_PREDEF(cInputKeyboardWin);
19 
21  {
28 
29  public:
32 
33  public:
34 
35  cInputKeyObj(cString sKeyName = "", VK_TYPE vKeyCode = VK_NULL) noexcept
36  : m_sKeyName(sKeyName)
37  , m_vKeyCode(vKeyCode)
38  {
39  }
40 
41  bool isMapped() const noexcept
42  {
43  return !m_sKeyName.IsEmpty();
44  }
45  cString get_Name() const noexcept
46  {
48  return m_sKeyName;
49  }
50  };
51 
53  {
55 
56  public:
57  static const int k_KeyQty = 256;
58  protected:
59  BYTE m_aKeyState[k_KeyQty];
60 
61  public:
63  {
64  InitZero();
65  }
67  {
68  ::memcpy(m_aKeyState, c.m_aKeyState, sizeof(m_aKeyState));
69  }
70  void InitZero() noexcept
71  {
72  cMem::Zero(m_aKeyState, sizeof(m_aKeyState));
73  }
74  BYTE GetKeyState(ITERATE_t nScanCode) const
75  {
77  ASSERT(IS_INDEX_GOOD(nScanCode, k_KeyQty));
78  return m_aKeyState[nScanCode];
79  }
80  bool IsKeyPressed(ITERATE_t nScanCode) const;
81  };
82 
84  {
87 
88  protected:
90 
91  public:
92  cInputKeyboardBase() noexcept
93  {
94  cMem::Zero(m_aKeyState, sizeof(m_aKeyState));
95  }
96 
97  ITERATE_t get_KeyQty() const noexcept
98  {
99  return m_aKeyObjects.GetSize();
100  }
101  const cInputKeyObj& GetKeyObj(ITERATE_t nScanCode) const
102  {
104  ASSERT(m_aKeyObjects.IsValidIndex(nScanCode));
105  return m_aKeyObjects[nScanCode];
106  }
107  cString GetKeyName(ITERATE_t nScanCode) const
108  {
113  if (!m_aKeyObjects.IsValidIndex(nScanCode))
114  return "";
115  return m_aKeyObjects[nScanCode].m_sKeyName;
116  }
117  };
118 
119 #if defined(_WIN32) && ! defined(UNDER_CE)
120 
121  class GRAYLIB_LINK cInputKeyboardWin : public cInputKeyboardBase
122  {
126 
127  private:
128  BYTE m_aVKeyToScanCode[256];
129 
130  public:
131  cInputKeyboardWin();
132  ~cInputKeyboardWin();
133 
134  ITERATE_t GetKeyOffsetFromVKey(VK_TYPE vKey) const;
135  cString GetVKeyName(VK_TYPE vKey) const;
136 
137  HRESULT InitKeyObjects();
138  HRESULT UpdateKeyStates(void);
139 
140  UNITTEST_FRIEND(cInputKeyboardWin);
141  };
142 #endif
143 }
144 #endif
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define IS_INDEX_GOOD(i, q)
cast the (likely) int to unsigned to check for negatives.
Definition: Index.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define VK_NULL
Definition: cInputBase.h:28
int VK_TYPE
a type of physical key in _WIN32. "#define VK_UP" etc. NOT the same as a scan code.
Definition: cInputBase.h:27
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cInputKeyboard.h:21
cInputKeyObj(cString sKeyName="", VK_TYPE vKeyCode=0) noexcept
Definition: cInputKeyboard.h:35
cString get_Name() const noexcept
Definition: cInputKeyboard.h:45
cString m_sKeyName
descriptive name for the key. _WIN32 GetKeyNameText(). "" = unused key
Definition: cInputKeyboard.h:30
VK_TYPE m_vKeyCode
Definition: cInputKeyboard.h:31
bool isMapped() const noexcept
Definition: cInputKeyboard.h:41
Definition: cInputKeyboard.h:84
cArrayStruct< cInputKeyObj > m_aKeyObjects
Array of all keys on the device. (up to 255)
Definition: cInputKeyboard.h:89
ITERATE_t get_KeyQty() const noexcept
Definition: cInputKeyboard.h:97
cString GetKeyName(ITERATE_t nScanCode) const
Definition: cInputKeyboard.h:107
const cInputKeyObj & GetKeyObj(ITERATE_t nScanCode) const
Definition: cInputKeyboard.h:101
cInputKeyboardBase() noexcept
Definition: cInputKeyboard.h:92
Definition: cInputKeyboard.h:53
BYTE GetKeyState(ITERATE_t nScanCode) const
Definition: cInputKeyboard.h:74
cInputKeyboardState() noexcept
Definition: cInputKeyboard.h:62
void InitZero() noexcept
Definition: cInputKeyboard.h:70
cInputKeyboardState(const cInputKeyboardState &c) noexcept
Definition: cInputKeyboard.h:66
ITERATE_t GetSize() const noexcept
Definition: cArray.h:137
bool IsEmpty() const noexcept
Definition: cString.h:176
Definition: cArray.h:932
bool IsValidIndex(ITERATE_t i) const noexcept
Definition: cArray.h:495
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
int ITERATE_t
like size_t but signed
Definition: Index.h:28
cStringT< GChar_t > cString
Definition: cString.h:636
static void Zero(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:100