SDL 鍵盤事件監控

#include <stdlib.h>

#include "SDL.h"

//#pragma comment(lib, "SDL.lib")

// Screen width
#define WIDTH 480
// Screen height
#define HEIGHT 320

#define SAFE_RELEASE(p)      { if(p != NULL) { SDL_FreeSurface(p); (p)=NULL; } }

SDL_Surface *gScreen;

void OnKeyUp(SDL_KeyboardEvent *key);

void PrintModifiers( SDLMod mod );

int main(int argc, char *argv[])
{
 // Initialize SDL's subsystems
 if (SDL_Init(SDL_INIT_VIDEO) < 0)
 {
  fprintf(stderr, "Unable to init SDL: %s/n", SDL_GetError());
  exit(1);
 }

 int       done   = 0;
 
 SDL_Event event;
 SDL_EnableUNICODE( 1 );

 atexit(SDL_Quit);
 
 gScreen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_SWSURFACE);
 
 //init();
 
 // If we fail, return error.
 if (gScreen == NULL)
 {
  fprintf(stderr, "Unable to set up video: %s/n", SDL_GetError());
  exit(1);
 }
 
 // Main loop: loop forever.
 while ( !done )
 {
  while(SDL_PollEvent(&event))
  {
   switch (event.type)
   {
   case SDL_KEYDOWN:
    {
     if(event.key.keysym.sym == SDLK_ESCAPE)
      done = true;
     
    }
    break;
   case SDL_KEYUP:
    {
     
     OnKeyUp(&event.key);
    }
    break;
   }
  }
 }
 SAFE_RELEASE(gScreen);
 SDL_Quit();
 return 0;
}

void OnKeyUp(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" );
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章