user input key store in array

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

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;
}

Advertisement

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).

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.

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

Thanks

Hopefully this will better explain what I mean smile.png

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);
    }
    ...

from https://github.com/osen/mutiny/blob/master/src/mutiny/Application.cpp

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.

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



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...

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.

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

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.

The most important about this approach is that you won't loose any analog key events so your character movements won't become discontinuous.

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?

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...

This topic is closed to new replies.

Advertisement