Key event

Started by
10 comments, last by cptrnet 18 years, 9 months ago
Hello, I want to make my game better by having an event when a key is pressed, such as if I press the up key it will call a function that I assign that key like UpKeyPress(); I was wondering how could I do this? Is this even nessesary? I have the code to say the key has been pressed, I just don't want a lot of if statements in my game loop. Also I don't want the key press to keep saying its pressed, like on my tetris game is just moves the block clear to the side if i press the left or right key. I could just probably keep a variable saying if its been pressed dont call the event until its been released. So what do you guys think. Is this something I should spend time with?
If you insist on saying "DUH", try to make a post that makes a little more sense
Advertisement
What programming language are you using, and what API are you using for your input?
I am using c++, directinput, but I would like a design that could be written in any language and api.
If you insist on saying "DUH", try to make a post that makes a little more sense
One mechanism to create a fairly flexible binding between key events and actions is to use a mechanism that maps keysyms to functions or function objects. An example in C++ would be a std::map<KeySym, boost::function<void (void)> >. This eliminates the need for switch or if statements as well as making it possible to have customized controls.

For other languages you replace the map with whatever type the language has that supports associative arrays. For example, in Python you might use a dictionary.
Im not too knowledgeable with maps or even to begin, I know that you have a value and a key or something. So would I just Iterate through the map and if it came upon lets say DIK_UP it would call the function that I assign it. Should I just have a default function for all the keys or should I have an empty map and just add it as I go?
If you insist on saying "DUH", try to make a post that makes a little more sense
A map can just be indexed with the key type. For example:
std::map<KeySym, boost::function<void (void)> action_map;// fill in the action mapKeySym up = /* whatever the keysym for up is */;action_map[up]();

This assumes that even unasigned keys are assigned a null function. If you prefer not to fill in a function for all the keys, you can use action_map.find() to get an iterator, and if the iterator is valid then call the value of the result.
Quote:Original post by cptrnet
Im not too knowledgeable with maps or even to begin, I know that you have a value and a key or something. So would I just Iterate through the map and if it came upon lets say DIK_UP it would call the function that I assign it. Should I just have a default function for all the keys or should I have an empty map and just add it as I go?


The key is the map's index: e.g.

map[key] <-- functor element

(*map[key])() <-- executes the functor (if your functor is contained as a pointer)

The reason std::map is good is that you don't have to iterate through it to find your key... just do this:

if(mymap.find(key) == mymap.end()) return false;

That indicates the key wasn't found. Otherwise, it's there!

You don't have to add a functor for a key if you don't want to; if the key has no functor, it will just return false.

[EDIT] - OOps... SICrane is faster than me [smile]
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Thanks alot for the help, I think this will help my game alot.
If you insist on saying "DUH", try to make a post that makes a little more sense
Just make sure you search for your key first...

if you just do this:

map[key], and nothing is in the map at key... you'll add a null node! (bad)

make sure you always search:

if(mymap.find(key) == mymap.end()) return false; // or something to that effect.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
I am lost at declaring the map I tried to use boost::function<void (void)> but it says boost is not a class or namespace. Do I have to use a function pointer? I am just lost.
If you insist on saying "DUH", try to make a post that makes a little more sense

This topic is closed to new replies.

Advertisement