bind DIKEYS "Quake Style"

Started by
0 comments, last by nes8bit 24 years ago
Does anyone know of a simple/fast way of being able to bind a command to a key using Direct Input? I already have a simple scripting language(if that''s what you want to call it) already implemented. The reason that I ask is because I thought parsing strings in a fps in realtime would be a bad idea. If you can think of a better idea, then i''d like to read about it.
Advertisement

You can do the parsing on startup, or when the user binds a new command. There is no need to continously parse strings in real time.

The simplest way to have commands bound to keys would be a keep an array of functions pointers which are intialized to null. When a function is bound, simply parse the string entered to see if its a bindable command, and parse the key entered to see if its a valid key. Then in your input event handler function, check to see if you have a nonzero pointer for the key pressed in the array and call it.

Here''s some psuedocode. I''m just typing it up so don''t take to a compiler =)

typedef void (*PCOMMAND)();
PCOMMANDS fnCommands[TOTAL_KEYS];

void MoveForward()
{ ........ }

void BindCommand(const char * string)
{
int key;
char * command;

//parse string for command and key here

if(key < 0 // key > TOTAL_KEYS)
error bad key index, return

if command == "+forward"
fnCommands[key] = &MoveForward
}

void HandleKeyEvent(const KeyEvent &key)
{
if(!fnCommands[key.id])
return;
if(key.state == BUTTONDOWN)
(fnCommands)();
}

This topic is closed to new replies.

Advertisement