我想在freepascal下使用SDL从键盘上读取ascii键。我不关心shift,ctrl,caps等的压力 - 只是在典型应用程序中输入时通常会产生字符的东西。
问题是我似乎无法正确检测KMOD_CAPS。如果多次按下它,最终会得到相同CAPS状态的不同结果。
这是我到目前为止所做的:
program ReadKeySDL;
{SDL implementation of the crt readkey function.}
uses
sdl;
const
validkeys=[8,13,27,32..126];
lowercase=[97..122];
var
screen : pSDL_Surface;
event : pSDL_Event;
eventkey : tSDLKey;
eventmod : tSDLMod;
key : char;
keyselected : boolean;
BEGIN
SDL_Init(SDL_INIT_VIDEO);
screen:=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
if (screen=nil) then halt(1);
new(event);
repeat
keyselected:=false;
repeat
if (SDL_WaitEvent(event) = 1) then
begin
if (event^.type_ = SDL_KeyDown) then
begin
eventkey:=event^.key.keysym.sym;
eventmod:=event^.key.keysym.modifier;
writeln;
writeln('eventkey: ',eventkey);
writeln('eventmod: ',eventmod);
if (eventkey in validkeys) then
begin
keyselected:=true;
if ((eventkey in lowercase) and ((eventmod and (KMOD_CAPS or KMOD_SHIFT)) <> 0)) then
key:=chr(eventkey - 32)
else
key:=chr(eventkey);
end;
end;
end;
until((keyselected) and (event^.type_ = SDL_KeyUp));
writeln('Your key is: ',key,' (',ord(key),')');
writeln('Final eventkey: ',eventkey);
writeln('Final eventmod: ',eventmod);
until(ord(key) = 27);
dispose(event);
SDL_FreeSurface(screen);
SDL_Quit;
END.