SDL输入(按键) [英] Inputs in SDL (on key pressed)

查看:1258
本文介绍了SDL输入(按键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在SDL的while循环中检测到按键或按键的释放。现在,我知道你可以用OnKeyPressed,OnKeyReleased,OnKeyHit等SDL获得事件,但是我想知道如何构建返回一个布尔值的KeyPressed函数,而不是一个事件。例如:

I would like to know how can I detect the press of a key or release of a key in a while loop in SDL. Now, I know you can get the events with SDL like OnKeyPressed, OnKeyReleased, OnKeyHit, etc, but I want to know how to build functions like 'KeyPressed' that returns a boolean, instead of being an event. Example:

while not KeyHit( KEY_ESC ) 
{
//Code here
}


推荐答案

我知道你已经选择了一个答案这里是一些实际的代码,我通常如何使用一个数组。 :

I know you have already selected an answer.. but here is some actual code of how I typically do it with one array. :)

首先将其定义在某处。

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 which 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()函数时,可能看起来像这样:

Then when you actually want to use the keyboard input i.e. a handleInput() function, it may look something like this:

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;
    }
}

当然,你可以轻松地做你所做的想要做

And of course you can easily do what you're wanting to do

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

**我网站上的更新版本:**
对于为了不发布大量的源代码,您可以查看支持

**Updated version on my website: ** For the sake of not posting a lot of source code, you can view a complete SDL Keyboard class in C++ that supports


  1. 单键输入
  2. 的完整的SDL Keyboard类>
  3. 同时按键组合(按任何顺序按键)

  4. 顺序键组合(按特定顺序按键)

  1. Single Key Input
  2. Simultaneous Key Combos (Keys all pressed in any order)
  3. Sequential Key Combonations (Keys all pressed in specific order)

http://kennycason.com/posts/2009-09-20-sdl-simple-space-shooter-game-demo-part-i.html (如果你有任何问题,让我知道)

http://kennycason.com/posts/2009-09-20-sdl-simple-space-shooter-game-demo-part-i.html (if you have any problems, let me know)

这篇关于SDL输入(按键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆