SDL Guide 中文译版(三下)
2008-04-09 04:27:34来源:互联网 阅读 ()
第三章下 键盘输入
键盘相关数据结构
- 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: 0xX", 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 (0xX)", (char)key->keysym.unicode,
key->keysym.unicode );
}
else{
printf( "? (0xX)", 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" );
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:相当于delphi的日历控件
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
