SDL Direct Input
Okay instead of using events, or polling for key events I discovered a method of directly getting input from the keyboard using SDL. This works much like DInput, a simple array is filled, a 1 indicating the key pressed.
However the guide describing this technique only showed a few snippets, not a full model. It warns that the events still must be Polled. I'm not sure as to how this would look.
Anyone care to elaborate on this SDL Input technique?
This is all it shows:
Uint8* kbarray;
//grab the keyboard state
kbarray=SDL_GetKeyState(NULL);
if(kbarray[SDLK_a]==1)
{
//the A key is down
}
What I know about that method is that you must use SDL_PumpEvents (it was a function like that) to make SDL set the state of the keys, well that function is described well on the SDL wiki, http://www.libsdl.org
Happy Ho-Ho. :) I wrote a little mouse/keyboard handling function in SDL to store all my active keys as an array (in my case, bools). Here's what worked for me:
This system also tosses the mouse buttons into the mix- left mouse button would be bKeyDown[1], right mouse button bKeyDown[3]. I found some functions needed to be activated when the mouse button changed state- going down or up (dragging something across the screen, for example). That needed to be tossed in with the applicable case. Everything else can just check for their bool condition later on in the program.
[EDIT: Just re-read your question, and realized I hadn't had enough coffee this morning yet. I read "keys" and "SDL" and hit the reply key. Sorry.]
The best example I found using SDL_GetKeyState() is in SuperPig's Enginuity part IV article:
[Edited by - Tok on December 3, 2005 4:13:33 PM]
bool bKeyDown[512];void HandleInput(){ SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_MOUSEBUTTONDOWN: bKeyDown[event.button.button] = true; break; case SDL_MOUSEBUTTONUP: bKeyDown[event.button.button] = false; break; case SDL_KEYDOWN: bKeyDown[event.key.keysym.sym] = true; break; case SDL_KEYUP: bKeyDown[event.key.keysym.sym] = false; break; case SDL_QUIT: GameState = gsEnd; break; default: break; } }}
This system also tosses the mouse buttons into the mix- left mouse button would be bKeyDown[1], right mouse button bKeyDown[3]. I found some functions needed to be activated when the mouse button changed state- going down or up (dragging something across the screen, for example). That needed to be tossed in with the applicable case. Everything else can just check for their bool condition later on in the program.
[EDIT: Just re-read your question, and realized I hadn't had enough coffee this morning yet. I read "keys" and "SDL" and hit the reply key. Sorry.]
The best example I found using SDL_GetKeyState() is in SuperPig's Enginuity part IV article:
void CInputTask::Update(){ SDL_PumpEvents(); keys=SDL_GetKeyState(&keyCount);}
I'm not sure if his example functions the same as mine for checking the keys later- for a simple "is the left mouse button in "fire" mode?" check for me isif(bKeyDown[1]) { // fire!
I'll keep an eye on this thread- lemme know if that helps of if any other questions come to mind. Cheers![Edited by - Tok on December 3, 2005 4:13:33 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement