C SDL键盘事件SDL_KEYUP按下键时触发 [英] C SDL Keyboard Events SDL_KEYUP Triggering When Key Is Down

查看:129
本文介绍了C SDL键盘事件SDL_KEYUP按下键时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C语言中使用SDL,并且试图使某个字符在按下某个键时在屏幕上移动,并在释放该键时停止,但是似乎在按下该键时触发了KEYUP事件.仍然被压制.如果我删除了pressed()中的KEYUP部分,则字符将在屏幕上滑动,但显然不会自行停止.如果我离开KEYUP部分,则必须反复按该键才能在屏幕上移动它们.知道我在做什么错吗?

I am using SDL in C, and I'm trying to make a character move on the screen when a key is pressed, and stop when it is released, but it seems as if the KEYUP event is triggering when the key is still being held down. If I remove the KEYUP section in pressed(), the characters will slide across the screen, but obviously won't stop on their own. If I leave the KEYUP section in, I have to press the key repeatedly to move them across the screen. Any idea what I'm doing wrong?

这就是我所拥有的:

...

int done = 0;
while (done==0)
{
    pressed();
    if (kenter == 1)
    {
        state = 1;
        subscreen();
    }
    if (kescape == 1)
    {
        done = 1;
    }
    if (kup == 1)
    {
        playery += -2;
    }
    if (kdown == 1)
    {
        playery += 2;
    }

    if (kright == 1)
    {
        playerx += 2;
    }
    if (kleft == 1)
    {
        playerx += - 2;
    }

...

int pressed ()
{

SDL_Event keyevent;

while (SDL_PollEvent(&keyevent))
{
    switch(keyevent.type)
    {
        case SDL_KEYDOWN:
        switch(keyevent.key.keysym.sym)
        {
            case SDLK_RETURN:
            {
                kenter = 1;
                break;
            }
            case SDLK_ESCAPE:
            {
                kescape = 1;
                break;
            }
            case SDLK_a:
            {
                ka = 1;
                break;
            }
            case SDLK_s:
            {
                ks = 1;
                break;
            }
            case SDLK_UP:
            {
                kup = 1;
                break;
            }
            case SDLK_DOWN:
            {
                kdown = 1;
                break;
            }
            case SDLK_RIGHT:
            {
                kright = 1;
                break;
            }
            case SDLK_LEFT:
            {
                kleft = 1;
                break;
            }
            default:
            break;
        }
    }
    switch(keyevent.type)
    {
        case SDL_KEYUP:
        switch(keyevent.key.keysym.sym)
        {
            case SDLK_RETURN:
            {
                kenter = 0;
                break;
            }
            case SDLK_ESCAPE:
            {
                kescape = 0;
                break;
            }
            case SDLK_a:
            {
                ka = 0;
                break;
            }
            case SDLK_s:
            {
                ks = 0;
                break;
            }
            case SDLK_UP:
            {
                kup = 0;
                break;
            }
            case SDLK_DOWN:
            {
                kdown = 0;
                break;
            }
            case SDLK_RIGHT:
            {
                kright = 0;
                break;
            }
            case SDLK_LEFT:
            {
                kleft = 0;
                break;
            }
            default:
            break;
        }
    }
}
return 0;
}

推荐答案

我认为您的switch语句已损坏.

I think your switch statements are broken.

使用这种不太混乱的方法

Use this less confusing way

int pressed ()
{

    SDL_Event event;

    while(SDL_PollEvent(&event) )
    {
        if(event.type == SDLK_KEYDOWN)
        {
                switch(event.key.keysym.sym)
                {
                    case SDLK_RETURN:
                        doStuff = 1
                        break;
                    default:
                        break;
                }
        }
        if(event.type == SDLK_KEYUP)
        {
                switch(event.key.keysym.sym)
                {
                    case SDLK_RETURN:
                        doStuff = 0;
                        break;
                    default:
                        break;
                }
        }
    }   
}

也很重要:

SDL教程:实用键盘输入

哦,避免使用全局变量!

Oh and avoid using global variables!

这篇关于C SDL键盘事件SDL_KEYUP按下键时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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