Huh? SDL - Weird (I mean it, WEIRD) stuff happens with keys...???

Started by
15 comments, last by choffstein 18 years, 4 months ago
Ok...... This is freaky... Anyways, SDL is kinda messing up or something... Look:

if (mainApp.GetKey(SDLK_RIGHT))
    player1.Setx(player1.Getx() + player1.GetSpeed());
CApp_Input::GetKey(Uint8 aKey) is this:

Uint8 CApp_Input::GetKey(Uint8 aKey)
{
    return (m_pKeysHeld[aKey]);
}

//^^^^  That is updated by:
void CApp_Input::UpdateKeys()
{
    m_pKeysHeld = SDL_GetKeyState(NULL);
}

I believe that should work... Guess what? It doesnt. HOWEVER - IF I change SDLK_LEFT to SDLK_f or whatever it works. HOWEVER - The above code works fine, if I just do SDL_GetKeyState(NULL)[SDLK_LEFT]... What can be wrong here? I had a problem with LALT and RALT before, too. It wasnt fixed either. I've also tried making GetKey(Uint8 ...) {...} to return SDL_GetKeyState(...)[aKey], but it doesnt work either... Any ideas? Thanks.
Advertisement
Well... Uhh... Anyone have any ideas?
Quote:Original post by agi_shi
Well... Uhh... Anyone have any ideas?


how often does UpdateKeys get called. are you positive it gets called (its not accidentally in some if clause )?

other than that we'll need some code...

EDIT: do you use SDL_PumpEvents for reasons described here
Quote:Original post by rip-off
Quote:Original post by agi_shi
Well... Uhh... Anyone have any ideas?


how often does UpdateKeys get called. are you positive it gets called (its not accidentally in some if clause )?

other than that we'll need some code...


Every frame. Ok, here:
bool Run(){    while (mainApp.UpdateEvent())    {        switch (mainApp.GetEvent().type)        {            case SDL_QUIT:                return false;            break;            case SDL_KEYDOWN:            {                switch(mainApp.GetEvent().key.keysym.sym)                {                    case SDLK_ESCAPE:                        return false;                    break;                }            }            break;        }    }    mainApp.UpdateKeys();    if (mainApp.GetKey(SDLK_LEFT))        player1.Setx(player1.Getx() - player1.GetSpeed());    if (mainApp.GetKey(SDLK_RIGHT))        player1.Setx(player1.Getx() + player1.GetSpeed());    return true;}

This is the 'so-called' core part of the problem. It's called every frame in this:
while (Run())    /*Draw my stuff...*/;


Here is the simple CApp_Input class (implementation, the declaration is simply the names of the funcs and the names of the variables...):
#include <SDL/SDL.h>#include "CApp_Input.h"CApp_Input::CApp_Input(){}CApp_Input::~CApp_Input(){}Uint8 CApp_Input::GetKey(Uint8 aKey){    return (m_pKeysHeld[aKey]);}void CApp_Input::UpdateKeys(){    m_pKeysHeld = SDL_GetKeyState(NULL);}


mainApp is a CApp. CApp inherits from CApp_Event and CApp_Input.

Like I said, returning the Uint8 doesnt work with SDLK_LEFT(or RIGHT). However, if I do it on the fly (SDL_GetKeyState(NULL)[SDLK_LEFT]) it works. And again, LEFT and RIGHT dont work and also F<whatever> and LALT and RALT. Before I never had these probs, maybe it's just me?
Quote:Original post by rip-off
Quote:Original post by agi_shi
Well... Uhh... Anyone have any ideas?


how often does UpdateKeys get called. are you positive it gets called (its not accidentally in some if clause )?

other than that we'll need some code...

EDIT: do you use SDL_PumpEvents for reasons described here


Nope, I dont. IIRC, SDL_PollEvent(...) calls it.

Anyways, I remember seeing that so I tried it, but it didnt work. Any ideas?

EDIT: Yup, I tried it again. Doesn't work.
Well... Anyone?
what can i say. should work. the only thing i can think of is to make the smallest example you can think of that demonstrates this behaviour, and post it so we can try it out.
Quote:Original post by rip-off
what can i say. should work. the only thing i can think of is to make the smallest example you can think of that demonstrates this behaviour, and post it so we can try it out.


Ok.. Sure.

I'll make an example right now, when I finish I'll post the source.

(It'll be one file though, I'm not going with perfect code right now...)
There we go:
#include <SDL/SDL.h>class input // no specific nice names right now, just code...{    public:        input();        ~input();        Uint8 GetKey(Uint8 aKey);        void UpdateKeys();    private:        Uint8 *m_pKeysHeld;};input::input(){}input::~input(){}Uint8 input::GetKey(Uint8 aKey){    return (m_pKeysHeld[aKey]);}void input::UpdateKeys(){    m_pKeysHeld = SDL_GetKeyState(NULL);}int main(int, char**){    if (SDL_Init(SDL_INIT_VIDEO) == -1)        return 1;    if (SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF) == NULL)        return 1;    input myIn;    SDL_Event theEvent;    short done = 0;    while (!done)    {        while (SDL_PollEvent(&theEvent))        {            switch (theEvent.type)            {                case SDL_QUIT:                    done = 1;                break;            }        }        myIn.UpdateKeys();        if (myIn.GetKey(SDLK_LEFT)) // Nope...  Ain't working            done = 1;        if (myIn.GetKey(SDLK_SPACE)) // Guess what happens?  It works            done = 1;        //Uncomment this and it works....  but comment that so it doesnt mess        //with it        //if (SDL_GetKeyState(NULL)[SDLK_LEFT])            //done = 1;    }    SDL_Quit();    return 0;}

Like I said inside, uncomment the direct one and see... Uncomment the other one and comment the direct one and see again...

Any ideas?
hmm. thats really strange. i'm going to take another look, because this works...

and as far as i can tell its doing the same thing

/*you may need to change the header...*/#include "SDL.h"int main( int argc, char **argv ){    Uint8 *keys;    SDL_Event event;    SDL_Surface *screen;    int done = 0;    SDL_Init(0);    screen = SDL_SetVideoMode( 400, 400, 32, 0 );    while(!done)    {        while( SDL_PollEvent( &event ) )        {            if( event.type == SDL_QUIT )                done = 1;        }        keys = SDL_GetKeyState(NULL);        if( keys[SDLK_RIGHT] )        {            SDL_WM_SetCaption( "right",NULL );        }        else        {            if( keys[SDLK_LEFT] )                SDL_WM_SetCaption( "Left",NULL );            else                SDL_WM_SetCaption( "NONE!",NULL );        }    }    SDL_Quit();    return 0;}

This topic is closed to new replies.

Advertisement