📄 guideinputkeyboard.html
字号:
>. There are, however, two other functions we must use to obtain all the information required. We must enable unicode translation by calling <TTCLASS="FUNCTION">SDL_EnableUNICODE(1)</TT> and we must convert <SPANCLASS="STRUCTNAME">SDLKey</SPAN> values into something printable, using <AHREF="sdlgetkeyname.html"><TTCLASS="FUNCTION">SDL_GetKeyName</TT></A></P><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>It is useful to note that unicode values < 0x80 translate directly a characters ASCII value. THis is used in the example below</P></BLOCKQUOTE></DIV><DIVCLASS="EXAMPLE"><ANAME="AEN351"></A><P><B>Example 3-11. Interpreting Key Event Information</B></P><PRECLASS="PROGRAMLISTING"> #include "SDL.h" /* Function Prototypes */ void PrintKeyInfo( SDL_KeyboardEvent *key ); void PrintModifiers( SDLMod mod ); /* main */ int main( int argc, char *argv[] ){ SDL_Event event; int quit = 0; /* Initialise SDL */ if( SDL_Init( SDL_INIT_VIDEO ) < 0){ fprintf( stderr, "Could not initialise SDL: %s\n", SDL_GetError() ); exit( -1 ); } /* Set a video mode */ if( !SDL_SetVideoMode( 320, 200, 0, 0 ) ){ fprintf( stderr, "Could not set video mode: %s\n", SDL_GetError() ); SDL_Quit(); exit( -1 ); } /* Enable Unicode translation */ SDL_EnableUNICODE( 1 ); /* Loop until an SDL_QUIT event is found */ while( !quit ){ /* Poll for events */ while( SDL_PollEvent( &event ) ){ switch( event.type ){ /* Keyboard event */ /* Pass the event data onto PrintKeyInfo() */ case SDL_KEYDOWN: case SDL_KEYUP: PrintKeyInfo( &event.key ); break; /* SDL_QUIT event (window close) */ case SDL_QUIT: quit = 1; break; default: break; } } } /* Clean up */ SDL_Quit(); exit( 0 ); } /* Print all information about a key event */ void PrintKeyInfo( SDL_KeyboardEvent *key ){ /* Is it a release or a press? */ if( key->type == SDL_KEYUP ) printf( "Release:- " ); else printf( "Press:- " ); /* Print the hardware scancode first */ printf( "Scancode: 0x%02X", key->keysym.scancode ); /* Print the name of the key */ printf( ", Name: %s", SDL_GetKeyName( key->keysym.sym ) ); /* We want to print the unicode info, but we need to make */ /* sure its a press event first (remember, release events */ /* don't have unicode info */ if( key->type == SDL_KEYDOWN ){ /* If the Unicode value is less than 0x80 then the */ /* unicode value can be used to get a printable */ /* representation of the key, using (char)unicode. */ printf(", Unicode: " ); if( key->keysym.unicode < 0x80 && key->keysym.unicode > 0 ){ printf( "%c (0x%04X)", (char)key->keysym.unicode, key->keysym.unicode ); } else{ printf( "? (0x%04X)", key->keysym.unicode ); } } printf( "\n" ); /* Print modifier info */ PrintModifiers( key->keysym.mod ); } /* Print modifier info */ void PrintModifiers( SDLMod mod ){ printf( "Modifers: " ); /* If there are none then say so and return */ if( mod == KMOD_NONE ){ printf( "None\n" ); return; } /* Check for the presence of each SDLMod value */ /* This looks messy, but there really isn't */ /* a clearer way. */ if( mod & KMOD_NUM ) printf( "NUMLOCK " ); if( mod & KMOD_CAPS ) printf( "CAPSLOCK " ); if( mod & KMOD_LCTRL ) printf( "LCTRL " ); if( mod & KMOD_RCTRL ) printf( "RCTRL " ); if( mod & KMOD_RSHIFT ) printf( "RSHIFT " ); if( mod & KMOD_LSHIFT ) printf( "LSHIFT " ); if( mod & KMOD_RALT ) printf( "RALT " ); if( mod & KMOD_LALT ) printf( "LALT " ); if( mod & KMOD_CTRL ) printf( "CTRL " ); if( mod & KMOD_SHIFT ) printf( "SHIFT " ); if( mod & KMOD_ALT ) printf( "ALT " ); printf( "\n" ); }</PRE></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN354"></A>Game-type Input</H2><P>I have found that people using keyboard events for games and other interactive applications don't always understand one fundemental point.</P><ANAME="AEN357"></A><BLOCKQUOTECLASS="BLOCKQUOTE"><P>Keyboard events <SPANCLASS="emphasis"><ICLASS="EMPHASIS">only</I></SPAN> take place when a keys state changes from being unpressed to pressed, and vice versa.</P></BLOCKQUOTE><P>Imagine you have an image of an alien that you wish to move around using the cursor keys: when you pressed the left arrow key you want him to slide over to the left, and when you press the down key you want him to slide down the screen. Examine the following code; it highlights an error that many people have made.<PRECLASS="PROGRAMLISTING"> /* Alien screen coordinates */ int alien_x=0, alien_y=0; . . /* Initialise SDL and video modes and all that */ . /* Main game loop */ /* Check for events */ while( SDL_PollEvent( &event ) ){ switch( event.type ){ /* Look for a keypress */ case SDL_KEYDOWN: /* Check the SDLKey values and move change the coords */ switch( event.key.keysym.sym ){ case SDLK_LEFT: alien_x -= 1; break; case SDLK_RIGHT: alien_x += 1; break; case SDLK_UP: alien_y -= 1; break; case SDLK_DOWN: alien_y += 1; break; default: break; } } } } . .</PRE>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><P>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><DIVCLASS="EXAMPLE"><ANAME="AEN363"></A><P><B>Example 3-12. Proper Game Movement</B></P><PRECLASS="PROGRAMLISTING"> /* Alien screen coordinates */ int alien_x=0, alien_y=0; int alien_xvel=0, alien_yvel=0; . . /* Initialise SDL and video modes and all that */ . /* Main game loop */ /* Check for events */ while( SDL_PollEvent( &event ) ){ switch( event.type ){ /* Look for a keypress */ case SDL_KEYDOWN: /* Check the SDLKey values and move change the coords */ switch( event.key.keysym.sym ){ case SDLK_LEFT: alien_xvel = -1; break; case SDLK_RIGHT: alien_xvel = 1; break; case SDLK_UP: alien_yvel = -1; break; case SDLK_DOWN: alien_yvel = 1; break; default: break; } break; /* We must also use the SDL_KEYUP events to zero the x */ /* and y velocity variables. But we must also be */ /* careful not to zero the velocities when we shouldn't*/ case SDL_KEYUP: switch( event.key.keysym.sym ){ case SDLK_LEFT: /* We check to make sure the alien is moving */ /* to the left. If it is then we zero the */ /* velocity. If the alien is moving to the */ /* right then the right key is still press */ /* so we don't tocuh the velocity */ if( alien_xvel < 0 ) alien_xvel = 0; break; case SDLK_RIGHT: if( alien_xvel > 0 ) alien_xvel = 0; break; case SDLK_UP: if( alien_yvel < 0 ) alien_yvel = 0; break; case SDLK_DOWN: if( alien_yvel > 0 ) alien_yvel = 0; break; default: break; } break; default: break; } } . . /* Update the alien position */ alien_x += alien_xvel; alien_y += alien_yvel;</PRE></DIV><P>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></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="guideinput.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="guideexamples.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Input handling</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="guideinput.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Examples</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -