Skip to main content
GameDev.net gamedev.net
🔒 Locked

Scan code to key name?

Started by SpaceDude May 1, 2004 at 9:07 PM 5 replies 1.7k views
Original Post
SpaceDude
SpaceDude
I want to add the option for players to remap their keys in my game. So basically, the user presses a key, i get the scan code for it and then i want the name of that key to appear on the screen. I already know how to convert scan codes into text, but thats not exactly what i want. If the user presses the space bar for example, i want "SPACE" to appear on screen not " ". So essentially i need the opposite of this: #define DIK_ESCAPE 0x01 #define DIK_1 0x02 #define DIK_2 0x03 #define DIK_3 0x04 #define DIK_4 0x05 #define DIK_5 0x06 etc... i.e.
void GetKeyName(BYTE ScanCode, char szKeyName[])
{
  switch (ScanCode)
  {
    case 0x01: strcpy(szKeyName, "Escape"); break;
    case 0x02: strcpy(szKeyName, "1"); break;
    case 0x03: strcpy(szKeyName, "2"); break;
  }
}
 
sure, i could define them all manually... but there are 256 keys, thats a bit of a drag. There must be some kind of function to do this, right? [edited by - SpaceDude on May 1, 2004 10:09:55 PM]
Mastaba
Mastaba
DirectInput device interfaces have a method for this kind of thing, but I do not believe Win32 itself has a function that does this.

[edited by - mastaba on May 1, 2004 10:48:13 PM]
Endurion
Endurion
GetKeyNameText

DInput tells you the keys name too, if you enumerate devices.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
SpaceDude
SpaceDude
I am in fact using DirectInput, so if you could tell me the name of the function i would be very gratefull. I can''t find it in the MSDN, I looked at the EnumDevices function for IDirectInput8 Interface but it doesn''t give the key names.

In the mean time i''ll try use GetKeyNameText, but that seems to be for use with windows messages... I hope it works.
SpaceDude
SpaceDude
ok, GetKeyNameText() seems to work for most keys, but not all. It doesn''t work for:

[, ], insert, home, pgup, pgdn, end, delete, arrow keys and some of the keypad keys.

char szKeyName[32];
GetKeyNameText(ScanCode<<16, szKeyName, 32);

Any ideas as to why its not working?
SpaceDude
SpaceDude
Alright i''ve fixed the problem... seems to be some differences between the scan codes used by window messages and direct input. Everthing seems fine except that Num Lock and Pause are inverted, but i can live with that. Here is the function i now use if anybody has the same problem:


inline int ScanCodeToName(BYTE ScanCode, char szName[], int iMaxLength)
{
LONG lParam = ScanCode;
if (lParam & 0x80)
lParam += 0x80;
lParam = lParam << 16;
return GetKeyNameText(lParam, szName, iMaxLength);
}
Endurion
Endurion
Sorry, didn''t look here too long,

in your EnumDevicesProc do a EnumObjects on your keyboard device. This will enumerate all the keys.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.