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

Started by
15 comments, last by choffstein 18 years, 4 months ago
Quote:Original post by rip-off
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

*** Source Snippet Removed ***


Well... Rip-off, that works for me. Using a variable... And SDL_GetKeyState(...). I actually thought of doing something like this so in my example I tried SDL_GetKeyState(NULL)[SDLK_LEFT] and it worked. But when I put it into a class (the example) it doesn't work. It's interesting no one else actually replied here lol.

*The problem continues...*
Advertisement
okay i solved it.

the "aKey" argument of GetKey needs to be an int.

how i missed this...

i noticed it eventually.

SDLK_LEFT is a high value ( bigger than a Uint8) it gets truncated in assignment. so you were getting the value of some other key.

enjoy!
Quote:Original post by rip-off
okay i solved it.

the "aKey" argument of GetKey needs to be an int.

how i missed this...

i noticed it eventually.

SDLK_LEFT is a high value ( bigger than a Uint8) it gets truncated in assignment. so you were getting the value of some other key.

enjoy!


WOW - THANKS A LOT. A LOT.

Really, thanks. I was using Uint8 because.. well, since the array is delclared as a Uint8 I thought that all the keys might be Uint8's. Now that I think about it, it's not that way. The result of x[j] might be a Uint8, but j might not be.

rip_off->rating->setByInt(rip_off->rating->get() + 1);


Hmmm.. I think (yup, 'another user with the same ip') I rated you up already before. Aww well lol.
Calling SDL_GetKeyState every tick is quite a lot of work. A polling system will help you out a lot.

It would end up being something like this:
SDL_Event event;while( SDL_PollEvent( &event ) ) {    switch( event.type )    {        case SDL_KEYDOWN:            KeyDown(&event.key.keysym); // Call the KeyDown event.            break;        case SDL_KEYUP:            KeyUp(&event.key.keysym); // Call the KeyUp event.            break;        case SDL_QUIT:            QuitApp(); // quit the app            break;    }}

Forgive me if that's wrong, I haven't writen C++ in a while (turned to C#). In your KeyUp and KeyDown methods, you could update an array depending on which key was pressed or "un-pressed". Good luck!
Rob Loach [Website] [Projects] [Contact]
Quote:Original post by Rob Loach
Calling SDL_GetKeyState every tick is quite a lot of work. A polling system will help you out a lot.

It would end up being something like this:
SDL_Event event;while( SDL_PollEvent( &event ) ) {    switch( event.type )    {        case SDL_KEYDOWN:            KeyDown(&event.key.keysym); // Call the KeyDown event.            break;        case SDL_KEYUP:            KeyUp(&event.key.keysym); // Call the KeyUp event.            break;        case SDL_QUIT:            QuitApp(); // quit the app            break;    }}

Forgive me if that's wrong, I haven't writen C++ in a while (turned to C#). In your KeyUp and KeyDown methods, you could update an array depending on which key was pressed or "un-pressed". Good luck!


Good idea. Maybe a bool array would be nice. Does anyone know how many keys there are on a keyboard? I don't want to count them...

Quote:Original post by agi_shi
Quote:Original post by Rob Loach
Calling SDL_GetKeyState every tick is quite a lot of work. A polling system will help you out a lot.

It would end up being something like this:
SDL_Event event;while( SDL_PollEvent( &event ) ) {    switch( event.type )    {        case SDL_KEYDOWN:            KeyDown(&event.key.keysym); // Call the KeyDown event.            break;        case SDL_KEYUP:            KeyUp(&event.key.keysym); // Call the KeyUp event.            break;        case SDL_QUIT:            QuitApp(); // quit the app            break;    }}

Forgive me if that's wrong, I haven't writen C++ in a while (turned to C#). In your KeyUp and KeyDown methods, you could update an array depending on which key was pressed or "un-pressed". Good luck!


Good idea. Maybe a bool array would be nice. Does anyone know how many keys there are on a keyboard? I don't want to count them...


okay. if you do it that way you are just duplicating SDL_GetKeyState.

the way i chose to do is to wrap input into a class.

the only keys you store are the ones relavent ( breakout might have 3, left, right and quit ).

in an update() oer equivelant function you just check wether those keys have been pressed or released, and update your bool array. then provide you won enum like so

enum Keys{
KEY_LEFT,
KEY_RIGHT,
KEY_QUIT,
};

the actual keys these are mapped to can get stored in a file, allowing you to make your program independant of the keys you use while programming
Quote:Original post by agi_shi
Good idea. Maybe a bool array would be nice. Does anyone know how many keys there are on a keyboard? I don't want to count them...


bool keyMap[SDLK_LAST];

This topic is closed to new replies.

Advertisement