Public Group

user input key store in array

This topic is 2181 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi

any ideas on how to catch only one input keyboard key and store in a char array? If I try like below I get multiple, for ex. if L is pressed, L keys.

Thanks

--------

if (hasDownFlag == 1 ){
strcat (&ApEntrystr[airportcode], description.c_str());
hasDownFlag = 0;
}
//    if (hasUpFlag == 1 ){
//    airportcode += 1;
//    }
if(hasOptAltFlag == 1){
airportcode = 0;
ApEntrystr[0] = 0;
}

Share on other sites

Can you just iterate through the array and ensure that it isn't already in there?

Also, if your are using C++ (as opposed to C), you might find std::vector<char> to be much easier for this task. I tend to have two vectors, one for pressed keys this frame (cleared at the beginning of a new frame) and one for keys that are down (they get removed from the array when the up signal is obtained).

Edited by Karsten_

Share on other sites

I don't understand what you mean. You have some code to show?

Thanks

Share on other sites

Hopefully this will better explain what I mean

Input.h

class Input
{
...
static std::vector<int> keys;
static std::vector<int> upKeys;
static std::vector<int> downKeys;
...
}


Application.cpp

    ....
else if(event.type == SDL_KEYDOWN)
{
bool found = false;

for(int i = 0; i < Input::keys.size(); i++)
{
if(Input::keys.at(i) == event.key.keysym.sym)
{
found = true;
break;
}
}

if(found == true)
{
// go onto the next SDL_Event
continue;
}

Input::keys.push_back(event.key.keysym.sym);
Input::downKeys.push_back(event.key.keysym.sym);
}
else if(event.type == SDL_KEYUP)
{
for(int i = 0; i < Input::keys.size(); i++)
{
if(Input::keys.at(i) == event.key.keysym.sym)
{
Input::keys.erase(Input::keys.begin() + i);
i--;
}
}

Input::upKeys.push_back(event.key.keysym.sym);
}
...

Edited by Karsten_

Share on other sites

Thanks, but still no luck as I cannot use sdl (init_video).

Share on other sites

Thanks, but still no luck as I cannot use sdl (init_video).

Just substitute the SDL parts for whatever library you are using. The general idea of using vectors to hold the current key state should work with anything.

If glut, then do this on the glutKeyboard(Up) callbacks

if X11 (via glx), use XNextEvent and if(xev.type == KeyPress)

etc...

Edited by Karsten_

Share on other sites

I get back from x-plane a char: gChar with the pressed key. So while pressed it loops all the time hence resulting in a huge amount of returned chars.

Any help/ideas for that? I don't want to use glut. Thanks

Share on other sites

Why not just store them all? When you press key, it sends like ~30 events per second so, for example, std::vector<key>(1000) can hold it easily and iterate through all of the key presses very fast.

Edited by GuardianX

Share on other sites

I think its not a good idea to translate key events into an array just to one moment later iterate over the whole array to find which keys are pressed and possibly loosing data when a key gets pressed and released or gets pressed repeatedly before searching the array again. Sometimes its also useful to only react when a key gets pressed or only when a key is released and not the whole time its down.

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

Share on other sites

I need the last 4 user key inputs. Later on I want to compare this and show all airports beginning with the user input, first letter, second etc..

So I get all into a vector then delete all except the first 4 values? Well I try that then. But seems complicated it the user typed for ex. LL...

Edited by FGFS

• 19
• 22
• 23
• 16
• 11