# Key event

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

## Recommended Posts

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?

##### Share on other sites
What programming language are you using, and what API are you using for your input?

##### Share on other sites
I am using c++, directinput, but I would like a design that could be written in any language and api.

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by cptrnetIm 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]

##### Share on other sites
Thanks alot for the help, I think this will help my game alot.

##### Share on other sites
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.

##### Share on other sites
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.

1. 1
2. 2
JoeJ
20
3. 3
4. 4
frob
12
5. 5

• 13
• 18
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632194
• Total Posts
3004681

×