Key binding in C++

Started by
2 comments, last by com 21 years, 10 months ago
Im trying to make a small game and the only thing the book i have hasnt helped me with is key binding. Im programmming in C++ so can anyone help me please!
Advertisement
You could try using an std::map with the character from the keyboard as the key, and a pointer to a function as the value.

For example: (psuedocode)


  typedef std::map< unsigned char, (void *)funcPtr > BindMapBindMap keyMapping;keyMapping[''A''] = MoveLeft();keyMapping[''S''] = MoveDown();keyMapping[''D''] = MoveRight();keyMapping[''W''] = MoveUp();  


That won''t work as-is, it would require a bit of tinkering with it, but it should get you where you need to be. There may be an easier way, but that''s just off the top of my head.
thank you very mutch
Also, you''d probably want to be able to load the bindings from a config file, like in Quake or Half-Life, in which case you could just do something like:


  char c;while( c = GetKeyBindingFromConfig() ){KeyMapping[c] = GetFunctionBindingFromConfig();}  

This topic is closed to new replies.

Advertisement