source: trunk/autoquest-misc/src/main/java/de/ugoe/cs/autoquest/keyboardmaps/VirtualKey.java @ 927

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