SDL inputs (by pressing a key)

I would like to know how I can detect a keystroke or key release in a while loop in the SDL. Now I know that you can receive events using the SDL, such as OnKeyPressed, OnKeyReleased, OnKeyHit, etc., but I want to know how to create functions like "KeyPressed" that returns a boolean, not an event. Example:

while not KeyHit( KEY_ESC ) 
{
//Code here
}
+5
source share
3 answers

I know that you have already chosen the answer .. but here is some actual code of how I usually do this with a single array. :)

first define it somewhere.

bool KEYS[322];  // 322 is the number of SDLK_DOWN events

for(int i = 0; i < 322; i++) { // init them all to false
   KEYS[i] = false;
}

SDL_EnableKeyRepeat(0,0); // you can configure this how you want, but it makes it nice for when you want to register a key continuously being held down

Then later create a keyboard () function that will register keyboard input

void keyboard() {
        // message processing loop
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            // check for messages
            switch (event.type) {
                // exit if the window is closed
            case SDL_QUIT:
                game_state = 0; // set game state to done,(do what you want here)
                break;
                // check for keypresses
            case SDL_KEYDOWN:
                KEYS[event.key.keysym.sym] = true;
                break;
            case SDL_KEYUP:
                KEYS[event.key.keysym.sym] = false;
                break;
            default:
                break;
            }
        } // end of message processing
}

, , handleInput(), :

void handleInput() {
    if(KEYS[SDLK_LEFT]) { // move left
        if(player->x - player->speed >= 0) {
            player->x -= player->speed;
        }
    }
    if(KEYS[SDLK_RIGHT]) { // move right
        if(player->x + player->speed <= screen->w) {
            player->x += player->speed;
        }
    }
    if(KEYS[SDLK_UP]) { // move up
        if(player->y - player->speed >= 0) {
            player->y -= player->speed;
        }
    }
    if(KEYS[SDLK_DOWN]) { // move down
        if(player->y + player->speed <= screen->h) {
            player->y += player->speed;
        }
    }
    if(KEYS[SDLK_s]) { // shoot 
        if(SDL_GetTicks() - player->lastShot > player->shotDelay) {
            shootbeam(player->beam);
        }
    }
    if(KEYS[SDLK_q]) {
        if(player->beam == PLAYER_BEAM_CHARGE) {
            player->beam = PLAYER_BEAM_NORMAL;
        } else {
            player->beam = PLAYER_BEAM_CHARGE;
        }
    }
    if(KEYS[SDLK_r]) {
        reset();
    }

    if(KEYS[SDLK_ESCAPE]) {
        gamestate = 0;
    }
}

, , ,

while(KEYS[SDLK_s]) {
    // do something
    keyboard(); // don't forget to redetect which keys are being pressed!
}

** -: ** , , SDL ++,

  • ( )
  • ( )

http://kennycason.com/posts/2009-09-20-sdl-simple-space-shooter-game-demo-part-i.html ( - , )

+11

2 . , true false SDK keydown/keyup, - false. keyPressed , , , , . secondTable [key]: = not secondTable [key]. !

0

LuaJIT FFI, :

Global:

KEYS = {}

:

ev = ffi.new("SDL_Event[1]")
function event()
    while sdl.SDL_PollEvent(ev) ~= 0 do
        local e = ev[0]
        local etype = e.type
        if etype == sdl.SDL_QUIT then
            return false -- quit
            -- os.exit() -- prevents interactive mode
        elseif etype == sdl.SDL_KEYDOWN then
            if e.key.keysym.sym == sdl.SDLK_ESCAPE then
                return false -- quit
                -- os.exit()
            end
            print("Pressed: ", e.key.keysym.scancode, "\n")
            KEYS[tonumber(e.key.keysym.sym)] = true
            -- print("Pressed: ", (e.key.keysym.sym == sdl.SDLK_w), "\n");
        elseif etype == sdl.SDL_KEYUP then
            KEYS[tonumber(e.key.keysym.sym)] = false
        elseif etype == sdl.SDL_VIDEORESIZE then
            -- print("video resize W:".. e.resize.w .. " H:" .. e.resize.h)
            width = e.resize.w
            height = e.resize.h
            onResize()
        end
    end
    return true -- everything ok
end

:

if KEYS[sdl.SDLK_w] == true then
    rot = rot + 1
end

:

KEYS[tonumber(e.key.keysym.sym)] = false

FFI CData, -, .

0

All Articles