1 
2 module ws.gui.input;
3 
4 import io = ws.io;
5 
6 version(linux)
7 	import x11.keysymdef;
8 
9 version(Windows)
10 	import core.sys.windows.windows;
11 
12 __gshared:
13 
14 
15 enum Keys = [
16     "buttonLeft": Mouse.buttonLeft,
17     "buttonRight": Mouse.buttonRight,
18     "buttonMiddle": Mouse.buttonMiddle,
19     "wheelUp": Mouse.wheelUp,
20     "wheelDown": Mouse.wheelDown,
21     "button4": Mouse.button4,
22     "button5": Mouse.button5,
23     
24     "shift": Keyboard.shift,
25     "control": Keyboard.control,
26     "caps": Keyboard.caps,
27     "win": Keyboard.win,
28     "escape": Keyboard.escape,
29     "enter": Keyboard.enter,
30     "backspace": Keyboard.backspace,
31     "space": Keyboard.space,
32     "delete": Keyboard.del,
33     "left": Keyboard.left,
34     "right": Keyboard.right,
35     "up": Keyboard.up,
36     "down": Keyboard.down
37 ];
38 
39 
40 static class Mouse {
41 	alias int button;
42 	
43 	static const {
44 		version(Posix){
45 			button buttonLeft = 1;
46 			button buttonRight = 3;
47 			button buttonMiddle = 2;
48 			button wheelUp = 4;
49 			button wheelDown = 5;
50 			button button4 = 8;
51 			button button5 = 9;
52 		}else{
53 			button buttonLeft = 1;
54 			button buttonRight = 2;
55 			button buttonMiddle = 4;
56 			button wheelUp = 10;
57 			button wheelDown = 11;
58 			button button4 = 5;
59 			button button5 = 6;
60 		}
61 		int X = 5500;
62 		int Y = 5501;
63 	}
64 	
65 	enum cursor {
66 		arrow,
67 		inverted,
68 		text,
69 		sizeAll,
70 		sizeVert,
71 		sizeHoriz,
72 		pointTR,
73 		pointTL,
74 		pointBR,
75 		pointBL,
76 		hand,
77 		inherit,
78 		none
79 	}
80 }
81 
82 static class Keyboard {
83 	
84 	alias ushort key;
85 	
86 	version(Windows){
87 		enum: key {
88 			shift = 16,
89 			control = 17,
90 			caps = 20,
91 			win = 91,
92 			escape = 27,
93 			enter = 13,
94 			backspace = 8,
95 			space = 32,
96 			del = 46,
97 			
98 			left = 37,
99 			up = 38,
100 			right = 39,
101 			down = 40
102 		}
103 	}
104 	version(linux){
105 		enum: key {
106 			
107 			shift = cast(key)XK_Shift_L,
108 			shiftR = cast(key)XK_Shift_R,
109 			control = cast(key)XK_Control_L,
110 			controlR = cast(key)XK_Control_R,
111 			caps = cast(key)XK_Caps_Lock,
112 			win = cast(key)XK_Super_L,
113 			winR = cast(key)XK_Super_R,
114 
115 			escape =	cast(key)XK_Escape,
116 			enter = cast(key)XK_Return,
117 
118 			backspace = cast(key)XK_BackSpace,
119 			space = cast(key)XK_space,
120 			del = cast(key)XK_Delete,
121 
122 			left = cast(key)XK_Left,
123 			up = cast(key)XK_Up,
124 			right = cast(key)XK_Right,
125 			down = cast(key)XK_Down,
126 
127 		}
128 	}
129 	
130 	static bool get(key i){
131 		return chars[i];
132 	}
133 	
134 	static void set(key i, bool p){
135 		chars[i] = p;
136 	}
137 	
138 	static void emulate(key k, bool p){
139 		version(Windows){
140 			if(chars[k] == p)
141 				return;
142 			chars[k] = p;
143 			INPUT ip;
144 			ip.type = INPUT_KEYBOARD;
145 			ip.ki.wScan = 0;
146 			ip.ki.time = 0;
147 			ip.ki.dwExtraInfo = 0;
148 			ip.ki.wVk = k;
149 			ip.ki.dwFlags = (p ? 0 : KEYEVENTF_KEYUP);
150 			SendInput(1, &ip, INPUT.sizeof);
151 		}
152 	}
153 
154 	protected {
155 		static bool[key.max] chars = [false];
156 	}
157 	
158 }
159 
160 
161 version(Windows)
162 extern(Windows){
163 
164 	uint SendInput(uint cInputs, INPUT* pInputs, int cbSize);
165 
166 	struct INPUT {
167 		DWORD type;
168 		union {
169 			MOUSEINPUT	  mi;
170 			KEYBDINPUT	  ki;
171 			HARDWAREINPUT   hi;
172 		};
173 	}
174 
175 	struct MOUSEINPUT {
176 		LONG	dx;
177 		LONG	dy;
178 		DWORD   mouseData;
179 		DWORD   dwFlags;
180 		DWORD   time;
181 		ULONG_PTR dwExtraInfo;
182 	}
183 
184 	struct KEYBDINPUT {
185 		WORD	wVk;
186 		WORD	wScan;
187 		DWORD   dwFlags;
188 		DWORD   time;
189 		ULONG_PTR dwExtraInfo;
190 	}
191 
192 	struct HARDWAREINPUT {
193 		DWORD   uMsg;
194 		WORD	wParamL;
195 		WORD	wParamH;
196 	}
197 
198 	const int INPUT_KEYBOARD = 1;
199 	const int KEYEVENTF_KEYUP = 2;
200 
201 }
202