#define KEYDOWN(name, key) (name[key] & 0x80)
...
char KEY[256] = "DIK_F10";
if ( KEYDOWN(buffer, KEY[256] ) )
{
...
}
Of course this doesn't work, but it illustrates the idea.
Thanks in advance
String to DIK
Started by adriano_usp, Mar 31 2007 07:01 AM
6 replies to this topic
#1 Members - Reputation: 396
Posted 31 March 2007 - 07:01 AM
Hi,
How would you convert an array of char to a DirectInput keyboard constant? Something like that:
Sponsor:
#2 Members - Reputation: 1307
Posted 31 March 2007 - 07:26 AM
The easiest thing to do would be to store the key value as well as the string:
Then test the value, and if you need its name, it's available in the same structure.
If the strings can change dynamically (why? telling us what you're actually trying to achieve may get better solutions), the most straightforward way will be to store a list of all of the DIK_ string combinations and which values they equate to (perhaps in an array of structures like the one above), then do a simple string search to find which matches the string.
For setting up the above, you might find the stringizing operator useful: http://msdn2.microsoft.com/en-US/library/7e3a913x.aspx
struct MYKEY
{
unsigned int value;
char name[256];
}
Then test the value, and if you need its name, it's available in the same structure.
If the strings can change dynamically (why? telling us what you're actually trying to achieve may get better solutions), the most straightforward way will be to store a list of all of the DIK_ string combinations and which values they equate to (perhaps in an array of structures like the one above), then do a simple string search to find which matches the string.
For setting up the above, you might find the stringizing operator useful: http://msdn2.microsoft.com/en-US/library/7e3a913x.aspx
#4 Members - Reputation: 1284
Posted 01 April 2007 - 05:33 AM
Quote:
Original post by circlesoft
This should do what you are looking for:
UINT virtualKey = VkKeyScan( character );
DWORD scanCode = MapVirtualKey( virtualKey, 0 );
These Win32 APIs are slightly obscure but work fine with the DInput scan codes.
He wasn't looking for the DIK of a character, but of a full string, basically the same identifier that DirectInput uses, but as a string.
A quick'n'easy way to set this up would be to use a std::map<std::string, DWORD>, and then just look up a string in the map. If found, then return the corresponding value. It might not be the most efficient method, but it works. (All this is assuming C++.)
#include <map>
#include <string>
#include <DInput.h>
//...
std::map<std::string, DWORD> KeyNameMap;
//...
//Initialize the map to contain all the data (tedious)
KeyNameMap["DIK_F10"] = DIK_F10;
KeyNameMap["DIK_ESCAPE"] = DIK_ESCAPE;
KeyNameMap["DIK_MINUS"] = DIK_MINUS;
// and so forth...
bool KeyDown(const BYTE* buffer, const std::string& keyName)
{
std::map<std::string, DWORD>::const_iterator key = KeyNameMap.find(keyName);
if (key != KeyNameMap.end())
{
return buffer[key->second] & 0x80;
}
else
{
return false;
}
}
//KeyDown can take a char array, char pointer, or std::string.
// Anything that can be implicitly converted into a temporary
// std::string, basically.
But as S1CA suggested, it does raise suspicion as to why you want to do this. There are very likely better designs for doing whatever it is you're trying to do.
#5 Moderators - Reputation: 1340
Posted 01 April 2007 - 07:05 AM
Quote:
Original post by Agony
A quick'n'easy way to set this up would be to use a std::map<std::string, DWORD>, and then just look up a string in the map. If found, then return the corresponding value. It might not be the most efficient method, but it works. (All this is assuming C++.)
A hashmap would work efficiently. Although a hashmap isn't part of the standard, SGI's STL and STLPort each provide one. Alternatively, one can be rolled by wrapping std::map, possibly with the help of boost::hash.
#6 Members - Reputation: 1018
Posted 01 April 2007 - 07:17 AM
Quote:
Original post by Muhammad Haggag
Alternatively, one can be rolled by wrapping std::map, possibly with the help of boost::hash.
This is already available in boost::unordered_map. You need to get it from the Boost Vault, though (under Containers/unordered.tar.gz). It's probably also part of their beta tr1 release.
jfl.
#7 Members - Reputation: 396
Posted 01 April 2007 - 04:32 PM
Wow... good replies. Thank you much.
OK, Let me explain why I would like to "convert" a string to a DIK...
My friend asked me to develop a simple application that executes an action when an specific key is pressed. The chosen key could be changed and its description (its name) would be read from a text file, edited by him.
So I sent him a list of all DIK constants, so that he writes the key name (in the text file) equals to the DIK name.
If I found a fast way to convert that key name to a DIK, I wouldn't need to write an structure to reference all the keys for this application. However, it was faster than I imagined [smile]. If someone has interested in the structure, here is it:
Cheers.
OK, Let me explain why I would like to "convert" a string to a DIK...
My friend asked me to develop a simple application that executes an action when an specific key is pressed. The chosen key could be changed and its description (its name) would be read from a text file, edited by him.
So I sent him a list of all DIK constants, so that he writes the key name (in the text file) equals to the DIK name.
If I found a fast way to convert that key name to a DIK, I wouldn't need to write an structure to reference all the keys for this application. However, it was faster than I imagined [smile]. If someone has interested in the structure, here is it:
struct STRUC_KEY
{
UCHAR keyByte;
char keyName[30];
};
STRUC_KEY MyKey[] =
{
DIK_0, "DIK_0",
DIK_1, "DIK_1",
DIK_2, "DIK_2",
DIK_3, "DIK_3",
DIK_4, "DIK_4",
DIK_5, "DIK_5",
DIK_6, "DIK_6",
DIK_7, "DIK_7",
DIK_8, "DIK_8",
DIK_9, "DIK_9",
DIK_A, "DIK_A",
DIK_ABNT_C1, "DIK_ABNT_C1",
DIK_ABNT_C2, "DIK_ABNT_C2",
DIK_ADD, "DIK_ADD",
DIK_APOSTROPHE, "DIK_APOSTROPHE",
DIK_APPS, "DIK_APPS",
DIK_AT, "DIK_AT",
DIK_AX, "DIK_AX",
DIK_B, "DIK_B",
DIK_BACK, "DIK_BACK",
DIK_BACKSLASH, "DIK_BACKSLASH",
DIK_C, "DIK_C",
DIK_CALCULATOR, "DIK_CALCULATOR",
DIK_CAPITAL, "DIK_CAPITAL",
DIK_COLON, "DIK_COLON",
DIK_COMMA, "DIK_COMMA",
DIK_CONVERT, "DIK_CONVERT",
DIK_D, "DIK_D",
DIK_DECIMAL, "DIK_DECIMAL",
DIK_DELETE, "DIK_DELETE",
DIK_DIVIDE, "DIK_DIVIDE",
DIK_DOWN, "DIK_DOWN",
DIK_E, "DIK_E",
DIK_END, "DIK_END",
DIK_EQUALS, "DIK_EQUALS",
DIK_ESCAPE, "DIK_ESCAPE",
DIK_F, "DIK_F",
DIK_F1, "DIK_F1",
DIK_F2, "DIK_F2",
DIK_F3, "DIK_F3",
DIK_F4, "DIK_F4",
DIK_F5, "DIK_F5",
DIK_F6, "DIK_F6",
DIK_F7, "DIK_F7",
DIK_F8, "DIK_F8",
DIK_F9, "DIK_F9",
DIK_F10, "DIK_F10",
DIK_F11, "DIK_F11",
DIK_F12, "DIK_F12",
DIK_F13, "DIK_F13",
DIK_F14, "DIK_F14",
DIK_F15, "DIK_F15",
DIK_G, "DIK_G",
DIK_GRAVE, "DIK_GRAVE",
DIK_H, "DIK_H",
DIK_HOME, "DIK_HOME",
DIK_I, "DIK_I",
DIK_INSERT, "DIK_INSERT",
DIK_J, "DIK_J",
DIK_K, "DIK_K",
DIK_KANA, "DIK_KANA",
DIK_KANJI, "DIK_KANJI",
DIK_L, "DIK_L",
DIK_LBRACKET, "DIK_LBRACKET",
DIK_LCONTROL, "DIK_LCONTROL",
DIK_LEFT, "DIK_LEFT",
DIK_LMENU, "DIK_LMENU",
DIK_LSHIFT, "DIK_LSHIFT",
DIK_LWIN, "DIK_LWIN",
DIK_M, "DIK_M",
DIK_MAIL, "DIK_MAIL",
DIK_MEDIASELECT, "DIK_MEDIASELECT",
DIK_MEDIASTOP, "DIK_MEDIASTOP",
DIK_MINUS, "DIK_MINUS",
DIK_MULTIPLY, "DIK_MULTIPLY",
DIK_MUTE, "DIK_MUTE",
DIK_MYCOMPUTER, "DIK_MYCOMPUTER",
DIK_N, "DIK_N",
DIK_NEXT, "DIK_NEXT",
DIK_NEXTTRACK, "DIK_NEXTTRACK",
DIK_NOCONVERT, "DIK_NOCONVERT",
DIK_NUMLOCK, "DIK_NUMLOCK",
DIK_NUMPAD0, "DIK_NUMPAD0",
DIK_NUMPAD1, "DIK_NUMPAD1",
DIK_NUMPAD2, "DIK_NUMPAD2",
DIK_NUMPAD3, "DIK_NUMPAD3",
DIK_NUMPAD4, "DIK_NUMPAD4",
DIK_NUMPAD5, "DIK_NUMPAD5",
DIK_NUMPAD6, "DIK_NUMPAD6",
DIK_NUMPAD7, "DIK_NUMPAD7",
DIK_NUMPAD8, "DIK_NUMPAD8",
DIK_NUMPAD9, "DIK_NUMPAD9",
DIK_NUMPADCOMMA, "DIK_NUMPADCOMMA",
DIK_NUMPADENTER, "DIK_NUMPADENTER",
DIK_NUMPADEQUALS, "DIK_NUMPADEQUALS",
DIK_O, "DIK_O",
DIK_OEM_102, "DIK_OEM_102",
DIK_P, "DIK_P",
DIK_PAUSE, "DIK_PAUSE",
DIK_PERIOD, "DIK_PERIOD",
DIK_PLAYPAUSE, "DIK_PLAYPAUSE",
DIK_POWER, "DIK_POWER",
DIK_PREVTRACK, "DIK_PREVTRACK",
DIK_PRIOR, "DIK_PRIOR",
DIK_Q, "DIK_Q",
DIK_R, "DIK_R",
DIK_RBRACKET, "DIK_RBRACKET",
DIK_RCONTROL, "DIK_RCONTROL",
DIK_RETURN, "DIK_RETURN",
DIK_RIGHT, "DIK_RIGHT",
DIK_RMENU, "DIK_RMENU",
DIK_RSHIFT, "DIK_RSHIFT",
DIK_RWIN, "DIK_RWIN",
DIK_S, "DIK_S",
DIK_SCROLL, "DIK_SCROLL",
DIK_SEMICOLON, "DIK_SEMICOLON",
DIK_SLASH, "DIK_SLASH",
DIK_SLEEP, "DIK_SLEEP",
DIK_SPACE, "DIK_SPACE",
DIK_STOP, "DIK_STOP",
DIK_SUBTRACT, "DIK_SUBTRACT",
DIK_SYSRQ, "DIK_SYSRQ",
DIK_T, "DIK_T",
DIK_TAB, "DIK_TAB",
DIK_U, "DIK_U",
DIK_UNDERLINE, "DIK_UNDERLINE",
DIK_UNLABELED, "DIK_UNLABELED",
DIK_UP, "DIK_UP",
DIK_V, "DIK_V",
DIK_VOLUMEDOWN, "DIK_VOLUMEDOWN",
DIK_VOLUMEUP, "DIK_VOLUMEUP",
DIK_W, "DIK_W",
DIK_WAKE, "DIK_WAKE",
DIK_WEBBACK, "DIK_WEBBACK",
DIK_WEBFAVORITES, "DIK_WEBFAVORITES",
DIK_WEBFORWARD, "DIK_WEBFORWARD",
DIK_WEBHOME, "DIK_WEBHOME",
DIK_WEBREFRESH, "DIK_WEBREFRESH",
DIK_WEBSEARCH, "DIK_WEBSEARCH",
DIK_WEBSTOP, "DIK_WEBSTOP",
DIK_X, "DIK_X",
DIK_Y, "DIK_Y",
DIK_YEN, "DIK_YEN",
DIK_Z, "DIK_Z",
DIK_LALT, "DIK_LALT",
DIK_RALT, "DIK_RALT",
};
Cheers.






