Mercurial > projects > dwt-samples
annotate snippets/control/Snippet25.d @ 120:1f0a7a472680
fix rm snippets from module statements.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 20 Jul 2008 19:17:23 +0200 |
parents | 4a04b6759f98 |
children |
rev | line source |
---|---|
78 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2004 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Bill Baxter <bill@billbaxter.com> | |
12 *******************************************************************************/ | |
120
1f0a7a472680
fix rm snippets from module statements.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
13 module control.Snippet25; |
78 | 14 |
15 /* | |
16 * Control example snippet: print key state, code and character | |
17 * | |
18 * For a list of all DWT example snippets see | |
19 * http://www.eclipse.org/swt/snippets/ | |
20 * | |
21 * @since 3.0 | |
22 */ | |
23 import dwt.DWT; | |
24 import dwt.widgets.Display; | |
25 import dwt.widgets.Shell; | |
26 import dwt.widgets.Listener; | |
27 import dwt.widgets.Event; | |
28 | |
29 import tango.io.Stdout; | |
30 import tango.text.convert.Format; | |
31 | |
32 static char[] stateMask (int stateMask) { | |
33 char[] string = ""; | |
34 if ((stateMask & DWT.CTRL) != 0) string ~= " CTRL"; | |
35 if ((stateMask & DWT.ALT) != 0) string ~= " ALT"; | |
36 if ((stateMask & DWT.SHIFT) != 0) string ~= " SHIFT"; | |
37 if ((stateMask & DWT.COMMAND) != 0) string ~= " COMMAND"; | |
38 return string; | |
39 } | |
40 | |
41 static char[] character (char character) { | |
42 switch (character) { | |
43 case 0: return "'\\0'"; | |
44 case DWT.BS: return "'\\b'"; | |
45 case DWT.CR: return "'\\r'"; | |
46 case DWT.DEL: return "DEL"; | |
47 case DWT.ESC: return "ESC"; | |
48 case DWT.LF: return "'\\n'"; | |
49 case DWT.TAB: return "'\\t'"; | |
50 default: | |
51 return "'" ~ character ~"'"; | |
52 } | |
53 } | |
54 | |
55 static char[] keyCode (int keyCode) { | |
56 switch (keyCode) { | |
57 | |
58 /* Keyboard and Mouse Masks */ | |
59 case DWT.ALT: return "ALT"; | |
60 case DWT.SHIFT: return "SHIFT"; | |
61 case DWT.CONTROL: return "CONTROL"; | |
62 case DWT.COMMAND: return "COMMAND"; | |
63 | |
64 /* Non-Numeric Keypad Keys */ | |
65 case DWT.ARROW_UP: return "ARROW_UP"; | |
66 case DWT.ARROW_DOWN: return "ARROW_DOWN"; | |
67 case DWT.ARROW_LEFT: return "ARROW_LEFT"; | |
68 case DWT.ARROW_RIGHT: return "ARROW_RIGHT"; | |
69 case DWT.PAGE_UP: return "PAGE_UP"; | |
70 case DWT.PAGE_DOWN: return "PAGE_DOWN"; | |
71 case DWT.HOME: return "HOME"; | |
72 case DWT.END: return "END"; | |
73 case DWT.INSERT: return "INSERT"; | |
74 | |
75 /* Virtual and Ascii Keys */ | |
76 case DWT.BS: return "BS"; | |
77 case DWT.CR: return "CR"; | |
78 case DWT.DEL: return "DEL"; | |
79 case DWT.ESC: return "ESC"; | |
80 case DWT.LF: return "LF"; | |
81 case DWT.TAB: return "TAB"; | |
82 | |
83 /* Functions Keys */ | |
84 case DWT.F1: return "F1"; | |
85 case DWT.F2: return "F2"; | |
86 case DWT.F3: return "F3"; | |
87 case DWT.F4: return "F4"; | |
88 case DWT.F5: return "F5"; | |
89 case DWT.F6: return "F6"; | |
90 case DWT.F7: return "F7"; | |
91 case DWT.F8: return "F8"; | |
92 case DWT.F9: return "F9"; | |
93 case DWT.F10: return "F10"; | |
94 case DWT.F11: return "F11"; | |
95 case DWT.F12: return "F12"; | |
96 case DWT.F13: return "F13"; | |
97 case DWT.F14: return "F14"; | |
98 case DWT.F15: return "F15"; | |
99 | |
100 /* Numeric Keypad Keys */ | |
101 case DWT.KEYPAD_ADD: return "KEYPAD_ADD"; | |
102 case DWT.KEYPAD_SUBTRACT: return "KEYPAD_SUBTRACT"; | |
103 case DWT.KEYPAD_MULTIPLY: return "KEYPAD_MULTIPLY"; | |
104 case DWT.KEYPAD_DIVIDE: return "KEYPAD_DIVIDE"; | |
105 case DWT.KEYPAD_DECIMAL: return "KEYPAD_DECIMAL"; | |
106 case DWT.KEYPAD_CR: return "KEYPAD_CR"; | |
107 case DWT.KEYPAD_0: return "KEYPAD_0"; | |
108 case DWT.KEYPAD_1: return "KEYPAD_1"; | |
109 case DWT.KEYPAD_2: return "KEYPAD_2"; | |
110 case DWT.KEYPAD_3: return "KEYPAD_3"; | |
111 case DWT.KEYPAD_4: return "KEYPAD_4"; | |
112 case DWT.KEYPAD_5: return "KEYPAD_5"; | |
113 case DWT.KEYPAD_6: return "KEYPAD_6"; | |
114 case DWT.KEYPAD_7: return "KEYPAD_7"; | |
115 case DWT.KEYPAD_8: return "KEYPAD_8"; | |
116 case DWT.KEYPAD_9: return "KEYPAD_9"; | |
117 case DWT.KEYPAD_EQUAL: return "KEYPAD_EQUAL"; | |
118 | |
119 /* Other keys */ | |
120 case DWT.CAPS_LOCK: return "CAPS_LOCK"; | |
121 case DWT.NUM_LOCK: return "NUM_LOCK"; | |
122 case DWT.SCROLL_LOCK: return "SCROLL_LOCK"; | |
123 case DWT.PAUSE: return "PAUSE"; | |
124 case DWT.BREAK: return "BREAK"; | |
125 case DWT.PRINT_SCREEN: return "PRINT_SCREEN"; | |
126 case DWT.HELP: return "HELP"; | |
127 | |
128 default: | |
129 return character (cast(char) keyCode); | |
130 } | |
131 | |
132 } | |
133 | |
134 void main () { | |
135 Display display = new Display (); | |
136 Shell shell = new Shell (display); | |
137 Listener listener = new class Listener { | |
138 public void handleEvent (Event e) { | |
139 char[] string = e.type == DWT.KeyDown ? "DOWN:" : "UP :"; | |
140 string ~= Format("stateMask=0x{:x}{},", e.stateMask, stateMask (e.stateMask)); | |
141 string ~= Format(" keyCode=0x{:x} {},", e.keyCode, keyCode (e.keyCode)); | |
142 string ~= Format(" character=0x{:x} {}", e.character, character (e.character)); | |
143 Stdout.formatln (string); | |
144 } | |
145 }; | |
146 shell.addListener (DWT.KeyDown, listener); | |
147 shell.addListener (DWT.KeyUp, listener); | |
148 shell.setSize (200, 200); | |
149 shell.open (); | |
150 while (!shell.isDisposed ()) { | |
151 if (!display.readAndDispatch ()) display.sleep (); | |
152 } | |
153 display.dispose (); | |
154 } |