Jump to content
  • Advertisement
Sign in to follow this  

string to virtual key

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any easy way to convert a string to a virtual key code? So that a string containing "VK_ESCAPE" is translated to the value of VK_ESCAPE. The reason I want to do this is because I am working on a program where I want the user to be able to set up all controls in an ini-file, which is loaded automaticly when the program is started.

Share this post


Link to post
Share on other sites
Advertisement
You'll need to have the actual strings available in your app somewhere. For a very simplistic approach, you can, for instance, declare an array of strings (or char*'s) and search for a match. If you don't want or need all available keys, use two lists: a string lookup list and a value list.

Make sure the keycode strings match in number and order the key values.


char *keycodes[] = {"VK_ESCAPE","VK_SPACE","VK_HOME",...};
int keyvalues[] = { 0x1b, 0x20, 0x24, ...};
// ... search for a match in the input string
int numKeycodes = sizeof(keycodes)/sizeof(char*);
int i=0;
int iniValue = -1;
while( i < numKeycodes ) {
if( iniString==keycodes ) {
iniValue = keyvalues;
break;
}
i++;
}
if( iniValue != -1 ) {
// do something with the matched value
}


Share this post


Link to post
Share on other sites
What he said, except you might be interested in C-preprocessor stringification. This would let you convert actual tokens to C-style strings which might make your code more uniform. Just a thought.

edit: in fact, by combining stringification with struct initialization syntax you might be able to simplify things a bit.
#include "TokenList.h"

struct TokenMatch
{
unsigned int value;
char * token
};

TOKEN_TO_PAIR(TOKEN) {TOKEN, #TOKEN}

int main()
{
TokenMatch pairs = {TOKEN_TO_PAIR(VK_ESCAPE),
TOKEN_TO_PAIR(VK_RETURN),
TOKEN_TO_PAIR(VK_LEFT),
// etc
};
}
It's untested so I probably screwed the syntax up at some point.

Share this post


Link to post
Share on other sites
Quote:
Original post by Exustio
Is there any easy way to convert a string to a virtual key code?
So that a string containing "VK_ESCAPE" is translated to the value of VK_ESCAPE.

The reason I want to do this is because I am working on a program where I want the user to be able to set up all controls in an ini-file, which is loaded automaticly when the program is started.


Why not use your own mapping, instead of expecting the user to know what "VK_ESCAPE" means? Besides, you don't want the user to be able to map J to D; you want the user to be able to map "turn left" to D.

Share this post


Link to post
Share on other sites
Yeah, that is probably what I will have to do, although I was hoping that there was some built in functions to load keys from an ini-file, would really save me some time.

Well well, can't expect everything to be easy, thanks for the help guys.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!