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
}
You should have 2 tables of booleans for keys. One table, in which you set keys true or false based on the SDL keydown/keyup events, and another one, that you initialize with false. When checking keyPressed, you just compare the second table key with the first table key, and if different, if second table key is false, then it was pressed, else it was released. After that, you do secondTable[key] := not secondTable[key]. Works!
I had this problem in LuaJIT with FFI, this is how I solved it:
Global:
Event code:
Update function:
Most time i wasted on this:
Because FFI is returning a CData object, which was used as the array-key, but it needs the integer.
I know you have already selected an answer.. but here is some actual code of how I typically do it with one array. :)
first define this somewhere.
Then later, create a keyboard() function which will register keyboard input
Then when you actually want to use the keyboard input i.e. a handleInput() function, it may look something like this:
And of course you can easily do what you're wanting to do
**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
http://kennycason.com/posts/2009-09-20-sdl-simple-space-shooter-game-demo-part-i.html (if you have any problems, let me know)