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