前情提要
上一次我们尝试了事件系统,这次,我们来试一下用键盘来进行输入
上次我们有这样的代码,这就是程序的基本架构了
SDL_Event my_event;
int quit=0;
while(!quit){
while( SDL_PollEvent( &my_event ) != 0 ){
if( my_event.type == SDL_QUIT ){
quit = 1;
}
}
SDL_BlitSurface(my_image, NULL, my_screen_surface, NULL);
SDL_UpdateWindowSurface(my_window);
}
我们来完善它,使其能够实现键盘的输入
键盘事件
还记得我们上次讲的SDL_Event共同体吗?
如果我们进行了键盘输入,再用 SDL_PollEvent()得到这个键盘事件,那么,可以通过SDL_event共同体的成员key来访问。
key是SDL_KeyboardEvent类型的,这是一个结构体,成员如下。
Uint32 |
type |
the event type; SDL_KEYDOWN or SDL_KEYUP |
Uint32 |
timestamp |
timestamp of the event |
Uint32 |
windowID |
the window with keyboard focus, if any |
Uint8 |
state |
the state of the key; SDL_PRESSED or SDL_RELEASED |
Uint8 |