1 ///////////////////////////////////////////////////////////////////////////////
2 //Telnet Win32 : an ANSI telnet client.
3 //Copyright (C) 1998-2000 Paul Brannan
4 //Copyright (C) 1998 I.Ioannou
5 //Copyright (C) 1997 Brad Johnson
7 //This program is free software; you can redistribute it and/or
8 //modify it under the terms of the GNU General Public License
9 //as published by the Free Software Foundation; either version 2
10 //of the License, or (at your option) any later version.
12 //This program is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 //GNU General Public License for more details.
17 //You should have received a copy of the GNU General Public License
18 //along with this program; if not, write to the Free Software
19 //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 ///////////////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
27 // Key translations - I.Ioannou (roryt@hol.gr) //
28 // Athens - Greece December 18, 1996 02:56am //
29 // Reads a .cfg file and keeps the definitions //
30 // modified for alternate keymap swiching //
31 // by Andrey V. Smilianets (smile@head.aval.kiev.ua) //
32 // Kiev - Ukraine, December 1997. //
33 // modified to work with MSVC and the Standard Template //
34 // library by Paul Brannan <pbranna@clemson.edu>, //
36 // updated June 7, 1998 by Paul Brannan to remove cout and //
38 // APP_KEY and APP2_Key added July 12, 1998 by Paul Brannan //
39 ///////////////////////////////////////////////////////////////////
40 // class KeyTranslator //
41 // Load : loads or replaces the keymap //
42 // TranslateKey : returns a char * to the key def //
43 // AddKeyDef : Changes or adds the key translation //
44 // DeleteKeyDef : Deletes a key def from the list //
45 ///////////////////////////////////////////////////////////////////
49 /////////////////////////////////////////////////////////////
50 // class KeyTranslator //
51 // Load : loads or replaces the keymap //
52 // TranslateKey : returns a sz to the key def //
53 // AddKeyDef : Changes or adds the key translation //
54 // DeleteKeyDef : Deletes a key def from the list //
55 /////////////////////////////////////////////////////////////
58 KeyTranslator::KeyTranslator():
59 mapArray(0,0,sizeof(KeyMap
)),
60 globals(0,0,sizeof(TKeyDef
)) {
61 ext_mode
= 0; // Paul Brannan 8/28/98
62 currentKeyMap
= mainKeyMap
= -1;
66 // perform keymap switching
67 int KeyTranslator::switchMap(TKeyDef
& tk
) {
68 if ( mapArray
.IsEmpty() ) {
69 return currentKeyMap
= -1;
71 int i
= mapArray
.Find(KeyMap(tk
));
73 if (currentKeyMap
== i
)
74 currentKeyMap
= mainKeyMap
; // restore to default
75 else currentKeyMap
= i
;
81 // Let the calling function interpret the error code (Paul Brannan 12/17/98)
82 int KeyTranslator::SwitchTo(int to
) {
84 int max
= mapArray
.GetItemsInContainer();
85 if (max
== 0) return -1;
86 if (to
< 0 || to
> (max
-1)) return 0;
93 // rewrited to support multiple keymaps
94 const char *KeyTranslator::TranslateKey(WORD wVirtualKeyCode
,
95 DWORD dwControlKeyState
)
97 if ( mapArray
.IsEmpty() ) return NULL
;
99 TKeyDef
ask(NULL
, dwControlKeyState
, wVirtualKeyCode
);
101 // if a keymap switch pressed
102 if ( switchMap(ask
) > 0 ) return "";
104 int i
= mapArray
[currentKeyMap
].map
.Find(ask
);
106 if ( i
!= INT_MAX
) return mapArray
[currentKeyMap
].map
[i
].GetszKey();
108 // if not found in current keymap
109 if ( currentKeyMap
!= mainKeyMap
) {
110 i
= mapArray
[mainKeyMap
].map
.Find(ask
);
111 if ( i
!= INT_MAX
) return mapArray
[mainKeyMap
].map
[i
].GetszKey();
118 // rewrited to support multiple keymaps
119 int KeyTranslator::AddKeyDef(WORD wVirtualKeyCode
, DWORD dwControlKeyState
,
122 if ( ! mapArray
[currentKeyMap
].map
.IsEmpty() ) {
123 int i
= mapArray
[currentKeyMap
].map
.Find(TKeyDef(NULL
, dwControlKeyState
, wVirtualKeyCode
));
125 mapArray
[currentKeyMap
].map
[i
] = lpzKeyDef
;
129 return mapArray
[currentKeyMap
].map
.Add( TKeyDef(lpzKeyDef
, dwControlKeyState
, wVirtualKeyCode
));
132 // Paul Brannan Feb. 22, 1999
133 int KeyTranslator::AddKeyDef(WORD wVirtualKeyCode
, DWORD dwControlKeyState
,
139 if ( ! mapArray
[currentKeyMap
].map
.IsEmpty() ) {
140 int i
= mapArray
[currentKeyMap
].map
.Find(TKeyDef(NULL
, dwControlKeyState
, wVirtualKeyCode
));
142 mapArray
[currentKeyMap
].map
[i
] = op
;
146 return mapArray
[currentKeyMap
].map
.Add( TKeyDef(op
, dwControlKeyState
, wVirtualKeyCode
));
150 int KeyTranslator::LookOnGlobal(char* vkey
) {
151 if ( ! globals
.IsEmpty() ) {
152 int max
= globals
.GetItemsInContainer();
153 for ( int i
= 0; i
< max
; i
++ )
154 if ( stricmp(globals
[i
].GetszKey(), vkey
) == 0 )
160 int KeyTranslator::AddGlobalDef(WORD wVirtualKeyCode
, char*lpzKeyDef
) {
161 if ( ! globals
.IsEmpty() ) {
162 int max
= globals
.GetItemsInContainer();
163 for ( int i
= 0; i
< max
; i
++ ) {
164 const char *s
= globals
[i
].GetszKey();
165 if ( stricmp(s
, lpzKeyDef
) == 0 ) {
166 globals
[i
] = DWORD(wVirtualKeyCode
);
171 return globals
.Add( TKeyDef(lpzKeyDef
, 0, wVirtualKeyCode
));
176 // rewrited to support multiple keymaps
177 int KeyTranslator::DeleteKeyDef(WORD wVirtualKeyCode
, DWORD dwControlKeyState
)
179 if ( mapArray
.IsEmpty() || mapArray
[currentKeyMap
].map
.IsEmpty() )
182 int i
= mapArray
[currentKeyMap
].map
.Find(TKeyDef(NULL
, dwControlKeyState
, wVirtualKeyCode
));
185 mapArray
[currentKeyMap
].map
.Destroy(i
);
192 // rewritten to support multiple keymaps
193 void KeyTranslator::DeleteAllDefs(void)
195 // This code wants to crash under the STL; Apparently the Destroy()
196 // function actually deletes the entry, rather than simply releasing
197 // memory. I think flush() should do the same thing, at least the
198 // way it is written with STL_BIDS (Paul Brannan 5/25/98).
201 max
= mapArray
.GetItemsInContainer();
202 if ( ! mapArray
.IsEmpty() ) {
203 for ( int i
= 0; i
< max
; i
++ ) {
204 if ( !mapArray
[i
].map
.IsEmpty() ) {
205 mapArray
[i
].map
.Flush();