我正在使用 SDL 在 C++ 中创建一个程序,当我按下一个键时,我需要显示一个消息框。我的代码创建一个窗口,当我按下一个键时可以捕获。这是已经拥有的:
#include <SDL/SDL.h>
#include <stdlib.h>
bool wait_for_events ()
{
SDL_WM_SetCaption("TESTE DE RATO E TECLAS", "TESTE DE RATO E TECLAS"); //titulo
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 0, 0); // criação da janela
SDL_Surface* temp = SDL_LoadBMP("sdl_logo.bmp"); // leitura do bitamp
SDL_Surface* bg = SDL_DisplayFormat(temp); // converter
SDL_FreeSurface(temp);
SDL_Event event;
int status;
char *key;
bool quit = false;
printf("Prima 'esc' ou pressione o botao 'x' para sair\n");
while ( !quit ) {
status = SDL_WaitEvent(&event);
if ( !status ) {
printf("SDL_WaitEvent error: %s\n", SDL_GetError());
return false;
}
switch (event.type) {
case SDL_KEYDOWN:
key = SDL_GetKeyName(event.key.keysym.sym);
printf("A Tecla '%s' Foi Pressionada\n", key );
if ( event.key.keysym.sym == SDLK_ESCAPE )
quit = true;
break;
case SDL_MOUSEBUTTONUP:
printf("Solto\n");
break;
case SDL_MOUSEBUTTONDOWN:
printf("Pressionado\n");
break;
case SDL_QUIT:
exit ( 1 );
break;
}
SDL_BlitSurface(bg, NULL, screen, NULL); // desenho do fundo
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
SDL_FreeSurface(bg);
SDL_Quit();
return true;
}
int main()
{
SDL_Surface *screen;
//iniciação do sistema de video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
fprintf(stderr, "Erro na inicializacao do SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
//tamanho da janela, resolução bits
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, "Erro na resolucao 640x480 video: %s\n", SDL_GetError());
exit(1);
}
wait_for_events();
return 0;
}