Lazarus

Free Pascal => General => Topic started by: mrguzgog on August 15, 2016, 09:45:49 am

Title: [SOLVED]SDL2 keyboard problem
Post by: mrguzgog on August 15, 2016, 09:45:49 am
I have this little function to check if a key is pressed using SDL:

Code: [Select]
    function keyPressed(key: longint): boolean;
    begin
        while SDL_PollEvent(sdlEvent) = 1 do
        begin
            if sdlEvent^.type_= SDL_KEYDOWN then
            begin
                if sdlEvent^.key.keysym.sym = key then exit(true); 
            end;
        end;
        exit(false);
    end;

If I call it using:
Code: [Select]
if keyPressed(SDLK_Escape) = true then...

it works fine, but if I call with...
Code: [Select]
if keyPressed(SDLK_UP) = true then...

it doesn't work. As it compiles properly, SDLK_UP must be defined (somewhere!), maybe I have the wrong type but longint looks to be right.

Can someone tell me what I'm doing wrong please?

Title: Re: SDL2 keyboard problem
Post by: mrguzgog on August 20, 2016, 05:02:11 pm
Oops, forgot to mark this as solved, I re-wrote the keyboard routine as follows and it works fine:

Code: [Select]
    procedure updateKeys();
    var
    keyPressed: longint;
    begin

        while SDL_PollEvent(sdlEvent) = 1 do
        begin
            if sdlEvent^.type_= SDL_KEYDOWN then
            begin
                keyPressed := sdlEvent^.key.keysym.sym;
               
                case keyPressed of
                    SDLK_ESCAPE: ...
                    SDLK_UP: ...
                end;
               
            end;
        end;
    end;

TinyPortal © 2005-2018