1 /* 2 DSFML - The Simple and Fast Multimedia Library for D 3 4 Copyright (c) 2013 - 2015 Jeremy DeHaan (dehaan.jeremiah@gmail.com) 5 6 This software is provided 'as-is', without any express or implied warranty. 7 In no event will the authors be held liable for any damages arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, including commercial applications, 10 and to alter it and redistribute it freely, subject to the following restrictions: 11 12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 14 15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 16 17 3. This notice may not be removed or altered from any source distribution 18 */ 19 20 ///A module containing the Keyboard class. 21 module dsfml.window.keyboard; 22 23 /** 24 *Give access to the real-time state of the keyboard. 25 * 26 *Keyboard provides an interface to the state of the keyboard. 27 * 28 *It only contains static functions (a single keyboard is assumed), so it's not meant to be instanciated. 29 * 30 *This class allows users to query the keyboard state at any time and directly, without having to deal with 31 *a window and its events. Compared to the KeyPressed and KeyReleased events, Keyboard can retrieve the state 32 *of a key at any time (you don't need to store and update a boolean on your side in order to know if a key is 33 *pressed or released), and you always get the real state of the keyboard, even if keys are pressed or released 34 *when your window is out of focus and no event is triggered. 35 */ 36 final abstract class Keyboard 37 { 38 enum Key 39 { 40 ///Unhandled key 41 Unknown = -1, 42 ///The A key 43 A = 0, 44 ///The B key 45 B, 46 ///The C key 47 C, 48 ///The D key 49 D, 50 ///The E key 51 E, 52 ///The F key 53 F, 54 ///The G key 55 G, 56 ///The H key 57 H, 58 ///The I key 59 I, 60 ///The J key 61 J, 62 ///The K key 63 K, 64 ///The L key 65 L, 66 ///The M key 67 M, 68 ///The N key 69 N, 70 ///The O key 71 O, 72 ///The P key 73 P, 74 ///The Q key 75 Q, 76 ///The R key 77 R, 78 ///The S key 79 S, 80 ///The T key 81 T, 82 ///The U key 83 U, 84 ///The V key 85 V, 86 ///The W key 87 W, 88 ///The X key 89 X, 90 ///The Y key 91 Y, 92 ///The Z key 93 Z, 94 ///The 0 key 95 Num0, 96 ///The 1 key 97 Num1, 98 ///The 2 key 99 Num2, 100 ///The 3 key 101 Num3, 102 ///The 4 key 103 Num4, 104 ///The 5 key 105 Num5, 106 ///The 6 key 107 Num6, 108 ///The 7 key 109 Num7, 110 ///The 8 key 111 Num8, 112 ///The 9 key 113 Num9, 114 ///The Escape key 115 Escape, 116 ///The left Control key 117 LControl, 118 ///The left Shift key 119 LShift, 120 ///The left Alt key 121 LAlt, 122 ///The left OS specific key: window (Windows and Linux), apple (MacOS X), ... 123 LSystem, 124 ///The right Control key 125 RControl, 126 ///The right Shift key 127 RShift, 128 ///The right Alt key 129 RAlt, 130 ///The right OS specific key: window (Windows and Linux), apple (MacOS X), ... 131 RSystem, 132 ///The Menu key 133 Menu, 134 ///The [ key 135 LBracket, 136 ///The ] key 137 RBracket, 138 ///The ; key 139 SemiColon, 140 ///The , key 141 Comma, 142 ///The . key 143 Period, 144 ///The ' key 145 Quote, 146 ///The / key 147 Slash, 148 ///The \ key 149 BackSlash, 150 ///The ~ key 151 Tilde, 152 ///The = key 153 Equal, 154 ///The - key 155 Dash, 156 ///The Space key 157 Space, 158 ///The Return key 159 Return, 160 ///The Backspace key 161 BackSpace, 162 ///The Tabulation key 163 Tab, 164 ///The Page up key 165 PageUp, 166 ///The Page down key 167 PageDown, 168 ///The End key 169 End, 170 ///The Home key 171 Home, 172 ///The Insert key 173 Insert, 174 ///The Delete key 175 Delete, 176 ///The + key 177 Add, 178 ///The - key 179 Subtract, 180 ///The * key 181 Multiply, 182 ///The / key 183 Divide, 184 ///Left arrow 185 Left, 186 ///Right arrow 187 Right, 188 ///Up arrow 189 Up, 190 ///Down arrow 191 Down, 192 ///The numpad 0 key 193 Numpad0, 194 ///The numpad 1 key 195 Numpad1, 196 ///The numpad 2 key 197 Numpad2, 198 ///The numpad 3 key 199 Numpad3, 200 ///The numpad 4 key 201 Numpad4, 202 ///The numpad 5 key 203 Numpad5, 204 ///The numpad 6 key 205 Numpad6, 206 ///The numpad 7 key 207 Numpad7, 208 ///The numpad 8 key 209 Numpad8, 210 ///The numpad 9 key 211 Numpad9, 212 ///The F1 key 213 F1, 214 ///The F2 key 215 F2, 216 ///The F3 key 217 F3, 218 ///The F4 key 219 F4, 220 ///The F5 key 221 F5, 222 ///The F6 key 223 F6, 224 ///The F7 key 225 F7, 226 ///The F8 key 227 F8, 228 ///The F9 key 229 F9, 230 ///The F10 key 231 F10, 232 ///The F11 key 233 F11, 234 ///The F12 key 235 F12, 236 ///The F13 key 237 F13, 238 ///The F14 key 239 F14, 240 ///The F15 key 241 F15, 242 ///The Pause key 243 Pause, 244 245 ///Keep last -- the total number of keyboard keys 246 KeyCount 247 } 248 249 ///Check if a key is pressed. 250 /// 251 ///Params: 252 /// key = Key to check. 253 /// 254 ///Returns: True if the key is pressed, false otherwise. 255 static bool isKeyPressed(Key key) 256 { 257 return (sfKeyboard_isKeyPressed(key)); 258 } 259 260 } 261 262 //known bugs: 263 //cannot press two keys at once for this unit test 264 unittest 265 { 266 version(DSFML_Unittest_Window) 267 { 268 import std.stdio; 269 270 writeln("Unit test for Keyboard realtime input"); 271 272 bool running = true; 273 274 writeln("Press any key for real time input. Press esc to exit."); 275 276 string[int] keys; 277 //in its own scope for code folding 278 { 279 keys[-1] = "Unknown"; 280 keys[0] = "A"; 281 keys[1] = "B"; 282 keys[2] = "C"; 283 keys[3] = "D"; 284 keys[4] = "E"; 285 keys[5] = "F"; 286 keys[6] = "G"; 287 keys[7] = "H"; 288 keys[8] = "I"; 289 keys[9] = "J"; 290 keys[10] = "K"; 291 keys[11] = "L"; 292 keys[12] = "M"; 293 keys[13] = "N"; 294 keys[14] = "O"; 295 keys[15] = "P"; 296 keys[16] = "Q"; 297 keys[17] = "R"; 298 keys[18] = "S"; 299 keys[19] = "T"; 300 keys[20] = "U"; 301 keys[21] = "V"; 302 keys[22] = "W"; 303 keys[23] = "X"; 304 keys[24] = "Y"; 305 keys[25] = "Z"; 306 keys[26] = "Num0"; 307 keys[26] = "Num1"; 308 keys[28] = "Num2"; 309 keys[29] = "Num3"; 310 keys[30] = "Num4"; 311 keys[31] = "Num5"; 312 keys[32] = "Num6"; 313 keys[33] = "Num7"; 314 keys[34] = "Num8"; 315 keys[35] = "Num9"; 316 keys[36] = "Escape"; 317 keys[37] = "LControl"; 318 keys[38] = "LShift"; 319 keys[39] = "LAlt"; 320 keys[40] = "LSystem"; 321 keys[41] = "RControl"; 322 keys[42] = "RShift"; 323 keys[43] = "RAlt"; 324 keys[44] = "RSystem"; 325 keys[45] = "Menu"; 326 keys[46] = "LBracket"; 327 keys[47] = "RBracket"; 328 keys[48] = "SemiColon"; 329 keys[49] = "Comma"; 330 keys[50] = "Period"; 331 keys[51] = "Quote"; 332 keys[52] = "Slash"; 333 keys[53] = "BackSlash"; 334 keys[54] = "Tilde"; 335 keys[55] = "Equal"; 336 keys[56] = "Dash"; 337 keys[57] = "Space"; 338 keys[58] = "Return"; 339 keys[59] = "BackSpace"; 340 keys[60] = "Tab"; 341 keys[61] = "PageUp"; 342 keys[62] = "PageDown"; 343 keys[63] = "End"; 344 keys[64] = "Home"; 345 keys[65] = "Insert"; 346 keys[66] = "Delete"; 347 keys[67] = "Add"; 348 keys[68] = "Subtract"; 349 keys[69] = "Multiply"; 350 keys[70] = "Divide"; 351 keys[71] = "Left"; 352 keys[72] = "Right"; 353 keys[73] = "Up"; 354 keys[74] = "Down"; 355 keys[75] = "Numpad0"; 356 keys[76] = "Numpad1"; 357 keys[77] = "Numpad2"; 358 keys[78] = "Numpad3"; 359 keys[79] = "Numpad4"; 360 keys[80] = "Numpad5"; 361 keys[81] = "Numpad6"; 362 keys[82] = "Numpad7"; 363 keys[83] = "Numpad8"; 364 keys[84] = "Numpad9"; 365 keys[85] = "F1"; 366 keys[86] = "F2"; 367 keys[87] = "F3"; 368 keys[88] = "F4"; 369 keys[89] = "F5"; 370 keys[90] = "F6"; 371 keys[91] = "F7"; 372 keys[92] = "F8"; 373 keys[93] = "F9"; 374 keys[94] = "F10"; 375 keys[95] = "F11"; 376 keys[96] = "F12"; 377 keys[97] = "F13"; 378 keys[98] = "F14"; 379 keys[99] = "F15"; 380 keys[100] = "Pause"; 381 } 382 383 //must check for each possible key 384 while(running) 385 { 386 for(int i =-1;i<101;++i) 387 { 388 if(Keyboard.isKeyPressed(cast(Keyboard.Key)i)) 389 { 390 if(i in keys) 391 { 392 writeln("Key "~ keys[i] ~ " was pressed."); 393 } 394 else 395 { 396 writeln(i); 397 } 398 if(i == 36) 399 { 400 running = false; 401 } 402 } 403 } 404 } 405 } 406 } 407 408 private extern(C) 409 { 410 bool sfKeyboard_isKeyPressed(int key); 411 } 412 413