Forum > General

[SOLVED]SDL2 keyboard problem

(1/1)

mrguzgog:
I have this little function to check if a key is pressed using SDL:


--- Code: ---    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;

--- End code ---

If I call it using:

--- Code: ---if keyPressed(SDLK_Escape) = true then...

--- End code ---

it works fine, but if I call with...

--- Code: ---if keyPressed(SDLK_UP) = true then...

--- End code ---

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?

mrguzgog:
Oops, forgot to mark this as solved, I re-wrote the keyboard routine as follows and it works fine:


--- Code: ---    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;


--- End code ---

Navigation

[0] Message Index

Go to full version