Skip to main content
GameDev.net gamedev.net
🔒 Locked

Key binding in C++

Started by com Jun 19, 2002 at 11:11 AM 2 replies 10k views
Original Post
com
com
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!
DoggettCK
DoggettCK
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 > BindMap

BindMap 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.
com
com
thank you very mutch
DoggettCK
DoggettCK
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();
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.