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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在SDL2中发送键盘事件,您可以按照以下步骤进行操作: 1. 首先,您需要初始化SDL2并创建一个窗口和渲染器。这可以通过调用`SDL_Init()`、`SDL_CreateWindow()`和`SDL_CreateRenderer()`函数来完成。 2. 为了接收键盘事件,您需要创建一个事件循环。您可以使用`SDL_PollEvent()`函数在循环中获取事件。 3. 在事件循环中,您可以检查每个事件的类型,以确定它是一个键盘事件键盘事件的类型是`SDL_KEYDOWN`和`SDL_KEYUP`。 4. 如果事件是键盘按下事件(`SDL_KEYDOWN`),您可以检查事件的`key.keysym.sym`属性来获取按下的键的代码。这将是一个SDL_Keycode类型的值。 5. 然后,您可以根据按下的键执行相应的操作。例如,您可以在控制台上打印出按下的键: ```c if (event.type == SDL_KEYDOWN) { SDL_Keycode keycode = event.key.keysym.sym; printf("Key pressed: %s\n", SDL_GetKeyName(keycode)); } ``` 6. 如果事件是键盘释放事件(`SDL_KEYUP`),您可以采取相应的操作,例如停止移动角色或停止播放声音等。 7. 最后,在您完成使用SDL2后,记得调用`SDL_Quit()`函数来清理并退出程序。 这是一个简单的示例代码片段,演示了如何在SDL2中发送键盘事件: ```c #include <SDL2/SDL.h> #include <stdio.h> int main() { SDL_Init(SDL_INIT_VIDEO); SDL_Window* window = SDL_CreateWindow("SDL2 Keyboard Event", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_Event event; int quit = 0; while (!quit) { while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { quit = 1; } else if (event.type == SDL_KEYDOWN) { SDL_Keycode keycode = event.key.keysym.sym; printf("Key pressed: %s\n", SDL_GetKeyName(keycode)); } else if (event.type == SDL_KEYUP) { SDL_Keycode keycode = event.key.keysym.sym; printf("Key released: %s\n", SDL_GetKeyName(keycode)); } } // 渲染器的其他绘制逻辑... SDL_RenderPresent(renderer); } SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } ``` 希望这可以帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值