/
KeyMapper.java
495 lines (394 loc) · 20.1 KB
/
KeyMapper.java
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/**
* Title: KeyMapper
* Copyright: Copyright (c) 2001
* Company:
*
* @author Kenneth J. Pouncey
* @version 0.1
* <p>
* Description:
* <p>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/
package org.tn5250j.keyboard;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.KeyStroke;
import org.tn5250j.event.KeyChangeListener;
import org.tn5250j.interfaces.ConfigureFactory;
import org.tn5250j.interfaces.OptionAccessFactory;
import org.tn5250j.tools.LangTool;
public class KeyMapper {
private static HashMap<KeyStroker, String> mappedKeys;
private static KeyStroker workStroke;
private static String lastKeyMnemonic;
private static Vector<KeyChangeListener> listeners;
public static void init() {
if (mappedKeys != null)
return;
mappedKeys = new HashMap<KeyStroker, String>(60);
workStroke = new KeyStroker(0, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD);
Properties keys = ConfigureFactory.getInstance().getProperties(
ConfigureFactory.KEYMAP);
if (!containsProperties(keys)) {
// Key <-> Keycode , isShiftDown , isControlDown , isAlternateDown, location
mappedKeys.put(new KeyStroker(10, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[enter]");
mappedKeys.put(new KeyStroker(10, false, false, false, false, KeyStroker.KEY_LOCATION_NUMPAD), "[enter].alt2");
mappedKeys.put(new KeyStroker(8, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[backspace]");
mappedKeys.put(new KeyStroker(9, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[tab]");
mappedKeys.put(new KeyStroker(9, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[backtab]");
mappedKeys.put(new KeyStroker(127, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[delete]");
mappedKeys.put(new KeyStroker(155, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[insert]");
mappedKeys.put(new KeyStroker(19, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[clear]");
mappedKeys.put(new KeyStroker(17, false, true, false, false, KeyStroker.KEY_LOCATION_LEFT), "[reset]");
mappedKeys.put(new KeyStroker(27, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[sysreq]");
mappedKeys.put(new KeyStroker(35, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[eof]");
mappedKeys.put(new KeyStroker(36, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[home]");
mappedKeys.put(new KeyStroker(39, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[right]");
mappedKeys.put(new KeyStroker(39, false, false, true, false, KeyStroker.KEY_LOCATION_STANDARD), "[nextword]");
mappedKeys.put(new KeyStroker(37, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[left]");
mappedKeys.put(new KeyStroker(37, false, false, true, false, KeyStroker.KEY_LOCATION_STANDARD), "[prevword]");
mappedKeys.put(new KeyStroker(38, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[up]");
mappedKeys.put(new KeyStroker(40, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[down]");
mappedKeys.put(new KeyStroker(34, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pgdown]");
mappedKeys.put(new KeyStroker(33, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pgup]");
mappedKeys.put(new KeyStroker(96, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[keypad0]");
mappedKeys.put(new KeyStroker(97, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[keypad1]");
mappedKeys.put(new KeyStroker(98, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[keypad2]");
mappedKeys.put(new KeyStroker(99, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[keypad3]");
mappedKeys.put(new KeyStroker(100, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[keypad4]");
mappedKeys.put(new KeyStroker(101, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[keypad5]");
mappedKeys.put(new KeyStroker(102, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[keypad6]");
mappedKeys.put(new KeyStroker(103, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[keypad7]");
mappedKeys.put(new KeyStroker(104, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[keypad8]");
mappedKeys.put(new KeyStroker(105, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[keypad9]");
mappedKeys.put(new KeyStroker(109, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[field-]");
mappedKeys.put(new KeyStroker(107, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[field+]");
mappedKeys.put(new KeyStroker(112, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf1]");
mappedKeys.put(new KeyStroker(113, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf2]");
mappedKeys.put(new KeyStroker(114, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf3]");
mappedKeys.put(new KeyStroker(115, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf4]");
mappedKeys.put(new KeyStroker(116, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf5]");
mappedKeys.put(new KeyStroker(117, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf6]");
mappedKeys.put(new KeyStroker(118, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf7]");
mappedKeys.put(new KeyStroker(119, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf8]");
mappedKeys.put(new KeyStroker(120, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf9]");
mappedKeys.put(new KeyStroker(121, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf10]");
mappedKeys.put(new KeyStroker(122, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf11]");
mappedKeys.put(new KeyStroker(123, false, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf12]");
mappedKeys.put(new KeyStroker(112, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf13]");
mappedKeys.put(new KeyStroker(113, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf14]");
mappedKeys.put(new KeyStroker(114, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf15]");
mappedKeys.put(new KeyStroker(115, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf16]");
mappedKeys.put(new KeyStroker(116, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf17]");
mappedKeys.put(new KeyStroker(117, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf18]");
mappedKeys.put(new KeyStroker(118, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf19]");
mappedKeys.put(new KeyStroker(119, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf20]");
mappedKeys.put(new KeyStroker(120, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf21]");
mappedKeys.put(new KeyStroker(121, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf22]");
mappedKeys.put(new KeyStroker(122, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf23]");
mappedKeys.put(new KeyStroker(123, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[pf24]");
mappedKeys.put(new KeyStroker(112, false, false, true, false, KeyStroker.KEY_LOCATION_STANDARD), "[help]");
mappedKeys.put(new KeyStroker(72, false, false, true, false, KeyStroker.KEY_LOCATION_STANDARD), "[hostprint]");
mappedKeys.put(new KeyStroker(67, false, true, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[copy]");
mappedKeys.put(new KeyStroker(86, false, false, true, false, KeyStroker.KEY_LOCATION_STANDARD), "[paste]");
mappedKeys.put(new KeyStroker(39, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[markright]");
mappedKeys.put(new KeyStroker(37, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[markleft]");
mappedKeys.put(new KeyStroker(38, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[markup]");
mappedKeys.put(new KeyStroker(40, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[markdown]");
mappedKeys.put(new KeyStroker(155, true, false, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[dupfield]");
mappedKeys.put(new KeyStroker(17, true, true, false, false, KeyStroker.KEY_LOCATION_STANDARD), "[newline]");
mappedKeys.put(new KeyStroker(34, false, false, true, false, KeyStroker.KEY_LOCATION_STANDARD), "[jumpnext]");
mappedKeys.put(new KeyStroker(33, false, false, true, false, KeyStroker.KEY_LOCATION_STANDARD), "[jumpprev]");
saveKeyMap();
} else {
setKeyMap(keys);
}
}
private static boolean containsProperties(Properties keystrokes) {
if (keystrokes != null && keystrokes.size() > 0)
return true;
else
return false;
}
private static void parseKeyStrokes(Properties keystrokes) {
String theStringList = "";
String theKey = "";
Enumeration<?> ke = keystrokes.propertyNames();
while (ke.hasMoreElements()) {
theKey = (String) ke.nextElement();
if (OptionAccessFactory.getInstance().isRestrictedOption(theKey)) {
continue;
}
theStringList = keystrokes.getProperty(theKey);
int kc = 0;
boolean is = false;
boolean ic = false;
boolean ia = false;
boolean iag = false;
int location = KeyStroker.KEY_LOCATION_STANDARD;
StringTokenizer tokenizer = new StringTokenizer(theStringList, ",");
// first is the keycode
kc = Integer.parseInt(tokenizer.nextToken());
// isShiftDown
if (tokenizer.nextToken().equals("true"))
is = true;
else
is = false;
// isControlDown
if (tokenizer.nextToken().equals("true"))
ic = true;
else
ic = false;
// isAltDown
if (tokenizer.nextToken().equals("true"))
ia = true;
else
ia = false;
// isAltDown Gr
if (tokenizer.hasMoreTokens()) {
if (tokenizer.nextToken().equals("true"))
iag = true;
else
iag = false;
if (tokenizer.hasMoreTokens()) {
location = Integer.parseInt(tokenizer.nextToken());
}
}
mappedKeys.put(new KeyStroker(kc, is, ic, ia, iag, location), theKey);
}
}
protected static void setKeyMap(Properties keystrokes) {
parseKeyStrokes(keystrokes);
}
public final static boolean isEqualLast(KeyEvent ke) {
return workStroke.equals(ke);
}
public final static void saveKeyMap() {
Properties map = ConfigureFactory.getInstance().getProperties(ConfigureFactory.KEYMAP);
map.clear();
// save off the keystrokes in the keymap
Collection<String> v = mappedKeys.values();
Set<KeyStroker> o = mappedKeys.keySet();
Iterator<KeyStroker> k = o.iterator();
Iterator<String> i = v.iterator();
while (k.hasNext()) {
KeyStroker ks = k.next();
map.put(i.next(), ks.toString());
}
ConfigureFactory.getInstance().saveSettings(ConfigureFactory.KEYMAP,
"------ Key Map key=keycode,isShiftDown,isControlDown,isAltDown,isAltGrDown,location --------");
}
public final static String getKeyStrokeText(KeyEvent ke) {
return getKeyStrokeText(ke, false);
}
public final static String getKeyStrokeText(KeyEvent ke, boolean isAltGr) {
if (!workStroke.equals(ke, isAltGr)) {
workStroke.setAttributes(ke, isAltGr);
lastKeyMnemonic = mappedKeys.get(workStroke);
}
if (lastKeyMnemonic != null &&
lastKeyMnemonic.endsWith(KeyStroker.altSuffix)) {
lastKeyMnemonic = lastKeyMnemonic.substring(0,
lastKeyMnemonic.indexOf(KeyStroker.altSuffix));
}
return lastKeyMnemonic;
}
public final static String getKeyStrokeMnemonic(KeyEvent ke) {
return getKeyStrokeMnemonic(ke, false);
}
public final static String getKeyStrokeMnemonic(KeyEvent ke, boolean isAltGr) {
workStroke.setAttributes(ke, isAltGr);
String keyMnemonic = mappedKeys.get(workStroke);
if (keyMnemonic != null &&
keyMnemonic.endsWith(KeyStroker.altSuffix)) {
keyMnemonic = keyMnemonic.substring(0,
keyMnemonic.indexOf(KeyStroker.altSuffix));
}
return keyMnemonic;
}
public final static int getKeyStrokeCode() {
return workStroke.hashCode();
}
public final static String getKeyStrokeDesc(String which) {
Collection<String> v = mappedKeys.values();
Set<KeyStroker> o = mappedKeys.keySet();
Iterator<KeyStroker> k = o.iterator();
Iterator<String> i = v.iterator();
while (k.hasNext()) {
KeyStroker ks = k.next();
String keyVal = i.next();
if (keyVal.equals(which))
return ks.getKeyStrokeDesc();
}
return LangTool.getString("key.dead");
}
public final static KeyStroker getKeyStroker(String which) {
Collection<String> v = mappedKeys.values();
Set<KeyStroker> o = mappedKeys.keySet();
Iterator<KeyStroker> k = o.iterator();
Iterator<String> i = v.iterator();
while (k.hasNext()) {
KeyStroker ks = k.next();
String keyVal = i.next();
if (keyVal.equals(which))
return ks;
}
return null;
}
public final static boolean isKeyStrokeDefined(String which) {
Collection<String> v = mappedKeys.values();
Set<KeyStroker> o = mappedKeys.keySet();
Iterator<KeyStroker> k = o.iterator();
Iterator<String> i = v.iterator();
while (k.hasNext()) {
k.next();
String keyVal = i.next();
if (keyVal.equals(which))
return true;
}
return false;
}
public final static boolean isKeyStrokeDefined(KeyEvent ke) {
return isKeyStrokeDefined(ke, false);
}
public final static boolean isKeyStrokeDefined(KeyEvent ke, boolean isAltGr) {
workStroke.setAttributes(ke, isAltGr);
return (null != mappedKeys.get(workStroke));
}
public final static KeyStroke getKeyStroke(String which) {
Collection<String> v = mappedKeys.values();
Set<KeyStroker> o = mappedKeys.keySet();
Iterator<KeyStroker> k = o.iterator();
Iterator<String> i = v.iterator();
while (k.hasNext()) {
KeyStroker ks = k.next();
String keyVal = i.next();
if (keyVal.equals(which)) {
int mask = 0;
if (ks.isShiftDown())
mask |= InputEvent.SHIFT_MASK;
if (ks.isControlDown())
mask |= InputEvent.CTRL_MASK;
if (ks.isAltDown())
mask |= InputEvent.ALT_MASK;
if (ks.isAltGrDown())
mask |= InputEvent.ALT_GRAPH_MASK;
return KeyStroke.getKeyStroke(ks.getKeyCode(), mask);
}
}
return KeyStroke.getKeyStroke(0, 0);
}
public final static void removeKeyStroke(String which) {
Collection<String> v = mappedKeys.values();
Set<KeyStroker> o = mappedKeys.keySet();
Iterator<KeyStroker> k = o.iterator();
Iterator<String> i = v.iterator();
while (k.hasNext()) {
KeyStroker ks = k.next();
String keyVal = i.next();
if (keyVal.equals(which)) {
mappedKeys.remove(ks);
return;
}
}
}
public final static void setKeyStroke(String which, KeyEvent ke) {
if (ke == null)
return;
Collection<String> v = mappedKeys.values();
Set<KeyStroker> o = mappedKeys.keySet();
Iterator<KeyStroker> k = o.iterator();
Iterator<String> i = v.iterator();
while (k.hasNext()) {
KeyStroker ks = k.next();
String keyVal = i.next();
if (keyVal.equals(which)) {
mappedKeys.remove(ks);
mappedKeys.put(new KeyStroker(ke), keyVal);
return;
}
}
// if we got here it was a dead key and we need to add it.
mappedKeys.put(new KeyStroker(ke), which);
}
public final static void setKeyStroke(String which, KeyEvent ke, boolean isAltGr) {
if (ke == null)
return;
Collection<String> v = mappedKeys.values();
Set<KeyStroker> o = mappedKeys.keySet();
Iterator<KeyStroker> k = o.iterator();
Iterator<String> i = v.iterator();
while (k.hasNext()) {
KeyStroker ks = k.next();
String keyVal = i.next();
if (keyVal.equals(which)) {
mappedKeys.remove(ks);
mappedKeys.put(new KeyStroker(ke, isAltGr), keyVal);
return;
}
}
// if we got here it was a dead key and we need to add it.
mappedKeys.put(new KeyStroker(ke, isAltGr), which);
}
public final static HashMap<KeyStroker, String> getKeyMap() {
return mappedKeys;
}
/**
* Add a KeyChangeListener to the listener list.
*
* @param listener The KeyChangedListener to be added
*/
public static synchronized void addKeyChangeListener(KeyChangeListener listener) {
if (listeners == null) {
listeners = new java.util.Vector<KeyChangeListener>(3);
}
listeners.addElement(listener);
}
/**
* Remove a Key Change Listener from the listener list.
*
* @param listener The KeyChangeListener to be removed
*/
public synchronized void removeKeyChangeListener(KeyChangeListener listener) {
if (listeners == null) {
return;
}
listeners.removeElement(listener);
}
/**
* Notify all registered listeners of the Key Change Event.
*
*/
public static void fireKeyChangeEvent() {
if (listeners != null) {
int size = listeners.size();
for (int i = 0; i < size; i++) {
KeyChangeListener target =
listeners.elementAt(i);
target.onKeyChanged();
}
}
}
}