SDL库的学习笔记之五—键盘

来源:互联网 发布:单片机程序开发 编辑:程序博客网 时间:2024/06/05 12:47

前情提要

上一次我们尝试了事件系统,这次,我们来试一下用键盘来进行输入
上次我们有这样的代码,这就是程序的基本架构了

    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

repeat

non-zero if this is a key repeat

SDL_Keysym

keysym

the SDL_Keysym representing the key that was pressed or released

这个SDL_Keysym也是一个结构体,里面是这样的,是读取到的不同类别的键盘键码

SDL_Scancode

scancode

SDL physical key code; see SDL_Scancode for details

SDL_Keycode

sym

SDL virtual key code; see SDL_Keycode for details

Uint16

mod

current key modifiers; see SDL_Keymod for details

Uint32

unused

那么,如果我有如下代码

    SDL_Event my_event;    int quit=0;    while(!quit){        while( SDL_PollEvent( &my_event ) != 0 ){            if( my_event.type == SDL_QUIT ){                quit = 1;            }        }           }   

我可以像这样进行键码的判断

            if( my_event.type == SDL_KEYDOWN ){                switch( my_event.key.keysym.sym ){                    case SDLK_0:                        /*do something*/                        break;                }            }

共同体和结构的嵌套看起来有点蛋疼= =不过其实很容易理解……
按键对应的键码可以在这里查看
SDL_Keycode

注意scancode是真正的按键扫描码,而keycode是SDL库规定的按键码,二者是不同的
需要转换的话,可以用SDL_GetKeyFromScancode和SDL_GetScancodeFromKey转换

函数原型如下:
SDL_Keycode SDL_GetKeyFromScancode(SDL_Scancode scancode)
SDL_Scancode SDL_GetScancodeFromKey(SDL_Keycode key)

最终代码

#include <stdlib.h>#include <stdio.h>#include "SDL2/SDL.h"int main(int argc, char* argv[]){    SDL_Window* my_window = NULL;    SDL_Surface* my_screen_surface = NULL;    SDL_Surface *my_image = NULL;    SDL_Event my_event;    SDL_Rect my_rect1={0,0,640/2,480};    SDL_Rect my_rect2={640/2,0,640/2,480};    int quit=0;    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {        SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());        return 1;    }    my_window = SDL_CreateWindow(        "HELLO WORLD",         SDL_WINDOWPOS_UNDEFINED,        SDL_WINDOWPOS_UNDEFINED,        640,        480,        SDL_WINDOW_SHOWN    );    if( my_window == NULL ){        printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );        return 1;    }    else{         my_screen_surface = SDL_GetWindowSurface( my_window );    }    my_image = SDL_LoadBMP( "hello.bmp" );    if( my_image == NULL ){        printf( "Unable to load image %s! SDL Error: %s\n", "hello.bmp", SDL_GetError() );        return 1;    }    while(!quit){        while( SDL_PollEvent( &my_event ) != 0 ){            if( my_event.type == SDL_QUIT ){                quit = 1;            }            if( my_event.type == SDL_KEYDOWN ){                switch( my_event.key.keysym.sym ){                    case SDLK_LEFT:                        SDL_BlitSurface(my_image, &my_rect1, my_screen_surface, &my_rect1);                        SDL_BlitSurface(my_image, &my_rect2, my_screen_surface, &my_rect2);                        SDL_UpdateWindowSurface(my_window);                        break;                    case SDLK_RIGHT:                        SDL_BlitSurface(my_image, &my_rect1, my_screen_surface, &my_rect2);                        SDL_BlitSurface(my_image, &my_rect2, my_screen_surface, &my_rect1);                        SDL_UpdateWindowSurface(my_window);                                             break;                }            }        }           }    SDL_FreeSurface( my_image );    SDL_DestroyWindow( my_window );    SDL_Quit();    return 0;}

按左右键进行图像调整
只要用左右方向键,就可以显示正常或者不正常(……)的图片

参考资料

Lazy Foo的教程

0 0