user input key store in array

Started by
10 comments, last by FGFS 10 years, 7 months ago

Why don't you just use the event and directly react to it?

Depending on the requirements, this could be ideal. However in many cases at this point in the program you don't know which entities / gameobjects / nodes need to react to the control so it is required to store a "snapshot" of keys which can be polled during the individual objects update / control events.

In the system I presented above, the last keys pressed this frame are stored in Input::downKeys.

The keys released during last frame are stored in Input::upKeys

and the current keys held down are stored in Input::keys

(This is mimicking the same API that Unity 3D provides (as is the nature of this project ;))).

So to get the last 4 keys pressed, just iterate through the last 4 keys in the Input::downKeys array.

This approach is pretty much platform agnostic so for x-plane, just substitute event.key.keysym.sym with gChar in whatever bit of code you get the key down event.

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.
Advertisement

Hmm if I do:

static std::vector<char> keys;
static std::vector<int> upKeys;
static std::vector<int> downKeys;

hasControlFlag = gFlags & xplm_ControlFlag;
hasDownFlag = gFlags & xplm_DownFlag;

if (hasOptAltFlag == 1)
{
keys.pop_back();
}

if (hasControlFlag == 1)
{
if (hasDownFlag == 1)
{
keys.push_back(gChar);
}
for (std::vector<char>::iterator it = keys.begin() ; it != keys.end(); ++it)
//for (std::vector<char>::iterator it = 0; it != 5; ++it)
{
std::copy(keys.begin(), keys.end(), ApEntrystr);
}

RenderText(font, 1, 1, 1, PanelLeft1 + 550, PanelTop1 - 320, test.displayAirport(ApEntrystr));
}

pusing ctrl and a key not only fills the vector but it displays also lots of rubbish and it crashes after a while.

Thanks again.

This topic is closed to new replies.

Advertisement