001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013:
014: package org.wings.util;
015:
016: import java.awt.event.InputEvent;
017: import java.awt.event.KeyEvent;
018:
019: import javax.swing.KeyStroke;
020:
021: /**
022: * This source code was taken from a code example at http://javaalmanac.com/
023: * and slightly altered to fit our purpose.
024: * @author ole
025: *
026: */
027: public class KeystrokeUtil {
028:
029: public static String keyStroke2String(KeyStroke key) {
030: if (key == null)
031: return "";
032: SStringBuilder s = new SStringBuilder(50);
033: int m = key.getModifiers();
034:
035: if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
036: s.append("Ctrl+");
037: }
038: if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
039: s.append("Meta+");
040: }
041: if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
042: s.append("Alt+");
043: }
044: if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
045: s.append("Shift+");
046: }
047: if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
048: s.append("Button1+");
049: }
050: if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
051: s.append("Button2+");
052: }
053: if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
054: s.append("Button3+");
055: }
056:
057: switch (key.getKeyEventType()) {
058: case KeyEvent.KEY_TYPED:
059: s.append(key.getKeyChar() + " ");
060: break;
061: case KeyEvent.KEY_PRESSED:
062: case KeyEvent.KEY_RELEASED:
063: s.append(getKeyText(key.getKeyCode()) + " ");
064: break;
065: default:
066: s.append("unknown-event-type ");
067: break;
068: }
069:
070: return s.toString();
071: }
072:
073: public static String getKeyText(int keyCode) {
074: if (keyCode >= KeyEvent.VK_0 && keyCode <= KeyEvent.VK_9
075: || keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z) {
076: return String.valueOf((char) keyCode);
077: }
078:
079: switch (keyCode) {
080: case KeyEvent.VK_COMMA:
081: return "COMMA";
082: case KeyEvent.VK_PERIOD:
083: return "PERIOD";
084: case KeyEvent.VK_SLASH:
085: return "SLASH";
086: case KeyEvent.VK_SEMICOLON:
087: return "SEMICOLON";
088: case KeyEvent.VK_EQUALS:
089: return "EQUALS";
090: case KeyEvent.VK_OPEN_BRACKET:
091: return "OPEN_BRACKET";
092: case KeyEvent.VK_BACK_SLASH:
093: return "BACK_SLASH";
094: case KeyEvent.VK_CLOSE_BRACKET:
095: return "CLOSE_BRACKET";
096:
097: case KeyEvent.VK_ENTER:
098: return "ENTER";
099: case KeyEvent.VK_BACK_SPACE:
100: return "BACK_SPACE";
101: case KeyEvent.VK_TAB:
102: return "TAB";
103: case KeyEvent.VK_CANCEL:
104: return "CANCEL";
105: case KeyEvent.VK_CLEAR:
106: return "CLEAR";
107: case KeyEvent.VK_SHIFT:
108: return "SHIFT";
109: case KeyEvent.VK_CONTROL:
110: return "CONTROL";
111: case KeyEvent.VK_ALT:
112: return "ALT";
113: case KeyEvent.VK_PAUSE:
114: return "PAUSE";
115: case KeyEvent.VK_CAPS_LOCK:
116: return "CAPS_LOCK";
117: case KeyEvent.VK_ESCAPE:
118: return "ESCAPE";
119: case KeyEvent.VK_SPACE:
120: return "SPACE";
121: case KeyEvent.VK_PAGE_UP:
122: return "PAGE_UP";
123: case KeyEvent.VK_PAGE_DOWN:
124: return "PAGE_DOWN";
125: case KeyEvent.VK_END:
126: return "END";
127: case KeyEvent.VK_HOME:
128: return "HOME";
129: case KeyEvent.VK_LEFT:
130: return "LEFT";
131: case KeyEvent.VK_UP:
132: return "UP";
133: case KeyEvent.VK_RIGHT:
134: return "RIGHT";
135: case KeyEvent.VK_DOWN:
136: return "DOWN";
137:
138: // numpad numeric keys handled below
139: case KeyEvent.VK_MULTIPLY:
140: return "MULTIPLY";
141: case KeyEvent.VK_ADD:
142: return "ADD";
143: case KeyEvent.VK_SEPARATOR:
144: return "SEPARATOR";
145: case KeyEvent.VK_SUBTRACT:
146: return "SUBTRACT";
147: case KeyEvent.VK_DECIMAL:
148: return "DECIMAL";
149: case KeyEvent.VK_DIVIDE:
150: return "DIVIDE";
151: case KeyEvent.VK_DELETE:
152: return "DELETE";
153: case KeyEvent.VK_NUM_LOCK:
154: return "NUM_LOCK";
155: case KeyEvent.VK_SCROLL_LOCK:
156: return "SCROLL_LOCK";
157:
158: case KeyEvent.VK_F1:
159: return "F1";
160: case KeyEvent.VK_F2:
161: return "F2";
162: case KeyEvent.VK_F3:
163: return "F3";
164: case KeyEvent.VK_F4:
165: return "F4";
166: case KeyEvent.VK_F5:
167: return "F5";
168: case KeyEvent.VK_F6:
169: return "F6";
170: case KeyEvent.VK_F7:
171: return "F7";
172: case KeyEvent.VK_F8:
173: return "F8";
174: case KeyEvent.VK_F9:
175: return "F9";
176: case KeyEvent.VK_F10:
177: return "F10";
178: case KeyEvent.VK_F11:
179: return "F11";
180: case KeyEvent.VK_F12:
181: return "F12";
182: case KeyEvent.VK_F13:
183: return "F13";
184: case KeyEvent.VK_F14:
185: return "F14";
186: case KeyEvent.VK_F15:
187: return "F15";
188: case KeyEvent.VK_F16:
189: return "F16";
190: case KeyEvent.VK_F17:
191: return "F17";
192: case KeyEvent.VK_F18:
193: return "F18";
194: case KeyEvent.VK_F19:
195: return "F19";
196: case KeyEvent.VK_F20:
197: return "F20";
198: case KeyEvent.VK_F21:
199: return "F21";
200: case KeyEvent.VK_F22:
201: return "F22";
202: case KeyEvent.VK_F23:
203: return "F23";
204: case KeyEvent.VK_F24:
205: return "F24";
206:
207: case KeyEvent.VK_PRINTSCREEN:
208: return "PRINTSCREEN";
209: case KeyEvent.VK_INSERT:
210: return "INSERT";
211: case KeyEvent.VK_HELP:
212: return "HELP";
213: case KeyEvent.VK_META:
214: return "META";
215: case KeyEvent.VK_BACK_QUOTE:
216: return "BACK_QUOTE";
217: case KeyEvent.VK_QUOTE:
218: return "QUOTE";
219:
220: case KeyEvent.VK_KP_UP:
221: return "KP_UP";
222: case KeyEvent.VK_KP_DOWN:
223: return "KP_DOWN";
224: case KeyEvent.VK_KP_LEFT:
225: return "KP_LEFT";
226: case KeyEvent.VK_KP_RIGHT:
227: return "KP_RIGHT";
228:
229: case KeyEvent.VK_DEAD_GRAVE:
230: return "DEAD_GRAVE";
231: case KeyEvent.VK_DEAD_ACUTE:
232: return "DEAD_ACUTE";
233: case KeyEvent.VK_DEAD_CIRCUMFLEX:
234: return "DEAD_CIRCUMFLEX";
235: case KeyEvent.VK_DEAD_TILDE:
236: return "DEAD_TILDE";
237: case KeyEvent.VK_DEAD_MACRON:
238: return "DEAD_MACRON";
239: case KeyEvent.VK_DEAD_BREVE:
240: return "DEAD_BREVE";
241: case KeyEvent.VK_DEAD_ABOVEDOT:
242: return "DEAD_ABOVEDOT";
243: case KeyEvent.VK_DEAD_DIAERESIS:
244: return "DEAD_DIAERESIS";
245: case KeyEvent.VK_DEAD_ABOVERING:
246: return "DEAD_ABOVERING";
247: case KeyEvent.VK_DEAD_DOUBLEACUTE:
248: return "DEAD_DOUBLEACUTE";
249: case KeyEvent.VK_DEAD_CARON:
250: return "DEAD_CARON";
251: case KeyEvent.VK_DEAD_CEDILLA:
252: return "DEAD_CEDILLA";
253: case KeyEvent.VK_DEAD_OGONEK:
254: return "DEAD_OGONEK";
255: case KeyEvent.VK_DEAD_IOTA:
256: return "DEAD_IOTA";
257: case KeyEvent.VK_DEAD_VOICED_SOUND:
258: return "DEAD_VOICED_SOUND";
259: case KeyEvent.VK_DEAD_SEMIVOICED_SOUND:
260: return "DEAD_SEMIVOICED_SOUND";
261:
262: case KeyEvent.VK_AMPERSAND:
263: return "AMPERSAND";
264: case KeyEvent.VK_ASTERISK:
265: return "ASTERISK";
266: case KeyEvent.VK_QUOTEDBL:
267: return "QUOTEDBL";
268: case KeyEvent.VK_LESS:
269: return "LESS";
270: case KeyEvent.VK_GREATER:
271: return "GREATER";
272: case KeyEvent.VK_BRACELEFT:
273: return "BRACELEFT";
274: case KeyEvent.VK_BRACERIGHT:
275: return "BRACERIGHT";
276: case KeyEvent.VK_AT:
277: return "AT";
278: case KeyEvent.VK_COLON:
279: return "COLON";
280: case KeyEvent.VK_CIRCUMFLEX:
281: return "CIRCUMFLEX";
282: case KeyEvent.VK_DOLLAR:
283: return "DOLLAR";
284: case KeyEvent.VK_EURO_SIGN:
285: return "EURO_SIGN";
286: case KeyEvent.VK_EXCLAMATION_MARK:
287: return "EXCLAMATION_MARK";
288: case KeyEvent.VK_INVERTED_EXCLAMATION_MARK:
289: return "INVERTED_EXCLAMATION_MARK";
290: case KeyEvent.VK_LEFT_PARENTHESIS:
291: return "LEFT_PARENTHESIS";
292: case KeyEvent.VK_NUMBER_SIGN:
293: return "NUMBER_SIGN";
294: case KeyEvent.VK_MINUS:
295: return "MINUS";
296: case KeyEvent.VK_PLUS:
297: return "PLUS";
298: case KeyEvent.VK_RIGHT_PARENTHESIS:
299: return "RIGHT_PARENTHESIS";
300: case KeyEvent.VK_UNDERSCORE:
301: return "UNDERSCORE";
302:
303: case KeyEvent.VK_FINAL:
304: return "FINAL";
305: case KeyEvent.VK_CONVERT:
306: return "CONVERT";
307: case KeyEvent.VK_NONCONVERT:
308: return "NONCONVERT";
309: case KeyEvent.VK_ACCEPT:
310: return "ACCEPT";
311: case KeyEvent.VK_MODECHANGE:
312: return "MODECHANGE";
313: case KeyEvent.VK_KANA:
314: return "KANA";
315: case KeyEvent.VK_KANJI:
316: return "KANJI";
317: case KeyEvent.VK_ALPHANUMERIC:
318: return "ALPHANUMERIC";
319: case KeyEvent.VK_KATAKANA:
320: return "KATAKANA";
321: case KeyEvent.VK_HIRAGANA:
322: return "HIRAGANA";
323: case KeyEvent.VK_FULL_WIDTH:
324: return "FULL_WIDTH";
325: case KeyEvent.VK_HALF_WIDTH:
326: return "HALF_WIDTH";
327: case KeyEvent.VK_ROMAN_CHARACTERS:
328: return "ROMAN_CHARACTERS";
329: case KeyEvent.VK_ALL_CANDIDATES:
330: return "ALL_CANDIDATES";
331: case KeyEvent.VK_PREVIOUS_CANDIDATE:
332: return "PREVIOUS_CANDIDATE";
333: case KeyEvent.VK_CODE_INPUT:
334: return "CODE_INPUT";
335: case KeyEvent.VK_JAPANESE_KATAKANA:
336: return "JAPANESE_KATAKANA";
337: case KeyEvent.VK_JAPANESE_HIRAGANA:
338: return "JAPANESE_HIRAGANA";
339: case KeyEvent.VK_JAPANESE_ROMAN:
340: return "JAPANESE_ROMAN";
341: case KeyEvent.VK_KANA_LOCK:
342: return "KANA_LOCK";
343: case KeyEvent.VK_INPUT_METHOD_ON_OFF:
344: return "INPUT_METHOD_ON_OFF";
345:
346: case KeyEvent.VK_AGAIN:
347: return "AGAIN";
348: case KeyEvent.VK_UNDO:
349: return "UNDO";
350: case KeyEvent.VK_COPY:
351: return "COPY";
352: case KeyEvent.VK_PASTE:
353: return "PASTE";
354: case KeyEvent.VK_CUT:
355: return "CUT";
356: case KeyEvent.VK_FIND:
357: return "FIND";
358: case KeyEvent.VK_PROPS:
359: return "PROPS";
360: case KeyEvent.VK_STOP:
361: return "STOP";
362:
363: case KeyEvent.VK_COMPOSE:
364: return "COMPOSE";
365: case KeyEvent.VK_ALT_GRAPH:
366: return "ALT_GRAPH";
367: }
368:
369: if (keyCode >= KeyEvent.VK_NUMPAD0
370: && keyCode <= KeyEvent.VK_NUMPAD9) {
371: char c = (char) (keyCode - KeyEvent.VK_NUMPAD0 + '0');
372: return "NUMPAD" + c;
373: }
374:
375: return "unknown(0x" + Integer.toString(keyCode, 16) + ")";
376: }
377: }
|