Home » Community » Forums » General Programming » string to virtual key
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 string to virtual key
Post New Topic  Post Reply 
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.



 User Rating: 1036   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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[i] ) {
      iniValue = keyvalues[i];
      break;
   }
   i++;
}
if( iniValue != -1 ) {
   // do something with the matched value
}




 User Rating: 1419   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1567   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1990   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1036   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: