|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| string to virtual key |
|
![]() Exustio Member since: 1/18/2009 From: Gothenburg, Sweden |
||||
|
|
||||
| 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 Member since: 2/10/2008 From: Emmaus, PA, United States |
||||
|
|
||||
| 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 } |
||||
|
||||
![]() nobodynews Member since: 12/30/2000 From: Fort Lauderdale, FL, United States |
||||
|
|
||||
| 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 }; } |
||||
|
||||
![]() Zahlman Moderator - Game Programming Member since: 1/9/2004 From: Toronto, Canada |
||||
|
|
||||
Quote: 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 Member since: 1/18/2009 From: Gothenburg, Sweden |
||||
|
|
||||
| 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. |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|