Just to add to the Keystate problem here is the code I use that works under Delphi and FreePascal (in {$MODE DELPHI} )

[pascal]
var keyState: PKeyStateArr;
begin
keyState := PKeyStateArr(SDL_GetKeyState(nil));
if(keyState[SDLK_ESC]=SDL_PRESSED) then
begin
Exit;
end;
[/pascal]

PKeyStateArr is declared in sdl.pas.