Mercurial > sdl-ios-xcode
comparison docs/html/guideinputkeyboard.html @ 55:55f1f1b3e27d
Added new docs for SDL 1.2.1
author | Sam Lantinga <slouken@lokigames.com> |
---|---|
date | Sun, 10 Jun 2001 19:31:57 +0000 |
parents | 74212992fb08 |
children | 355632dca928 |
comparison
equal
deleted
inserted
replaced
54:028447a8a758 | 55:55f1f1b3e27d |
---|---|
2 ><HEAD | 2 ><HEAD |
3 ><TITLE | 3 ><TITLE |
4 >Handling the Keyboard</TITLE | 4 >Handling the Keyboard</TITLE |
5 ><META | 5 ><META |
6 NAME="GENERATOR" | 6 NAME="GENERATOR" |
7 CONTENT="Modular DocBook HTML Stylesheet Version 1.61 | 7 CONTENT="Modular DocBook HTML Stylesheet Version 1.64 |
8 "><LINK | 8 "><LINK |
9 REL="HOME" | 9 REL="HOME" |
10 TITLE="SDL Library Documentation" | 10 TITLE="SDL Library Documentation" |
11 HREF="index.html"><LINK | 11 HREF="index.html"><LINK |
12 REL="UP" | 12 REL="UP" |
76 ><DIV | 76 ><DIV |
77 CLASS="SECT2" | 77 CLASS="SECT2" |
78 ><H2 | 78 ><H2 |
79 CLASS="SECT2" | 79 CLASS="SECT2" |
80 ><A | 80 ><A |
81 NAME="AEN205" | 81 NAME="AEN271" |
82 >Keyboard Related Structures</A | 82 >Keyboard Related Structures</A |
83 ></H2 | 83 ></H2 |
84 ><P | 84 ><P |
85 >It should make it a lot easier to understand this tutorial is you are familiar with the data types involved in keyboard access, so I'll explain them first.</P | 85 >It should make it a lot easier to understand this tutorial is you are familiar with the data types involved in keyboard access, so I'll explain them first.</P |
86 ><DIV | 86 ><DIV |
87 CLASS="SECT3" | 87 CLASS="SECT3" |
88 ><H3 | 88 ><H3 |
89 CLASS="SECT3" | 89 CLASS="SECT3" |
90 ><A | 90 ><A |
91 NAME="AEN208" | 91 NAME="AEN274" |
92 >SDLKey</A | 92 >SDLKey</A |
93 ></H3 | 93 ></H3 |
94 ><P | 94 ><P |
95 ><SPAN | 95 ><SPAN |
96 CLASS="STRUCTNAME" | 96 CLASS="STRUCTNAME" |
112 ><DIV | 112 ><DIV |
113 CLASS="SECT3" | 113 CLASS="SECT3" |
114 ><H3 | 114 ><H3 |
115 CLASS="SECT3" | 115 CLASS="SECT3" |
116 ><A | 116 ><A |
117 NAME="AEN216" | 117 NAME="AEN282" |
118 >SDLMod</A | 118 >SDLMod</A |
119 ></H3 | 119 ></H3 |
120 ><P | 120 ><P |
121 >SDLMod is an enumerated type, similar to <SPAN | 121 >SDLMod is an enumerated type, similar to <SPAN |
122 CLASS="STRUCTNAME" | 122 CLASS="STRUCTNAME" |
132 ><DIV | 132 ><DIV |
133 CLASS="SECT3" | 133 CLASS="SECT3" |
134 ><H3 | 134 ><H3 |
135 CLASS="SECT3" | 135 CLASS="SECT3" |
136 ><A | 136 ><A |
137 NAME="AEN222" | 137 NAME="AEN288" |
138 >SDL_keysym</A | 138 >SDL_keysym</A |
139 ></H3 | 139 ></H3 |
140 ><PRE | 140 ><PRE |
141 CLASS="PROGRAMLISTING" | 141 CLASS="PROGRAMLISTING" |
142 >typedef struct{ | 142 >typedef struct{ |
213 ><DIV | 213 ><DIV |
214 CLASS="SECT3" | 214 CLASS="SECT3" |
215 ><H3 | 215 ><H3 |
216 CLASS="SECT3" | 216 CLASS="SECT3" |
217 ><A | 217 ><A |
218 NAME="AEN241" | 218 NAME="AEN307" |
219 >SDL_KeyboardEvent</A | 219 >SDL_KeyboardEvent</A |
220 ></H3 | 220 ></H3 |
221 ><PRE | 221 ><PRE |
222 CLASS="PROGRAMLISTING" | 222 CLASS="PROGRAMLISTING" |
223 >typedef struct{ | 223 >typedef struct{ |
281 ><DIV | 281 ><DIV |
282 CLASS="SECT2" | 282 CLASS="SECT2" |
283 ><H2 | 283 ><H2 |
284 CLASS="SECT2" | 284 CLASS="SECT2" |
285 ><A | 285 ><A |
286 NAME="AEN258" | 286 NAME="AEN324" |
287 >Reading Keyboard Events</A | 287 >Reading Keyboard Events</A |
288 ></H2 | 288 ></H2 |
289 ><P | 289 ><P |
290 >Reading keybaord events from the event queue is quite simple (the event queue and using it is described <A | 290 >Reading keybaord events from the event queue is quite simple (the event queue and using it is described <A |
291 HREF="sdlevent.html" | 291 HREF="sdlevent.html" |
306 CLASS="LITERAL" | 306 CLASS="LITERAL" |
307 >SDL_KEYDOWN</TT | 307 >SDL_KEYDOWN</TT |
308 > events using a <TT | 308 > events using a <TT |
309 CLASS="LITERAL" | 309 CLASS="LITERAL" |
310 >switch</TT | 310 >switch</TT |
311 > statement, like so: | 311 > statement, like so:</P |
312 <PRE | 312 ><DIV |
313 CLASS="EXAMPLE" | |
314 ><A | |
315 NAME="AEN334" | |
316 ></A | |
317 ><P | |
318 ><B | |
319 >Example 3-10. Reading Keyboard Events</B | |
320 ></P | |
321 ><PRE | |
313 CLASS="PROGRAMLISTING" | 322 CLASS="PROGRAMLISTING" |
314 > SDL_Event event; | 323 > SDL_Event event; |
315 . | 324 . |
316 . | 325 . |
317 /* Poll for events. SDL_PollEvent() returns 0 when there are no */ | 326 /* Poll for events. SDL_PollEvent() returns 0 when there are no */ |
332 break; | 341 break; |
333 } | 342 } |
334 } | 343 } |
335 . | 344 . |
336 .</PRE | 345 .</PRE |
337 > | 346 ></DIV |
338 This is a very basic example. No information about the key press or release is interpreted. We will explore the other extreme out our first full example below - reporting all available information about a keyboard event.</P | 347 ><P |
348 >This is a very basic example. No information about the key press or release is interpreted. We will explore the other extreme out our first full example below - reporting all available information about a keyboard event.</P | |
339 ></DIV | 349 ></DIV |
340 ><DIV | 350 ><DIV |
341 CLASS="SECT2" | 351 CLASS="SECT2" |
342 ><H2 | 352 ><H2 |
343 CLASS="SECT2" | 353 CLASS="SECT2" |
344 ><A | 354 ><A |
345 NAME="AEN269" | 355 NAME="AEN338" |
346 >A More Detailed Look</A | 356 >A More Detailed Look</A |
347 ></H2 | 357 ></H2 |
348 ><P | 358 ><P |
349 >Before we can read events SDL must be initialised with <A | 359 >Before we can read events SDL must be initialised with <A |
350 HREF="sdlinit.html" | 360 HREF="sdlinit.html" |
382 ></BLOCKQUOTE | 392 ></BLOCKQUOTE |
383 ></DIV | 393 ></DIV |
384 ><DIV | 394 ><DIV |
385 CLASS="EXAMPLE" | 395 CLASS="EXAMPLE" |
386 ><A | 396 ><A |
387 NAME="AEN282" | 397 NAME="AEN351" |
388 ></A | 398 ></A |
389 ><P | 399 ><P |
390 ><B | 400 ><B |
391 >Example 3-1. keys.c - Key event information</B | 401 >Example 3-11. Interpreting Key Event Information</B |
392 ></P | 402 ></P |
393 ><PRE | 403 ><PRE |
394 CLASS="PROGRAMLISTING" | 404 CLASS="PROGRAMLISTING" |
395 > #include "SDL.h" | 405 > #include "SDL.h" |
396 | 406 |
403 | 413 |
404 SDL_Event event; | 414 SDL_Event event; |
405 int quit = 0; | 415 int quit = 0; |
406 | 416 |
407 /* Initialise SDL */ | 417 /* Initialise SDL */ |
408 if( SDL_Init( SDL_INIT_VIDEO ) ){ | 418 if( SDL_Init( SDL_INIT_VIDEO ) < 0){ |
409 fprintf( stderr, "Could not initialise SDL: %s\n", SDL_GetError() ); | 419 fprintf( stderr, "Could not initialise SDL: %s\n", SDL_GetError() ); |
410 exit( -1 ); | 420 exit( -1 ); |
411 } | 421 } |
412 | 422 |
413 /* Set a video mode */ | 423 /* Set a video mode */ |
516 ><DIV | 526 ><DIV |
517 CLASS="SECT2" | 527 CLASS="SECT2" |
518 ><H2 | 528 ><H2 |
519 CLASS="SECT2" | 529 CLASS="SECT2" |
520 ><A | 530 ><A |
521 NAME="AEN285" | 531 NAME="AEN354" |
522 >Game-type Input</A | 532 >Game-type Input</A |
523 ></H2 | 533 ></H2 |
524 ><P | 534 ><P |
525 >I have found that people using keyboard events for games and other interactive applications don't always understand one fundemental point.</P | 535 >I have found that people using keyboard events for games and other interactive applications don't always understand one fundemental point.</P |
526 ><A | 536 ><A |
527 NAME="AEN288" | 537 NAME="AEN357" |
528 ></A | 538 ></A |
529 ><BLOCKQUOTE | 539 ><BLOCKQUOTE |
530 CLASS="BLOCKQUOTE" | 540 CLASS="BLOCKQUOTE" |
531 ><P | 541 ><P |
532 >Keyboard events <I | 542 >Keyboard events <I |
573 . | 583 . |
574 .</PRE | 584 .</PRE |
575 > | 585 > |
576 At first glance you may think this is a perfectly reasonable piece of code for the task, but it isn't. Like I said keyboard events only occur when a key changes state, so the user would have to press and release the left cursor key 100 times to move the alien 100 pixels to the left.</P | 586 At first glance you may think this is a perfectly reasonable piece of code for the task, but it isn't. Like I said keyboard events only occur when a key changes state, so the user would have to press and release the left cursor key 100 times to move the alien 100 pixels to the left.</P |
577 ><P | 587 ><P |
578 >To get around this problem we must not use the events to change the position of the alien, we use the events to set flags which are then used in a seperate section of code to move the alien. Something like this: | 588 >To get around this problem we must not use the events to change the position of the alien, we use the events to set flags which are then used in a seperate section of code to move the alien. Something like this:</P |
579 <PRE | 589 ><DIV |
590 CLASS="EXAMPLE" | |
591 ><A | |
592 NAME="AEN363" | |
593 ></A | |
594 ><P | |
595 ><B | |
596 >Example 3-12. Proper Game Movement</B | |
597 ></P | |
598 ><PRE | |
580 CLASS="PROGRAMLISTING" | 599 CLASS="PROGRAMLISTING" |
581 > /* Alien screen coordinates */ | 600 > /* Alien screen coordinates */ |
582 int alien_x=0, alien_y=0; | 601 int alien_x=0, alien_y=0; |
583 int alien_xvel=0, alien_yvel=0; | 602 int alien_xvel=0, alien_yvel=0; |
584 . | 603 . |
647 . | 666 . |
648 . | 667 . |
649 /* Update the alien position */ | 668 /* Update the alien position */ |
650 alien_x += alien_xvel; | 669 alien_x += alien_xvel; |
651 alien_y += alien_yvel;</PRE | 670 alien_y += alien_yvel;</PRE |
652 > | 671 ></DIV |
653 As can be seen, we use two extra variables, alien_xvel and alien_yvel, which represent the motion of the ship, it is these variables that we update when we detect keypresses and releases.</P | 672 ><P |
673 >As can be seen, we use two extra variables, alien_xvel and alien_yvel, which represent the motion of the ship, it is these variables that we update when we detect keypresses and releases.</P | |
654 ></DIV | 674 ></DIV |
655 ></DIV | 675 ></DIV |
656 ><DIV | 676 ><DIV |
657 CLASS="NAVFOOTER" | 677 CLASS="NAVFOOTER" |
658 ><HR | 678 ><HR |