5
5
6
6
KeyboardMonitor* keyboardMonitor;
7
7
8
- KeyboardMonitor::KeyboardMonitor ( HANDLE hModule ) :
8
+ KeyboardMonitor::KeyboardMonitor (const HANDLE hModule ) :
9
9
m_hModule( hModule ),
10
- m_hhook( NULL ),
11
- m_bRejectKeyBoardInputs( FALSE )
10
+ m_bRejectKeyBoardInputs( false ),
11
+ m_hhook( nullptr )
12
12
{
13
13
UWM_KEYBOARD_CHAR = RegisterWindowMessage (UWM_KEYBOARD_MSG_CHAR);
14
14
UWM_KEYBOARD_UP = RegisterWindowMessage (UWM_KEYBOARD_MSG_UP);
15
15
UWM_KEYBOARD_DOWN = RegisterWindowMessage (UWM_KEYBOARD_MSG_DOWN);
16
16
}
17
17
18
- KeyboardMonitor::~KeyboardMonitor (void )
18
+ KeyboardMonitor::~KeyboardMonitor ()
19
19
{
20
20
ClearHooks ();
21
21
}
22
22
23
23
// -----------------------------------------------------------------------
24
- BOOL KeyboardMonitor::HandleHook (int nCode )
24
+ bool KeyboardMonitor::HandleHook ( const int nCode )
25
25
{
26
26
if (nCode < 0 || HC_ACTION != nCode )
27
27
{
28
- return FALSE ;
28
+ return false ;
29
29
}
30
- return TRUE ;
30
+ return true ;
31
31
}
32
32
33
33
// -----------------------------------------------------------------------
34
34
LRESULT CALLBACK KeyboardMonitor::CallLowLevelKeyboardProc (int nCode, WPARAM wParam, LPARAM lParam)
35
35
{
36
36
// are we handling that hook?
37
- if ( FALSE == keyboardMonitor->HandleHook ( nCode ))
37
+ if (false == keyboardMonitor->HandleHook ( nCode ))
38
38
{
39
- return CallNextHookEx ( keyboardMonitor->GetHHOOK (), nCode, wParam, lParam);
39
+ return CallNextHookEx ( keyboardMonitor->GetHhook (), nCode, wParam, lParam);
40
40
}
41
41
42
42
// By returning a non-zero value from the hook procedure, the
43
43
// message does not get passed to the target window
44
- KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
45
- int error=GetLastError ();
44
+ const auto *pkbhs = reinterpret_cast <KBDLLHOOKSTRUCT *>(lParam);
46
45
switch (nCode)
47
46
{
48
47
case HC_ACTION:
49
48
{
50
- LRESULT hHandle = keyboardMonitor->HandleMessage ( wParam, pkbhs->vkCode , pkbhs->dwExtraInfo );
49
+ const auto hHandle = keyboardMonitor->HandleMessage ( wParam, pkbhs->vkCode , pkbhs->dwExtraInfo );
51
50
switch ( hHandle )
52
51
{
53
52
case 1 : // the key was handled by someone
@@ -56,7 +55,7 @@ LRESULT CALLBACK KeyboardMonitor::CallLowLevelKeyboardProc(int nCode, WPARAM wPa
56
55
// other processes are supposed to ignore WM_NULL
57
56
// this is not full proof, (we might not be the first hook in the queue), but the odds of
58
57
// multiple hooks all trying to do the same thing is limited.
59
- if ( keyboardMonitor->RejectKeyboadInputs () == TRUE )
58
+ if ( keyboardMonitor->RejectKeyboadInputs () == true )
60
59
{
61
60
return 1 ;
62
61
}
@@ -65,6 +64,9 @@ LRESULT CALLBACK KeyboardMonitor::CallLowLevelKeyboardProc(int nCode, WPARAM wPa
65
64
66
65
case 0 :
67
66
break ;
67
+
68
+ default :
69
+ break ;
68
70
}
69
71
}
70
72
@@ -81,24 +83,27 @@ LRESULT CALLBACK KeyboardMonitor::CallLowLevelKeyboardProc(int nCode, WPARAM wPa
81
83
default :
82
84
break ;
83
85
}
84
- return CallNextHookEx ( keyboardMonitor->GetHHOOK (), nCode, wParam, lParam);
86
+ return CallNextHookEx ( keyboardMonitor->GetHhook (), nCode, wParam, lParam);
85
87
}// CallLowLevelKeyboardProc
86
88
87
- // -----------------------------------------------------------------------
88
- // Just look if one of the windows expects that key
89
- BOOL KeyboardMonitor::IsSpecialKey ( const WPARAM wParam ) const
89
+ /* *
90
+ * \brief check if the given param means that the one of the special key was pressed.
91
+ * \param wParam the key we are checking against.
92
+ * \return if the given key is a special key or not.
93
+ */
94
+ bool KeyboardMonitor::IsSpecialKey ( const WPARAM wParam ) const
90
95
{
91
- for ( std::map< HWND, WPARAM >::const_iterator it = m_mapWindows.begin (); it != m_mapWindows.end (); ++it )
96
+ for (auto it = m_mapWindows.begin (); it != m_mapWindows.end (); ++it )
92
97
{
93
98
if ( wParam == it->second )
94
99
{
95
100
// yes, this window uses that key.
96
- return TRUE ;
101
+ return true ;
97
102
}
98
103
}
99
104
100
105
// if we are here we never found it.
101
- return FALSE ;
106
+ return false ;
102
107
}
103
108
104
109
// -----------------------------------------------------------------------
@@ -119,10 +124,10 @@ LRESULT KeyboardMonitor::HandleMessage
119
124
}
120
125
121
126
// get the number of milli secs from start of system
122
- DWORD tick = GetTickCount ();
127
+ const auto tick = GetTickCount ();
123
128
//
124
129
// do not allow items to be too quick
125
- if ( GetLastKey ( tick ) .similar ( msg, wParam) )
130
+ if ( GetLastKey ( tick ) .Similar ( msg, wParam) )
126
131
{
127
132
// the key we just entered has been repeated too soon for us and is rejected.
128
133
// TRACE( "<< Repeat Key >>\n" );
@@ -132,10 +137,10 @@ LRESULT KeyboardMonitor::HandleMessage
132
137
// it is not uncommon for apps to request the last key more than once.
133
138
SetLastKey ( LAST_KEY (msg, wParam, tick) );
134
139
135
- LRESULT lResult = 0L ; // assume that nobody will handle it.
136
- for ( std::map< HWND, WPARAM >::const_iterator it = m_mapWindows.begin (); it != m_mapWindows.end (); ++it )
140
+ auto lResult = 0L ; // assume that nobody will handle it.
141
+ for ( auto it = m_mapWindows.begin (); it != m_mapWindows.end (); ++it )
137
142
{
138
- HWND hWnd = it->first ;
143
+ const auto hWnd = it->first ;
139
144
switch ( msg )
140
145
{
141
146
case WM_KEYDOWN:
@@ -165,9 +170,9 @@ LRESULT KeyboardMonitor::HandleMessage
165
170
}
166
171
167
172
// -----------------------------------------------------------------------
168
- BOOL KeyboardMonitor::RejectKeyboadInputs ( BOOL bReject )
173
+ bool KeyboardMonitor::RejectKeyboadInputs (bool bReject )
169
174
{
170
- BOOL bThen = m_bRejectKeyBoardInputs;
175
+ const auto bThen = m_bRejectKeyBoardInputs;
171
176
m_bRejectKeyBoardInputs = bReject;
172
177
return bThen;
173
178
}
@@ -178,7 +183,7 @@ BOOL KeyboardMonitor::RejectKeyboadInputs( BOOL bReject )
178
183
*
179
184
* @return bool return true if we are rejecting all keyboard inputs.
180
185
*/
181
- BOOL KeyboardMonitor::RejectKeyboadInputs ( ) const
186
+ bool KeyboardMonitor::RejectKeyboadInputs ( ) const
182
187
{
183
188
return m_bRejectKeyBoardInputs;
184
189
}
@@ -190,13 +195,13 @@ void KeyboardMonitor::SetLastKey( const LAST_KEY& lk )
190
195
}
191
196
192
197
/* *
193
- * if the last DWORD used was more than 100 milli secs ago we can assume that it is a new key
198
+ * \brief if the last DWORD used was more than 100 milliseconds ago we can assume that it is a new key
194
199
* so we just return an empty key.
195
200
*
196
- * @ param DWORD the current time so we can compare if the last key was a long time ago,
197
- * @ return LAST_KEY the last key that was saved.
201
+ * \ param dwCurrentMilli the current time so we can compare if the last key was a long time ago,
202
+ * \ return LAST_KEY the last key that was saved.
198
203
*/
199
- KeyboardMonitor::LAST_KEY KeyboardMonitor::GetLastKey ( DWORD dwCurrentMilli)
204
+ KeyboardMonitor::LAST_KEY KeyboardMonitor::GetLastKey ( const DWORD dwCurrentMilli) const
200
205
{
201
206
if ( (dwCurrentMilli - m_lk.tick () ) > 100 /* 100 milli seconds*/ )
202
207
{
@@ -213,13 +218,13 @@ KeyboardMonitor::LAST_KEY KeyboardMonitor::GetLastKey( DWORD dwCurrentMilli)
213
218
}
214
219
215
220
// -----------------------------------------------------------------------
216
- BOOL KeyboardMonitor::RemoveHwnd ( HWND hWnd )
221
+ bool KeyboardMonitor::RemoveHwnd ( const HWND hWnd )
217
222
{
218
- std::map< HWND, WPARAM >::const_iterator iter = m_mapWindows.find ( hWnd );
223
+ const auto iter = m_mapWindows.find ( hWnd );
219
224
if ( iter == m_mapWindows.end () )
220
225
{
221
226
// don't know that item
222
- return FALSE ;
227
+ return false ;
223
228
}
224
229
225
230
// remove it
@@ -232,68 +237,68 @@ BOOL KeyboardMonitor::RemoveHwnd( HWND hWnd )
232
237
}
233
238
234
239
// if we are here then we still have some hooks needed.
235
- return TRUE ;
240
+ return true ;
236
241
}
237
242
238
243
// -----------------------------------------------------------------------
239
- BOOL KeyboardMonitor::CreateHooks ()
244
+ bool KeyboardMonitor::CreateHooks ()
240
245
{
241
246
// have we already created the hooks?
242
- if ( NULL != GetHHOOK () )
247
+ if (nullptr != GetHhook () )
243
248
{
244
- return TRUE ;
249
+ return true ;
245
250
}
246
251
247
252
// hook into the low level keyboard message
248
- HHOOK hookllKey = SetWindowsHookEx (WH_KEYBOARD_LL, ( HOOKPROC) CallLowLevelKeyboardProc, ( HINSTANCE) m_hModule,0 );
253
+ const auto hookllKey = SetWindowsHookEx (WH_KEYBOARD_LL, static_cast < HOOKPROC>( CallLowLevelKeyboardProc), static_cast < HINSTANCE>( m_hModule) ,0 );
249
254
250
255
// make sure that they are all created properly.
251
- if ( hookllKey == NULL )
256
+ if ( hookllKey == nullptr )
252
257
{
253
- return FALSE ;
258
+ return false ;
254
259
}
255
260
256
261
// save the value
257
- SetHHOOK ( hookllKey );
262
+ SetHhook ( hookllKey );
258
263
259
- return TRUE ;
264
+ return true ;
260
265
}
261
266
262
267
// -----------------------------------------------------------------------
263
- BOOL KeyboardMonitor::ClearHooks ( )
268
+ bool KeyboardMonitor::ClearHooks ( )
264
269
{
265
- if ( NULL == GetHHOOK () )
270
+ if ( nullptr == GetHhook () )
266
271
{
267
272
// we were not hooked in the first place.
268
- return FALSE ;
273
+ return false ;
269
274
}
270
275
271
276
272
- BOOL bResult = UnhookWindowsHookEx ( GetHHOOK () );
277
+ const auto bResult = UnhookWindowsHookEx ( GetHhook () );
273
278
274
- // what ever happened, (True or False), the user no longer wants that hook.
275
- SetHHOOK ( NULL );
279
+ // what ever happened, (true or False), the user no longer wants that hook.
280
+ SetHhook ( nullptr );
276
281
277
282
return bResult;
278
283
}
279
284
280
285
// -----------------------------------------------------------------------
281
- BOOL KeyboardMonitor::AddHwnd ( HWND hWnd, WPARAM vKey )
286
+ bool KeyboardMonitor::AddHwnd ( const HWND hWnd, const WPARAM vKey )
282
287
{
283
288
if ( !CreateHooks () )
284
289
{
285
- return FALSE ;
290
+ return false ;
286
291
}
287
292
288
293
// do we aready have that window?
289
- std::map< HWND, WPARAM >::const_iterator iter = m_mapWindows.find ( hWnd );
294
+ const auto iter = m_mapWindows.find ( hWnd );
290
295
if ( iter != m_mapWindows.end () )
291
296
{
292
- return FALSE ; // already exists.
297
+ return false ; // already exists.
293
298
}
294
299
295
300
// this is new so we need to add it here.
296
301
m_mapWindows[ hWnd ] = vKey;
297
302
298
- return TRUE ; // this was added.
303
+ return true ; // this was added.
299
304
}
0 commit comments