source: trunk/quest-misc/src/main/java/de/ugoe/cs/tasktree/keyboardmaps/VirtualKey.java @ 838

Last change on this file since 838 was 838, checked in by sherbold, 12 years ago
  • code documentation and clean-up
File size: 12.3 KB
Line 
1package de.ugoe.cs.tasktree.keyboardmaps;
2
3import java.awt.event.KeyEvent;
4
5/**
6 * <p>
7 * Enumeration of virtual keys.
8 * </p>
9 *
10 * @version 1.0
11 * @author Patrick Harms
12 */
13public enum VirtualKey {
14
15    ENTER(KeyEvent.VK_ENTER),
16    BACK_SPACE(KeyEvent.VK_BACK_SPACE),
17    TAB(KeyEvent.VK_TAB),
18    CANCEL(KeyEvent.VK_CANCEL),
19    CLEAR(KeyEvent.VK_CLEAR),
20    SHIFT(KeyEvent.VK_SHIFT),
21    CONTROL(KeyEvent.VK_CONTROL),
22    ALT(KeyEvent.VK_ALT),
23    PAUSE(KeyEvent.VK_PAUSE),
24    CAPS_LOCK(KeyEvent.VK_CAPS_LOCK),
25    ESCAPE(KeyEvent.VK_ESCAPE),
26    SPACE(KeyEvent.VK_SPACE),
27    PAGE_UP(KeyEvent.VK_PAGE_UP),
28    PAGE_DOWN(KeyEvent.VK_PAGE_DOWN),
29    END(KeyEvent.VK_END),
30    HOME(KeyEvent.VK_HOME),
31
32    LEFT(KeyEvent.VK_LEFT),
33    UP(KeyEvent.VK_UP),
34    RIGHT(KeyEvent.VK_RIGHT),
35    DOWN(KeyEvent.VK_DOWN),
36
37    COMMA(KeyEvent.VK_COMMA),
38    MINUS(KeyEvent.VK_MINUS),
39    PERIOD(KeyEvent.VK_PERIOD),
40    SLASH(KeyEvent.VK_SLASH),
41
42    DIGIT_0(KeyEvent.VK_0),
43    DIGIT_1(KeyEvent.VK_1),
44    DIGIT_2(KeyEvent.VK_2),
45    DIGIT_3(KeyEvent.VK_3),
46    DIGIT_4(KeyEvent.VK_4),
47    DIGIT_5(KeyEvent.VK_5),
48    DIGIT_6(KeyEvent.VK_6),
49    DIGIT_7(KeyEvent.VK_7),
50    DIGIT_8(KeyEvent.VK_8),
51    DIGIT_9(KeyEvent.VK_9),
52
53    SEMICOLON(KeyEvent.VK_SEMICOLON),
54    EQUALS(KeyEvent.VK_EQUALS),
55
56    LETTER_A(KeyEvent.VK_A),
57    LETTER_B(KeyEvent.VK_B),
58    LETTER_C(KeyEvent.VK_C),
59    LETTER_D(KeyEvent.VK_D),
60    LETTER_E(KeyEvent.VK_E),
61    LETTER_F(KeyEvent.VK_F),
62    LETTER_G(KeyEvent.VK_G),
63    LETTER_H(KeyEvent.VK_H),
64    LETTER_I(KeyEvent.VK_I),
65    LETTER_J(KeyEvent.VK_J),
66    LETTER_K(KeyEvent.VK_K),
67    LETTER_L(KeyEvent.VK_L),
68    LETTER_M(KeyEvent.VK_M),
69    LETTER_N(KeyEvent.VK_N),
70    LETTER_O(KeyEvent.VK_O),
71    LETTER_P(KeyEvent.VK_P),
72    LETTER_Q(KeyEvent.VK_Q),
73    LETTER_R(KeyEvent.VK_R),
74    LETTER_S(KeyEvent.VK_S),
75    LETTER_T(KeyEvent.VK_T),
76    LETTER_U(KeyEvent.VK_U),
77    LETTER_V(KeyEvent.VK_V),
78    LETTER_W(KeyEvent.VK_W),
79    LETTER_X(KeyEvent.VK_X),
80    LETTER_Y(KeyEvent.VK_Y),
81    LETTER_Z(KeyEvent.VK_Z),
82
83    OPEN_BRACKET(KeyEvent.VK_OPEN_BRACKET),
84    BACK_SLASH(KeyEvent.VK_BACK_SLASH),
85    CLOSE_BRACKET(KeyEvent.VK_CLOSE_BRACKET),
86
87    NUMPAD_0(KeyEvent.VK_NUMPAD0),
88    NUMPAD_1(KeyEvent.VK_NUMPAD1),
89    NUMPAD_2(KeyEvent.VK_NUMPAD2),
90    NUMPAD_3(KeyEvent.VK_NUMPAD3),
91    NUMPAD_4(KeyEvent.VK_NUMPAD4),
92    NUMPAD_5(KeyEvent.VK_NUMPAD5),
93    NUMPAD_6(KeyEvent.VK_NUMPAD6),
94    NUMPAD_7(KeyEvent.VK_NUMPAD7),
95    NUMPAD_8(KeyEvent.VK_NUMPAD8),
96    NUMPAD_9(KeyEvent.VK_NUMPAD9),
97    MULTIPLY(KeyEvent.VK_MULTIPLY),
98    ADD(KeyEvent.VK_ADD),
99
100    SEPARATOR(KeyEvent.VK_SEPARATOR),
101
102    SUBTRACT(KeyEvent.VK_SUBTRACT),
103    DECIMAL(KeyEvent.VK_DECIMAL),
104    DIVIDE(KeyEvent.VK_DIVIDE),
105    DELETE(KeyEvent.VK_DELETE),
106    NUM_LOCK(KeyEvent.VK_NUM_LOCK),
107    SCROLL_LOCK(KeyEvent.VK_SCROLL_LOCK),
108
109    F1(KeyEvent.VK_F1),
110    F2(KeyEvent.VK_F2),
111    F3(KeyEvent.VK_F3),
112    F4(KeyEvent.VK_F4),
113    F5(KeyEvent.VK_F5),
114    F6(KeyEvent.VK_F6),
115    F7(KeyEvent.VK_F7),
116    F8(KeyEvent.VK_F8),
117    F9(KeyEvent.VK_F9),
118    F10(KeyEvent.VK_F10),
119    F11(KeyEvent.VK_F11),
120    F12(KeyEvent.VK_F12),
121    F13(KeyEvent.VK_F13),
122    F14(KeyEvent.VK_F14),
123    F15(KeyEvent.VK_F15),
124    F16(KeyEvent.VK_F16),
125    F17(KeyEvent.VK_F17),
126    F18(KeyEvent.VK_F18),
127    F19(KeyEvent.VK_F19),
128    F20(KeyEvent.VK_F20),
129    F21(KeyEvent.VK_F21),
130    F22(KeyEvent.VK_F22),
131    F23(KeyEvent.VK_F23),
132    F24(KeyEvent.VK_F24),
133
134    PRINTSCREEN(KeyEvent.VK_PRINTSCREEN),
135    INSERT(KeyEvent.VK_INSERT),
136    HELP(KeyEvent.VK_HELP),
137    META(KeyEvent.VK_META),
138
139    BACK_QUOTE(KeyEvent.VK_BACK_QUOTE),
140    QUOTE(KeyEvent.VK_QUOTE),
141
142    KP_UP(KeyEvent.VK_KP_UP),
143    KP_DOWN(KeyEvent.VK_KP_DOWN),
144    KP_LEFT(KeyEvent.VK_KP_LEFT),
145    KP_RIGHT(KeyEvent.VK_KP_RIGHT),
146
147    DEAD_GRAVE(KeyEvent.VK_DEAD_GRAVE),
148    DEAD_ACUTE(KeyEvent.VK_DEAD_ACUTE),
149    DEAD_CIRCUMFLEX(KeyEvent.VK_DEAD_CIRCUMFLEX),
150    DEAD_TILDE(KeyEvent.VK_DEAD_TILDE),
151    DEAD_MACRON(KeyEvent.VK_DEAD_MACRON),
152    DEAD_BREVE(KeyEvent.VK_DEAD_BREVE),
153    DEAD_ABOVEDOT(KeyEvent.VK_DEAD_ABOVEDOT),
154    DEAD_DIAERESIS(KeyEvent.VK_DEAD_DIAERESIS),
155    DEAD_ABOVERING(KeyEvent.VK_DEAD_ABOVERING),
156    DEAD_DOUBLEACUTE(KeyEvent.VK_DEAD_DOUBLEACUTE),
157    DEAD_CARON(KeyEvent.VK_DEAD_CARON),
158    DEAD_CEDILLA(KeyEvent.VK_DEAD_CEDILLA),
159    DEAD_OGONEK(KeyEvent.VK_DEAD_OGONEK),
160    DEAD_IOTA(KeyEvent.VK_DEAD_IOTA),
161    DEAD_VOICED_SOUND(KeyEvent.VK_DEAD_VOICED_SOUND),
162    DEAD_SEMIVOICED_SOUND(KeyEvent.VK_DEAD_SEMIVOICED_SOUND),
163
164    AMPERSAND(KeyEvent.VK_AMPERSAND),
165    ASTERISK(KeyEvent.VK_ASTERISK),
166    QUOTEDBL(KeyEvent.VK_QUOTEDBL),
167    LESS(KeyEvent.VK_LESS),
168    GREATER(KeyEvent.VK_GREATER),
169    BRACELEFT(KeyEvent.VK_BRACELEFT),
170    BRACERIGHT(KeyEvent.VK_BRACERIGHT),
171
172    AT(KeyEvent.VK_AT),
173    COLON(KeyEvent.VK_COLON),
174    CIRCUMFLEX(KeyEvent.VK_CIRCUMFLEX),
175    DOLLAR(KeyEvent.VK_DOLLAR),
176    EURO_SIGN(KeyEvent.VK_EURO_SIGN),
177    EXCLAMATION_MARK(KeyEvent.VK_EXCLAMATION_MARK),
178    INVERTED_EXCLAMATION_MARK(KeyEvent.VK_INVERTED_EXCLAMATION_MARK),
179    LEFT_PARENTHESIS(KeyEvent.VK_LEFT_PARENTHESIS),
180    NUMBER_SIGN(KeyEvent.VK_NUMBER_SIGN),
181    PLUS(KeyEvent.VK_PLUS),
182    RIGHT_PARENTHESIS(KeyEvent.VK_RIGHT_PARENTHESIS),
183    UNDERSCORE(KeyEvent.VK_UNDERSCORE),
184
185    WINDOWS(KeyEvent.VK_WINDOWS),
186    CONTEXT_MENU(KeyEvent.VK_CONTEXT_MENU),
187
188    FINAL(KeyEvent.VK_FINAL),
189    CONVERT(KeyEvent.VK_CONVERT),
190    NONCONVERT(KeyEvent.VK_NONCONVERT),
191    ACCEPT(KeyEvent.VK_ACCEPT),
192    MODECHANGE(KeyEvent.VK_MODECHANGE),
193    KANA(KeyEvent.VK_KANA),
194    KANJI(KeyEvent.VK_KANJI),
195    ALPHANUMERIC(KeyEvent.VK_ALPHANUMERIC),
196    KATAKANA(KeyEvent.VK_KATAKANA),
197    HIRAGANA(KeyEvent.VK_HIRAGANA),
198    FULL_WIDTH(KeyEvent.VK_FULL_WIDTH),
199    HALF_WIDTH(KeyEvent.VK_HALF_WIDTH),
200    ROMAN_CHARACTERS(KeyEvent.VK_ROMAN_CHARACTERS),
201    ALL_CANDIDATES(KeyEvent.VK_ALL_CANDIDATES),
202    PREVIOUS_CANDIDATE(KeyEvent.VK_PREVIOUS_CANDIDATE),
203    CODE_INPUT(KeyEvent.VK_CODE_INPUT),
204    JAPANESE_KATAKANA(KeyEvent.VK_JAPANESE_KATAKANA),
205    JAPANESE_HIRAGANA(KeyEvent.VK_JAPANESE_HIRAGANA),
206    JAPANESE_ROMAN(KeyEvent.VK_JAPANESE_ROMAN),
207    KANA_LOCK(KeyEvent.VK_KANA_LOCK),
208    INPUT_METHOD_ON_OFF(KeyEvent.VK_INPUT_METHOD_ON_OFF),
209
210    CUT(KeyEvent.VK_CUT),
211    COPY(KeyEvent.VK_COPY),
212    PASTE(KeyEvent.VK_PASTE),
213    UNDO(KeyEvent.VK_UNDO),
214    AGAIN(KeyEvent.VK_AGAIN),
215    FIND(KeyEvent.VK_FIND),
216    PROPS(KeyEvent.VK_PROPS),
217    STOP(KeyEvent.VK_STOP),
218    COMPOSE(KeyEvent.VK_COMPOSE),
219    ALT_GRAPH(KeyEvent.VK_ALT_GRAPH),
220    BEGIN(KeyEvent.VK_BEGIN),
221
222    UNDEFINED(KeyEvent.VK_UNDEFINED);
223
224    /*
225     * BAR(KeyEvent.VK_UNDEFINED),
226     * APOSTROPHE(KeyEvent.VK_UNDEFINED),
227     * QUESTIONMARK(KeyEvent.VK_UNDEFINED),
228     * DEGREE(KeyEvent.VK_UNDEFINED),
229     * HENKAN_MODE(KeyEvent.VK_UNDEFINED),
230     * MUHENKAN(KeyEvent.VK_UNDEFINED),
231     * EISU_TOGGLE(KeyEvent.VK_UNDEFINED),
232     * HANGUL(KeyEvent.VK_UNDEFINED),
233     * HANGUL_HANJA(KeyEvent.VK_UNDEFINED),
234     * EXECUTE(KeyEvent.VK_UNDEFINED);
235     */
236
237    /**
238     * <p>
239     * Virtual key code of the virtual key.
240     * </p>
241     */
242    private int virtualKeyCode = -1;
243
244    /**
245     * <p>
246     * Description of the virtual key.
247     * </p>
248     */
249    private String description;
250
251    /**
252     * <p>
253     * Constructor. Creates a new VirtualKey.
254     * </p>
255     *
256     * @param virtualKeyCode
257     *            key code of the virtual key
258     */
259    private VirtualKey(int virtualKeyCode) {
260        this.virtualKeyCode = virtualKeyCode;
261        this.description = KeyEvent.getKeyText(this.virtualKeyCode);
262    }
263
264    /**
265     * <p>
266     * Returns the description of the virtual key.
267     * </p>
268     *
269     * @return the description.
270     */
271    String getDescription() {
272        return description;
273    }
274
275    /**
276     * <p>
277     * Returns whether the key is a combination key (e.g., shift, alt) or not.
278     * </p>
279     *
280     * @return true, if the key is a combiniation key; false otherwise
281     */
282    public boolean isCombinationKey() {
283        switch (this)
284        {
285            case SHIFT:
286            case CONTROL:
287            case ALT:
288            case ALT_GRAPH:
289            case WINDOWS:
290                return true;
291
292            default:
293                return false;
294        }
295    }
296
297    /**
298     * <p>
299     * Returns whether the key is a lock key (e.g., caps lock, num lock) or not.
300     * </p>
301     *
302     * @return true, if the key is a lock key; false otherwise
303     */
304    public boolean isLockKey() {
305        switch (this)
306        {
307            case CAPS_LOCK:
308            case NUM_LOCK:
309            case SCROLL_LOCK:
310                return true;
311
312            default:
313                return false;
314        }
315    }
316
317    /**
318     * <p>
319     * Returns whether the key is shift.
320     * </p>
321     *
322     * @return true, if the key is shift; false otherwise
323     */
324    public boolean isShiftKey() {
325        switch (this)
326        {
327            case SHIFT:
328                return true;
329
330            default:
331                return false;
332        }
333    }
334
335    /**
336     * <p>
337     * Returns whether the is an alt key.
338     * </p>
339     *
340     * @return true, if the key is alt or altgr; false otherwise
341     */
342    public boolean isAltKey() {
343        switch (this)
344        {
345            case ALT:
346            case ALT_GRAPH:
347                return true;
348
349            default:
350                return false;
351        }
352    }
353
354    /**
355     * <p>
356     * Returns whether the key is control.
357     * </p>
358     *
359     * @return true, if the key is control; false otherwise
360     */
361    public boolean isControlKey() {
362        switch (this)
363        {
364            case CONTROL:
365                return true;
366
367            default:
368                return false;
369        }
370    }
371
372    /**
373     * <p>
374     * Returns whether the key is the windows key.
375     * </p>
376     *
377     * @return true, if the key is the windows key; false otherwise
378     */
379    public boolean isWindowsKey() {
380        switch (this)
381        {
382            case WINDOWS:
383                return true;
384
385            default:
386                return false;
387        }
388    }
389
390    /**
391     * <p>
392     * Returns whether the key is the meta key.
393     * </p>
394     *
395     * @return true, if the key is the meta key; false otherwise
396     */
397    public boolean isMetaKey() {
398        switch (this)
399        {
400            case META:
401                return true;
402
403            default:
404                return false;
405        }
406    }
407
408    /**
409     * <p>
410     * Returns whether the key is a letter.
411     * </p>
412     *
413     * @return true, if the key is a letter; false otherwise
414     */
415    public boolean isLetter() {
416        if (virtualKeyCode > -1) {
417            return Character.isLetter((char) virtualKeyCode);
418        }
419        else {
420            return false;
421        }
422    }
423
424    /**
425     * <p>
426     * Returns whether the key is a digit.
427     * </p>
428     *
429     * @return true, if the key is a digit; false otherwise
430     */
431    public boolean isDigit() {
432        if (virtualKeyCode > -1) {
433            return Character.isDigit(virtualKeyCode);
434        }
435        else {
436            return false;
437        }
438    }
439
440    /**
441     * <p>
442     * Parses an {@link String} and returns the respective VirtualKey if possible.
443     * </p>
444     *
445     * @param numberString
446     *            String representation of the virtual key
447     * @return created VirtualKey
448     * @throws IllegalArgumentException
449     *             thrown if there is no VirtualKey that correlates to string
450     */
451    public static VirtualKey parseVirtualKey(String string) {
452        int virtualKeyCode = Integer.parseInt(string);
453        for (VirtualKey key1 : VirtualKey.values()) {
454            if (key1.virtualKeyCode == virtualKeyCode) {
455                return key1;
456            }
457        }
458
459        throw new IllegalArgumentException("there is no virtual key with id " + string);
460    }
461
462    /**
463     * <p>
464     * Returns the VirtualKey associated with an integer.
465     * </p>
466     *
467     * @param number
468     *            integer to which the according VirtualKey is returned
469     * @return the VirtualKey
470     * @throws IllegalArgumentException
471     *             thrown if there is no VirtualKey that correlates to number
472     */
473    public static VirtualKey valueOf(int number) {
474        for (VirtualKey virtualKey : VirtualKey.values()) {
475            if (virtualKey.virtualKeyCode == number) {
476                return virtualKey;
477            }
478        }
479
480        throw new IllegalArgumentException("there is no virtual key with number " + number);
481    }
482
483}
Note: See TracBrowser for help on using the repository browser.