This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
keyboard.h
414 lines (345 loc) · 10.4 KB
/
keyboard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// keyboard.h
#ifndef _KEYBOARD_H
# define _KEYBOARD_H
# include "misc.h"
# include "driver.h"
# include "stringtool.h"
# include <vector>
# include <list>
# include <map>
/// a scan code with flags
class ScanCode
{
public:
///
enum
{
BREAK = KEYBOARD_INPUT_DATA::BREAK, /// key release flag
E0 = KEYBOARD_INPUT_DATA::E0, /// extended key flag
E1 = KEYBOARD_INPUT_DATA::E1, /// extended key flag
E0E1 = KEYBOARD_INPUT_DATA::E0E1, /// extended key flag
};
public:
USHORT m_scan; ///
USHORT m_flags; ///
public:
///
ScanCode() : m_scan(0), m_flags(0) { }
///
ScanCode(USHORT i_scan, USHORT i_flags)
: m_scan(i_scan), m_flags(i_flags) { }
///
bool operator==(const ScanCode &i_sc) const
{
return (m_scan == i_sc.m_scan &&
(E0E1 & m_flags) == (E0E1 & i_sc.m_flags));
}
///
bool operator!=(const ScanCode &i_sc) const { return !(*this == i_sc); }
};
/// a key
class Key
{
public:
enum
{
///
MAX_SCAN_CODES_SIZE = 4,
};
private:
///
typedef std::vector<tstringi> Names;
public:
/// if this key pressed physically
bool m_isPressed;
/// if this key pressed on Win32
bool m_isPressedOnWin32;
/// if this key pressed by assign
bool m_isPressedByAssign;
private:
/// key name
Names m_names;
/// key scan code length
size_t m_scanCodesSize;
/// key scan code
ScanCode m_scanCodes[MAX_SCAN_CODES_SIZE];
public:
///
Key()
: m_isPressed(false),
m_isPressedOnWin32(false),
m_isPressedByAssign(false),
m_scanCodesSize(0)
{ }
/// for Event::* only
Key(const tstringi &i_name)
: m_isPressed(false),
m_isPressedOnWin32(false),
m_isPressedByAssign(false),
m_scanCodesSize(0)
{
addName(i_name);
addScanCode(ScanCode());
}
/// get key name (first name)
const tstringi &getName() const { return m_names.front(); }
/// get scan codes
const ScanCode *getScanCodes() const { return m_scanCodes; }
///
size_t getScanCodesSize() const { return m_scanCodesSize; }
/// add a name of key
void addName(const tstringi &i_name);
/// add a scan code
void addScanCode(const ScanCode &i_sc);
/// initializer
Key &initialize();
/// equation by name
bool operator==(const tstringi &i_name) const;
///
bool operator!=(const tstringi &i_name) const
{ return !(*this == i_name); }
/// is the scan code of this key ?
bool isSameScanCode(const Key &i_key) const;
/// is the i_key's scan code the prefix of this key's scan code ?
bool isPrefixScanCode(const Key &i_key) const;
/// stream output
friend tostream &operator<<(tostream &i_ost, const Key &i_key);
/// <
bool operator<(const Key &i_key) const
{ return getName() < i_key.getName(); }
};
///
class Modifier
{
///
typedef u_int64 MODIFIERS;
///
MODIFIERS m_modifiers;
///
MODIFIERS m_dontcares;
public:
///
enum Type // モディファイヤキー名として使用できるキータイプ
{
Type_begin = 0, ///
Type_Shift = Type_begin, /// <BASIC_MODIFIER>
Type_Alt, /// <BASIC_MODIFIER>
Type_Control, /// <BASIC_MODIFIER>
Type_Windows, /// <BASIC_MODIFIER>
Type_BASIC, ///
Type_Up = Type_BASIC, /// <KEYSEQ_MODIFIER>
Type_Down, /// <KEYSEQ_MODIFIER>
Type_KEYSEQ, ///
Type_Repeat = Type_KEYSEQ, /// <ASSIGN_MODIFIER>
Type_ImeLock, /// <ASSIGN_MODIFIER>
Type_ImeComp, /// <ASSIGN_MODIFIER>
Type_NumLock, /// <ASSIGN_MODIFIER>
Type_CapsLock, /// <ASSIGN_MODIFIER>
Type_ScrollLock, /// <ASSIGN_MODIFIER>
Type_KanaLock, /// <ASSIGN_MODIFIER>
Type_Maximized, /// <ASSIGN_MODIFIER>
Type_Minimized, /// <ASSIGN_MODIFIER>
Type_MdiMaximized, /// <ASSIGN_MODIFIER>
Type_MdiMinimized, /// <ASSIGN_MODIFIER>
Type_Mod0, /// <ASSIGN_MODIFIER>
Type_Mod1, /// <ASSIGN_MODIFIER>
Type_Mod2, /// <ASSIGN_MODIFIER>
Type_Mod3, /// <ASSIGN_MODIFIER>
Type_Mod4, /// <ASSIGN_MODIFIER>
Type_Mod5, /// <ASSIGN_MODIFIER>
Type_Mod6, /// <ASSIGN_MODIFIER>
Type_Mod7, /// <ASSIGN_MODIFIER>
Type_Mod8, /// <ASSIGN_MODIFIER>
Type_Mod9, /// <ASSIGN_MODIFIER>
Type_Lock0, /// <ASSIGN_MODIFIER>
Type_Lock1, /// <ASSIGN_MODIFIER>
Type_Lock2, /// <ASSIGN_MODIFIER>
Type_Lock3, /// <ASSIGN_MODIFIER>
Type_Lock4, /// <ASSIGN_MODIFIER>
Type_Lock5, /// <ASSIGN_MODIFIER>
Type_Lock6, /// <ASSIGN_MODIFIER>
Type_Lock7, /// <ASSIGN_MODIFIER>
Type_Lock8, /// <ASSIGN_MODIFIER>
Type_Lock9, /// <ASSIGN_MODIFIER>
Type_ASSIGN, ///
Type_end = Type_ASSIGN ///
};
public:
///
Modifier();
///
Modifier &on(Type i_type) { return press(i_type); }
///
Modifier &off(Type i_type) { return release(i_type); }
///
Modifier &press(Type i_type)
{ m_modifiers |= ((MODIFIERS(1)) << i_type); return care(i_type); }
///
Modifier &release(Type i_type)
{ m_modifiers &= ~((MODIFIERS(1)) << i_type); return care(i_type); }
///
Modifier &care(Type i_type)
{ m_dontcares &= ~((MODIFIERS(1)) << i_type); return *this; }
///
Modifier &dontcare(Type i_type)
{ m_dontcares |= ((MODIFIERS(1)) << i_type); return *this; }
/// set all modifiers to dontcare
Modifier &dontcare() { m_dontcares = ~MODIFIERS(0); return *this; }
///
Modifier &on(Type i_type, bool i_isOn) { return press(i_type, i_isOn); }
///
Modifier &press(Type i_type, bool i_isPressed)
{ return i_isPressed ? press(i_type) : release(i_type); }
///
Modifier &care(Type i_type, bool i_doCare)
{ return i_doCare ? care(i_type) : dontcare(i_type); }
///
bool operator==(const Modifier &i_m) const
{ return m_modifiers == i_m.m_modifiers && m_dontcares == i_m.m_dontcares; }
/// add m's modifiers where this dontcare
void add(const Modifier &i_m);
//Modifier &operator+=(const Modifier &i_m);
/** does match. (except dontcare modifiers) (is the m included in the *this
set ?) */
bool doesMatch(const Modifier &i_m) const
{ return ((m_modifiers | m_dontcares) == (i_m.m_modifiers | m_dontcares)); }
///
bool isOn(Type i_type) const { return isPressed(i_type); }
///
bool isPressed(Type i_type) const
{ return !!(m_modifiers & ((MODIFIERS(1)) << i_type)); }
///
bool isDontcare(Type i_type) const
{ return !!(m_dontcares & ((MODIFIERS(1)) << i_type)); }
/// stream output
friend tostream &operator<<(tostream &i_ost, const Modifier &i_m);
/// <
bool operator<(const Modifier &i_m) const
{
return m_modifiers < i_m.m_modifiers ||
(m_modifiers == i_m.m_modifiers && m_dontcares < i_m.m_dontcares);
}
};
/// stream output
tostream &operator<<(tostream &i_ost, Modifier::Type i_type);
///
class ModifiedKey
{
public:
Modifier m_modifier; ///
Key *m_key; ///
public:
///
ModifiedKey() : m_key(NULL) { }
///
ModifiedKey(Key *i_key) : m_key(i_key) { }
///
ModifiedKey(const Modifier &i_modifier, Key *i_key)
: m_modifier(i_modifier), m_key(i_key) { }
///
bool operator==(const ModifiedKey &i_mk) const
{ return m_modifier == i_mk.m_modifier && m_key == i_mk.m_key; }
///
bool operator!=(const ModifiedKey &i_mk) const
{ return !operator==(i_mk); }
/// stream output
friend tostream &operator<<(tostream &i_ost, const ModifiedKey &i_mk);
/// <
bool operator<(const ModifiedKey &i_mk) const
{
return *m_key < *i_mk.m_key ||
(!(*i_mk.m_key < *m_key) && m_modifier < i_mk.m_modifier);
}
};
///
// キーの名前とスキャンコードを格納するamap
class Keyboard
{
public:
/// keyboard modifiers (pointer into Keys)
typedef std::list<Key *> Mods;
private:
/** keyboard keys (hashed by first scan code).
Keys must be *list* of Key.
Because *pointers* into Keys exist anywhere in this program, the address
of Key's elements must be fixed. */
enum {
HASHED_KEYS_SIZE = 128, ///
};
typedef std::list<Key> Keys; /// スキャンコード列を格納するリスト
typedef std::map<tstringi, Key *> Aliases; /// key name aliases
///
class Substitute
{
public:
ModifiedKey m_mkeyFrom;
ModifiedKey m_mkeyTo;
public:
Substitute(const ModifiedKey &i_mkeyFrom,
const ModifiedKey &i_mkeyTo)
: m_mkeyFrom(i_mkeyFrom), m_mkeyTo(i_mkeyTo)
{
}
};
typedef std::list<Substitute> Substitutes; /// substitutes
private:
Keys m_hashedKeys[HASHED_KEYS_SIZE]; /// スキャンコードの最初の値をキーとするハッシュテーブル hashtable[hash][list][scancode] な形
Aliases m_aliases; /// 別名
Substitutes m_substitutes; /// 代理キー
Key m_syncKey; /// key used to synchronize
private:
///
Mods m_mods[Modifier::Type_BASIC]; // 一般的なモディファイヤキー
public:
/// m_hashedKeys内のキーコードを列挙するためのクラス
class KeyIterator
{
///
Keys *m_hashedKeys;
///
size_t m_hashedKeysSize;
///
Keys::iterator m_i;
///
void next();
public:
///
KeyIterator(Keys *i_hashedKeys, size_t i_hashedKeysSize);
///
Key *operator *();
///
void operator++() { next(); }
};
private:
///
Keys &getKeys(const Key &i_key);
public:
/// add a key
void addKey(const Key &i_key);
/// add a key name alias
void addAlias(const tstringi &i_aliasName, Key *i_key);
/// add substitute
void addSubstitute(const ModifiedKey &i_mkeyFrom,
const ModifiedKey &i_mkeyTo);
/// get a sync key
Key *getSyncKey() { return &m_syncKey; }
/// add a modifier key
void addModifier(Modifier::Type i_mt, Key * i_key);
/// search a key
Key *searchKey(const Key &i_key);
/// search a key (of which the key's scan code is the prefix)
Key *searchPrefixKey(const Key &i_key);
/// search a key by name
Key *searchKey(const tstringi &i_name);
/// search a key by non-alias name
Key *searchKeyByNonAliasName(const tstringi &i_name);
/// search a substitute
ModifiedKey searchSubstitute(const ModifiedKey &i_mkey);
/// get modifiers
Mods &getModifiers(Modifier::Type i_mt) { return m_mods[i_mt]; }
/// get key iterator
KeyIterator getKeyIterator()
{ return KeyIterator(&m_hashedKeys[0], HASHED_KEYS_SIZE); }
};
#endif // !_KEYBOARD_H