# String To Virtual KeyCode

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

## Recommended Posts

Hi, I need to know if there is a function in win32 that you pass in a string representation of a key on a keyboard and it gives you the value of the key such as 0x50 or whatever. What I need to do is players can save there user defined controls into a file. Can I just save to the file ofstream << VK_UP? and then read it ifstream >> player.Up or something? I was thinking of just saving the string "Up" and then reading it in ifstream << strUp, and then paddle.Up = StringToKeyCode(strUp)? I dont know, can someone point me in the right direction? Thanks.

##### Share on other sites
I don't know of a function that does it for you, but it shouldn't be hard to write. The VK_ macroes are actually integers, so I store mine as an integer attached to an action.

Look into std::map for a good way to structure an input-action association.

##### Share on other sites
I'm not entirely sure, but I don't think you can do that. You can, however do a lookup table. it may even be able to rip a list of #defines from somewhere and read it as a lookup table. Just instead of ifstream>>player.up, you'd have player.up = keyLookupNext() and keyLookupNext would lookup the key in the ripped #define list.

It wouldn't surprise me, though, if someone had already written somehting like this into a library somewhere.

##### Share on other sites
I don't know what you mean by a look up table, and rip a list of #defines, but I guess Im just going to have to write function.

##### Share on other sites
A lookup table is basically a table (think databases) with (in your case) two fields. One field will have the string, for example, "A" (for Shift+A), and the other field will have the integer, like 65. This means you look through your lookup table to find the entry with the string "A", and then check the corresponding integer entry, which is 65, which is the keycode. You represent this in a table:

String | Keycode-----------------  "A"  |    65  "B"  |    66

... etc. I hope that helps. Otherwise, there's always Google.

##### Share on other sites
Quote:
 Original post by cptrnetHi, I need to know if there is a function in win32 that you pass in a string representation of a key on a keyboard and it gives you the value of the key such as 0x50 or whatever.

You can use VkKeyScan. This is a rather iffy thing to do though because there is no guarantee and random character will map to a single key. Particularly outside the US.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999987
×