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

string to virtual key

Started by Exustio Nov 4, 2009 at 5:54 PM 3 replies 2.7k views
Original Post
Exustio
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.
Buckeye
Buckeye
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 stringint 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}

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
nobodynews
nobodynews
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.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
Zahlman
Zahlman
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.
Exustio
Exustio
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.

Topic Locked

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

Sign in to reply to this topic.