1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet25"
5     dependency "dwt" path="../../../../../../"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "glib-2.0" \
14       "gmodule-2.0" \
15       "gnomeui-2" \
16       "gnomevfs-2" \
17       "gobject-2.0" \
18       "gthread-2.0" \
19       "gtk-x11-2.0" \
20       "pango-1.0" \
21       "pangocairo-1.0" \
22       "X11" \
23       "Xcomposite" \
24       "Xcursor" \
25       "Xdamage" \
26       "Xext" \
27       "Xfixes" \
28       "Xi" \
29       "Xinerama" \
30       "Xrandr" \
31       "Xrender" \
32       "Xtst" \
33       platform="linux"
34 +/
35 
36 /*******************************************************************************
37  * Copyright (c) 2000, 2004 IBM Corporation and others.
38  * All rights reserved. This program and the accompanying materials
39  * are made available under the terms of the Eclipse Public License v1.0
40  * which accompanies this distribution, and is available at
41  * http://www.eclipse.org/legal/epl-v10.html
42  *
43  * Contributors:
44  *     IBM Corporation - initial API and implementation
45  * Port to the D programming language:
46  *     Bill Baxter <bill@billbaxter.com>
47  *******************************************************************************/
48 module org.eclipse.swt.snippets.Snippet25;
49  
50 /*
51  * Control example snippet: print key state, code and character
52  *
53  * For a list of all SWT example snippets see
54  * http://www.eclipse.org/swt/snippets/
55  * 
56  * @since 3.0
57  */
58 import org.eclipse.swt.SWT;
59 import org.eclipse.swt.widgets.Display;
60 import org.eclipse.swt.widgets.Shell;
61 import org.eclipse.swt.widgets.Listener;
62 import org.eclipse.swt.widgets.Event;
63 import java.lang.all;
64 
65 version(Tango){
66     import tango.util.Convert;
67     import tango.io.Stdout;
68     void writeln(in char[] line) {
69         Stdout(line)("\n").flush();
70     }
71 } else { // Phobos
72     import std.stdio;
73     import std.conv;
74 }
75 
76 String stateMask (int stateMask) {
77 	String string = "";
78 	if ((stateMask & SWT.CTRL) != 0) string ~= " CTRL";
79 	if ((stateMask & SWT.ALT) != 0) string ~= " ALT";
80 	if ((stateMask & SWT.SHIFT) != 0) string ~= " SHIFT";
81 	if ((stateMask & SWT.COMMAND) != 0) string ~= " COMMAND";
82 	return string;
83 }
84 
85 String character (wchar character) {
86 	switch (character) {
87     case 0: 		return "'\\0'";
88     case SWT.BS:	return "'\\b'";
89     case SWT.CR:	return "'\\r'";
90     case SWT.DEL:	return "DEL";
91     case SWT.ESC:	return "ESC";
92     case SWT.LF:	return "'\\n'";
93     case SWT.TAB:	return "'\\t'";
94     default:
95 		return to!(String)("'"w ~ character ~"'"w);
96 	}
97 }
98 
99 String keyCode (int keyCode) {
100 	switch (keyCode) {
101 		
102 		/* Keyboard and Mouse Masks */
103     case SWT.ALT: 		return "ALT";
104     case SWT.SHIFT: 	return "SHIFT";
105     case SWT.CONTROL:	return "CONTROL";
106     case SWT.COMMAND:	return "COMMAND";
107 			
108 		/* Non-Numeric Keypad Keys */
109     case SWT.ARROW_UP:		return "ARROW_UP";
110     case SWT.ARROW_DOWN:	return "ARROW_DOWN";
111     case SWT.ARROW_LEFT:	return "ARROW_LEFT";
112     case SWT.ARROW_RIGHT:	return "ARROW_RIGHT";
113     case SWT.PAGE_UP:		return "PAGE_UP";
114     case SWT.PAGE_DOWN:		return "PAGE_DOWN";
115     case SWT.HOME:			return "HOME";
116     case SWT.END:			return "END";
117     case SWT.INSERT:		return "INSERT";
118 
119 		/* Virtual and Ascii Keys */
120     case SWT.BS:	return "BS";
121     case SWT.CR:	return "CR";		
122     case SWT.DEL:	return "DEL";
123     case SWT.ESC:	return "ESC";
124     case SWT.LF:	return "LF";
125     case SWT.TAB:	return "TAB";
126 	
127 		/* Functions Keys */
128     case SWT.F1:	return "F1";
129     case SWT.F2:	return "F2";
130     case SWT.F3:	return "F3";
131     case SWT.F4:	return "F4";
132     case SWT.F5:	return "F5";
133     case SWT.F6:	return "F6";
134     case SWT.F7:	return "F7";
135     case SWT.F8:	return "F8";
136     case SWT.F9:	return "F9";
137     case SWT.F10:	return "F10";
138     case SWT.F11:	return "F11";
139     case SWT.F12:	return "F12";
140     case SWT.F13:	return "F13";
141     case SWT.F14:	return "F14";
142     case SWT.F15:	return "F15";
143 		
144 		/* Numeric Keypad Keys */
145     case SWT.KEYPAD_ADD:		return "KEYPAD_ADD";
146     case SWT.KEYPAD_SUBTRACT:	return "KEYPAD_SUBTRACT";
147     case SWT.KEYPAD_MULTIPLY:	return "KEYPAD_MULTIPLY";
148     case SWT.KEYPAD_DIVIDE:		return "KEYPAD_DIVIDE";
149     case SWT.KEYPAD_DECIMAL:	return "KEYPAD_DECIMAL";
150     case SWT.KEYPAD_CR:			return "KEYPAD_CR";
151     case SWT.KEYPAD_0:			return "KEYPAD_0";
152     case SWT.KEYPAD_1:			return "KEYPAD_1";
153     case SWT.KEYPAD_2:			return "KEYPAD_2";
154     case SWT.KEYPAD_3:			return "KEYPAD_3";
155     case SWT.KEYPAD_4:			return "KEYPAD_4";
156     case SWT.KEYPAD_5:			return "KEYPAD_5";
157     case SWT.KEYPAD_6:			return "KEYPAD_6";
158     case SWT.KEYPAD_7:			return "KEYPAD_7";
159     case SWT.KEYPAD_8:			return "KEYPAD_8";
160     case SWT.KEYPAD_9:			return "KEYPAD_9";
161     case SWT.KEYPAD_EQUAL:		return "KEYPAD_EQUAL";
162 
163 		/* Other keys */
164     case SWT.CAPS_LOCK:		return "CAPS_LOCK";
165     case SWT.NUM_LOCK:		return "NUM_LOCK";
166     case SWT.SCROLL_LOCK:	return "SCROLL_LOCK";
167     case SWT.PAUSE:			return "PAUSE";
168     case SWT.BREAK:			return "BREAK";
169     case SWT.PRINT_SCREEN:	return "PRINT_SCREEN";
170     case SWT.HELP:			return "HELP";
171 
172     default:
173         return character (cast(char) keyCode);
174 	}
175 
176 }
177 
178 void main () {
179 	Display display = new Display ();
180 	Shell shell = new Shell (display);
181 	Listener listener = new class Listener {
182 		public void handleEvent (Event e) {
183 			String string = Format("{} stateMask=0x{:x}{}, keyCode=0x{:x} {}, character=0x{:x} {}",
184 				e.type == SWT.KeyDown ? "DOWN:" : "UP  :",
185 				e.stateMask, stateMask (e.stateMask),
186 				e.keyCode, keyCode (e.keyCode),
187 				e.character, character (e.character));
188 			writeln(string);
189 		}
190 	};
191 	shell.addListener (SWT.KeyDown, listener);
192 	shell.addListener (SWT.KeyUp, listener);
193 	shell.setSize (200, 200);
194 	shell.open ();
195 	while (!shell.isDisposed ()) {
196 		if (!display.readAndDispatch ()) display.sleep ();
197 	}
198 	display.dispose ();
199 }