Jump to content
  • Advertisement
Sign in to follow this  
Eldritch

Windows: Name of key codes

This topic is 2983 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 all! Is there any way to find out the name of a key? For example.. if I run a function on key 32 it would return "space" (or similar) and if I run it on the coed for "LALT" it would return "left alt" (or similar). Or do I have to create my own table for this, and localize it for each language (horrible work to do, but if there is no better way...)?

Share this post


Link to post
Share on other sites
Advertisement
I can't recall if I've ever used this method, but a brief search on the MSDN brought up this: GetKeyNameText


#include <iostream>
#include <string>
#include <windows.h>
#include <windowsx.h>
using namespace std;

wstring getKeyText(unsigned int vKey)
{
WCHAR buf[256];
unsigned int scanCode = MapVirtualKey(vKey, 0) << 16;
GetKeyNameText(scanCode, buf, 256);
return wstring(buf);
}

int main()
{
wcout << getKeyText(VK_RETURN) << endl;
}



[Edited by - CaspianB on April 13, 2010 8:21:44 AM]

Share this post


Link to post
Share on other sites
Super! Thank you :) Honestly, did not manage to come up with any good search query... my logic sucks today. Cheers!

Share this post


Link to post
Share on other sites
Just noticed that trying to get the name of VK_UP returns "NUM 8" and not "UP".. Any suggestion why this is happening?

Share this post


Link to post
Share on other sites
I'm not sure if there's a way to do it for mouse buttons...

I believe the oddities you are getting is due to the extended-key code bit which is bit 24 on the passed in scan code. If you look at the numpad, then "Num 8" Is the Up arrow.

If you modify the scanCode value like this to set the bit you will get the correct name when passing in VK_PRIOR or VK_UP... scanCode |= 0x01000000;

However, I do not think you always want that bit set. For example, if that bit is set and you pass in VK_RETURN you get "Num Enter" back instead of just "Enter" or if you pass in VK_F1 you get nothing back... So you'll probably need to add some code to check if it should be set or not.

It looks like there might be an example of this here...
http://www.ffuts.org/blog/mapvirtualkey-getkeynametext-and-a-story-of-how-to/

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!