String To Virtual KeyCode

Started by
4 comments, last by Anon Mike 18 years, 6 months ago
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.
If you insist on saying "DUH", try to make a post that makes a little more sense
Advertisement
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.
XBox 360 gamertag: templewulf feel free to add me!
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.
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.
If you insist on saying "DUH", try to make a post that makes a little more sense
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.

Edit: Added code tags.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
Quote:Original post by cptrnet
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.

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.
-Mike

This topic is closed to new replies.

Advertisement