13
13
*
14
14
*/
15
15
16
- #if !defined( __TKEYS_H )
17
- #define __TKEYS_H
16
+ #if defined( Uses_TKeys ) && !defined( __TKeys )
17
+ #define __TKeys
18
18
19
19
#if defined( __FLAT__ ) && !defined( __WINDOWS_H )
20
20
#include < tvision/compat/windows/windows.h>
21
21
#endif
22
22
23
+ // // Key codes
24
+ //
25
+ // The following constants can be used to define menu hotkeys and to
26
+ // examine the 'keyCode' field of 'evKeyDown' event records.
27
+ //
28
+ // Not all key combinations can be used in all platforms. In particular:
29
+ //
30
+ // * In 16-bit mode, the 'control key' definitions cannot be used to define
31
+ // menu hotkeys, etc., which require scan codes. They are intended only
32
+ // to provide mnemonic names for the ASCII control codes.
33
+ //
34
+ // * In 16-bit mode, keystrokes corresponding to the 'additional extended
35
+ // key codes' are not reported.
36
+ //
37
+ // * Keystrokes corresponding to system shortcuts (e.g. Alt+F4 for 'close
38
+ // window' in graphical environments) are not reported either.
39
+ //
40
+ // * In Unix, support for key combinations varies among terminal emulators.
41
+ // Ctrl+H, Ctrl+I, Ctrl+J and Ctrl+M are not likely to work since they
42
+ // usually represent Backspace, Tab and Enter.
43
+ //
44
+ // These constants do not cover all possible combinations of the Shift, Ctrl
45
+ // and Alt modifiers. For that purpose, see the TKey class below.
46
+
23
47
const ushort
24
48
25
- // Control keys
26
- //
27
- // NOTE: these Control key definitions are intended only to provide
28
- // mnemonic names for the ASCII control codes. In 16-bit mode, they
29
- // cannot be used to define menu hotkeys, etc., which require scan codes.
49
+ // Control keys
30
50
31
51
kbCtrlA = 0x0001 , kbCtrlB = 0x0002 , kbCtrlC = 0x0003 ,
32
52
kbCtrlD = 0x0004 , kbCtrlE = 0x0005 , kbCtrlF = 0x0006 ,
@@ -78,7 +98,7 @@ const ushort
78
98
kbAlt0 = 0x8100 , kbAltMinus = 0x8200 , kbAltEqual = 0x8300 ,
79
99
kbCtrlPgUp = 0x8400 , kbNoKey = 0x0000 ,
80
100
81
- // Additional extended key codes, usable in 32-bit mode
101
+ // Additional extended key codes
82
102
83
103
kbAltEsc = 0x0100 , kbAltBack = 0x0e00 , kbF11 = 0x8500 ,
84
104
kbF12 = 0x8600 , kbShiftF11 = 0x8700 , kbShiftF12 = 0x8800 ,
@@ -90,18 +110,17 @@ const ushort
90
110
kbAltIns = 0xa200 , kbAltDel = 0xa300 , kbAltTab = 0xa500 ,
91
111
kbAltEnter = 0xa600 ,
92
112
93
- // Keyboard state and shift masks
94
-
95
- // Changes for this version:
96
- // In 32-bit mode, distinguishing between the right and left shift
97
- // keys is not supported, and there is an additional new flag,
98
- // kbEnhanced, which is set if the key pressed was an enhanced key
99
- // (e.g. <insert> or <home>)
113
+ // // Keyboard state and shift masks
114
+ //
115
+ // The following constants can be used when examining the 'controlKeyState'
116
+ // field of an 'evKeyDown' event record.
100
117
//
101
- // In 16-bit mode, there are additional flags for the right and left
102
- // Control and Alt keys, but this is not supported. The flags are
103
- // there for source compatibility with the 32-bit version which does
104
- // support this.
118
+ // Distinguishing between the left and right Shift keys is only supported in
119
+ // 16-bit mode, while distinguishing between the left and right Ctrl and Alt
120
+ // keys is only supported in 32-bit DPMI and on Windows.
121
+ //
122
+ // The kbScrollState, kbNumState, kbCapsState and kbEnhanced flags are only
123
+ // reported on DOS and Windows.
105
124
106
125
#if !defined( __FLAT__ )
107
126
kbLeftShift = 0x0001 ,
@@ -134,4 +153,64 @@ const ushort
134
153
kbInsState = 0x200 ; // Ensure this doesn't overlap above values
135
154
#endif
136
155
137
- #endif // __TKEYS_H
156
+ #endif // __TKeys
157
+
158
+ #if !defined( __TKey )
159
+ #define __TKey
160
+
161
+ // // TKey
162
+ //
163
+ // This class provides the ability to define new key combinations by
164
+ // specifying a key code and a mask of key modifiers. These key combinations
165
+ // can then be used to define menu hotkeys and examine 'evKeyDown' event
166
+ // records.
167
+ //
168
+ // The only modifiers taken into account are Shift, Ctrl and Alt, making no
169
+ // distinction between the left and right key on platforms that support it.
170
+ //
171
+ // Given that some key code constants already imply the presence of a key
172
+ // modifier, there are key combinations which can be defined in multiple
173
+ // ways. TKey ensures equality between all of them. For example:
174
+ //
175
+ // assert(kbCtrlA == TKey('A', kbCtrlShift));
176
+ // assert(TKey(kbCtrlTab, kbShift) == TKey(kbTab, kbShift | kbCtrlShift));
177
+ // assert(TKey(kbAltDel, kbCtrlShift) == TKey(kbCtrlDel, kbAltShift));
178
+
179
+ class TKey
180
+ {
181
+ public:
182
+
183
+ constexpr TKey () noexcept ;
184
+ TKey (ushort keyCode, ushort shiftState = 0 ) noexcept ;
185
+
186
+ ushort code;
187
+ ushort mods;
188
+ };
189
+
190
+ inline constexpr TKey::TKey () noexcept :
191
+ code(0 ),
192
+ mods(0 )
193
+ {
194
+ }
195
+
196
+ inline constexpr Boolean operator ==(TKey a, TKey b) noexcept
197
+ {
198
+ return (a.code | (a.mods << 16 )) == (b.code | (b.mods << 16 ));
199
+ }
200
+
201
+ inline constexpr Boolean operator !=(TKey a, TKey b) noexcept
202
+ {
203
+ return !(a == b);
204
+ }
205
+
206
+ inline ipstream& operator >> ( ipstream& is, TKey& p )
207
+ { return is >> p.code >> p.mods ; }
208
+ inline ipstream& operator >> ( ipstream& is, TKey*& p )
209
+ { return is >> p->code >> p->mods ; }
210
+
211
+ inline opstream& operator << ( opstream& os, TKey& p )
212
+ { return os << p.code << p.mods ; }
213
+ inline opstream& operator << ( opstream& os, TKey* p )
214
+ { return os << p->code << p->mods ; }
215
+
216
+ #endif // __TKey
0 commit comments