http://blog.sina.com.cn/ufownl[订阅]
字体大小: 正文
第三章 输入处理(中)(2007-08-23 23:07:41)
 

键盘

SDLKey 枚举类型,每一个符号代表一个键,如SDLK_a、SDLK_SPACE,在SDL_keysym.h中定义。

SDLMod 枚举类型,类似SDLKey,但用于修饰键,如Control、Alt、Shift,可以组合。

SDL_keysym

typedef struct{

  Uint8 scancode;

  SDLKey sym;

  SDLMod mod;

  Uint16 unicode;

} SDL_keysym;

用于按键和放开消息。scancode是硬件相关键码。除非你有特殊用途,否则忽略。sym指示键,mod为修饰键集合,例如KMOD_NUM | KMOD_CAPS | KMOD_LSHIFT 为Numlock、Capslock和左Shift组合。最后unicode为对应字符。注意,仅当是按键消息时unicode才有效,放开消息时无效。Unicode变换必须用SDL_EnableUNICODE()打开。

SDL_KeyboardEvent

typedef struct{

  Uint8 type;

  Uint8 state;

  SDL_keysym keysym;

} SDL_KeyboardEvent;

 

描述一个键盘消息。type指示按键(SDL_KEYDOWN)或放开(SDL_KEYUP)。state是冗余的,和含义type相同只是符号不同(SDL_PRESSED,SDL_RELEASED)。

读取键盘消息

使用消息循环用SDL_PollEvent()从消息队列里读取,并用switch-case检测SDL_KEYUP和SDL_KEYDOWN。下面是个基本的例子。

 

例3-10 读键盘消息

  SDL_Event event;

  .

  .

  /* Poll for events. SDL_PollEvent() returns 0 when there are no  */

  /* more events on the event queue, our while loop will exit when */

  /* that occurs.                                                  */

  while( SDL_PollEvent( &event ) ){

    /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */

    switch( event.type ){

      case SDL_KEYDOWN:

        printf( "Key press detected\n" );

        break;

 

      case SDL_KEYUP:

        printf( "Key release detected\n" );

        break;

 

      default:

        break;

    }

  }

更详细的内容

我们已经知道要用SDL_Init和SDL_SetVideoMode初始化,但我们还得用另外两个函数取得必要信息。要调用SDL_EnableUNICODE(1)以允许unicode变换,还要用SDL_GetKeyName将SDLKey转换成可打印字符。注意:小于0x80的unicode字符直接映射到ASCII码。下例用到这一点。

 

例3-11 解释按键消息

    #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 ) ){

            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" );

    }

加载中,请稍候...
  • 评论加载中,请稍候...

验证码:请点击后输入验证码  收听验证码

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

相关博文
读取中...
推荐博文
读取中...