免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1761 | 回复: 4
打印 上一主题 下一主题

请问这种情况如何处理啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-05-11 17:52 |只看该作者 |倒序浏览
程序采用事件触发,在一个比较大的循环体里,当检测到按键则退出循环,那按键应该如何检测呢?(捕捉按键的函数采用不等待按键的方式)
不知道描述清楚没?目的就是不影响程序的效率,又能快速捕捉到按键。
我估计这种情况应该很常见,新手忘各位指教。

论坛徽章:
0
2 [报告]
发表于 2010-05-17 21:19 |只看该作者
可以用一守护进程去处理。但检测按钮跟硬件有点关系,具体不详。

论坛徽章:
0
3 [报告]
发表于 2010-05-17 22:52 |只看该作者
Example 3-10. Reading Keyboard Events
  SDL_Event event;

  1.   /* Poll for events. SDL_PollEvent() returns 0 when there are no  */
  2.   /* more events on the event queue, our while loop will exit when */
  3.   /* that occurs.                                                  */
  4.   while( SDL_PollEvent( &event ) ){
  5.     /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */
  6.     switch( event.type ){
  7.       case SDL_KEYDOWN:
  8.         printf( "Key press detected\n" );
  9.         break;

  10.       case SDL_KEYUP:
  11.         printf( "Key release detected\n" );
  12.         break;

  13.       default:
  14.         break;
  15.     }
  16.   }
复制代码

  1.   #include "SDL.h"

  2.     /* Function Prototypes */
  3.     void PrintKeyInfo( SDL_KeyboardEvent *key );
  4.     void PrintModifiers( SDLMod mod );

  5.     /* main */
  6.     int main( int argc, char *argv[] ){
  7.         
  8.         SDL_Event event;
  9.         int quit = 0;
  10.         
  11.         /* Initialise SDL */
  12.         if( SDL_Init( SDL_INIT_VIDEO ) ){
  13.             fprintf( stderr, "Could not initialise SDL: %s\n", SDL_GetError() );
  14.             exit( -1 );
  15.         }

  16.         /* Set a video mode */
  17.         if( !SDL_SetVideoMode( 320, 200, 0, 0 ) ){
  18.             fprintf( stderr, "Could not set video mode: %s\n", SDL_GetError() );
  19.             SDL_Quit();
  20.             exit( -1 );
  21.         }

  22.         /* Enable Unicode translation */
  23.         SDL_EnableUNICODE( 1 );

  24.         /* Loop until an SDL_QUIT event is found */
  25.         while( !quit ){

  26.             /* Poll for events */
  27.             while( SDL_PollEvent( &event ) ){
  28.                
  29.                 switch( event.type ){
  30.                     /* Keyboard event */
  31.                     /* Pass the event data onto PrintKeyInfo() */
  32.                     case SDL_KEYDOWN:
  33.                     case SDL_KEYUP:
  34.                         PrintKeyInfo( &event.key );
  35.                         break;

  36.                     /* SDL_QUIT event (window close) */
  37.                     case SDL_QUIT:
  38.                         quit = 1;
  39.                         break;

  40.                     default:
  41.                         break;
  42.                 }

  43.             }

  44.         }

  45.         /* Clean up */
  46.         SDL_Quit();
  47.         exit( 0 );
  48.     }

  49.     /* Print all information about a key event */
  50.     void PrintKeyInfo( SDL_KeyboardEvent *key ){
  51.         /* Is it a release or a press? */
  52.         if( key->type == SDL_KEYUP )
  53.             printf( "Release:- " );
  54.         else
  55.             printf( "Press:- " );

  56.         /* Print the hardware scancode first */
  57.         printf( "Scancode: 0x%02X", key->keysym.scancode );
  58.         /* Print the name of the key */
  59.         printf( ", Name: %s", SDL_GetKeyName( key->keysym.sym ) );
  60.         /* We want to print the unicode info, but we need to make */
  61.         /* sure its a press event first (remember, release events */
  62.         /* don't have unicode info                                */
  63.         if( key->type == SDL_KEYDOWN ){
  64.             /* If the Unicode value is less than 0x80 then the    */
  65.             /* unicode value can be used to get a printable       */
  66.             /* representation of the key, using (char)unicode.    */
  67.             printf(", Unicode: " );
  68.             if( key->keysym.unicode < 0x80 && key->keysym.unicode > 0 ){
  69.                 printf( "%c (0x%04X)", (char)key->keysym.unicode,
  70.                         key->keysym.unicode );
  71.             }
  72.             else{
  73.                 printf( "? (0x%04X)", key->keysym.unicode );
  74.             }
  75.         }
  76.         printf( "\n" );
  77.         /* Print modifier info */
  78.         PrintModifiers( key->keysym.mod );
  79.     }

  80.     /* Print modifier info */
  81.     void PrintModifiers( SDLMod mod ){
  82.         printf( "Modifers: " );

  83.         /* If there are none then say so and return */
  84.         if( mod == KMOD_NONE ){
  85.             printf( "None\n" );
  86.             return;
  87.         }

  88.         /* Check for the presence of each SDLMod value */
  89.         /* This looks messy, but there really isn't    */
  90.         /* a clearer way.                              */
  91.         if( mod & KMOD_NUM ) printf( "NUMLOCK " );
  92.         if( mod & KMOD_CAPS ) printf( "CAPSLOCK " );
  93.         if( mod & KMOD_LCTRL ) printf( "LCTRL " );
  94.         if( mod & KMOD_RCTRL ) printf( "RCTRL " );
  95.         if( mod & KMOD_RSHIFT ) printf( "RSHIFT " );
  96.         if( mod & KMOD_LSHIFT ) printf( "LSHIFT " );
  97.         if( mod & KMOD_RALT ) printf( "RALT " );
  98.         if( mod & KMOD_LALT ) printf( "LALT " );
  99.         if( mod & KMOD_CTRL ) printf( "CTRL " );
  100.         if( mod & KMOD_SHIFT ) printf( "SHIFT " );
  101.         if( mod & KMOD_ALT ) printf( "ALT " );
  102.         printf( "\n" );
  103.     }
复制代码
不想直接用的话可以读libsdl的源代码,看看是怎么实现的。

论坛徽章:
0
4 [报告]
发表于 2010-05-17 23:26 |只看该作者
可以考虑用信号量

论坛徽章:
0
5 [报告]
发表于 2010-05-24 16:09 |只看该作者
使用中断
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP