Jump to content
  • Advertisement
Sign in to follow this  
cptrnet

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Edit: Added code tags.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!