• entries
    707
  • comments
    1173
  • views
    433290

Snippet #1

Sign in to follow this  
Programmer16

122 views

From now on, when I'm developing something and a I end up doing something that I think is useful, I'm going to post it. Most of the time I won't modify the code at all (like in the following snippet I didn't take out my engine specific code), so you may have to modify some of the code, but the base code should be entirely transferable. When I get a chance, I might add a list so that you guys can go directly to a snippet from the main page.

Snippet #1 handles converting a key into a character. It takes a simple key id, and returns the corresponding character (lowercase or uppercase; including numbers and other characters like :, ", <, etc.)
(NOTE: The IK_ IDs directly relate to DIK_ IDs. Some of the IK_ IDs are spelled differently than the DIK_ IDs, so I am posting the IK_ enumeration for reference.)

enum InputKeys
{
IK_0 = DIK_0,
IK_1 = DIK_1,
IK_2 = DIK_2,
IK_3 = DIK_3,
IK_4 = DIK_4,
IK_5 = DIK_5,
IK_6 = DIK_6,
IK_7 = DIK_7,
IK_8 = DIK_8,
IK_9 = DIK_9,
IK_A = DIK_A,
IK_B = DIK_B,
IK_C = DIK_C,
IK_D = DIK_D,
IK_E = DIK_E,
IK_F = DIK_F,
IK_G = DIK_G,
IK_H = DIK_H,
IK_I = DIK_I,
IK_J = DIK_J,
IK_K = DIK_K,
IK_L = DIK_L,
IK_M = DIK_M,
IK_N = DIK_N,
IK_O = DIK_O,
IK_P = DIK_P,
IK_Q = DIK_Q,
IK_R = DIK_R,
IK_S = DIK_S,
IK_T = DIK_T,
IK_U = DIK_U,
IK_V = DIK_V,
IK_W = DIK_W,
IK_X = DIK_X,
IK_Y = DIK_Y,
IK_Z = DIK_Z,
IK_ADD = DIK_ADD,
IK_APOSTROPHE = DIK_APOSTROPHE,
IK_APPS = DIK_APPS,
IK_BACKSPACE = DIK_BACK,
IK_BACKSLASH = DIK_BACKSLASH,
IK_CAPSLOCK = DIK_CAPITAL,
IK_COLON = DIK_COLON,
IK_COMMA = DIK_COMMA,
IK_DECIMAL = DIK_DECIMAL,
IK_DELETE = DIK_DELETE,
IK_DIVIDE = DIK_DIVIDE,
IK_EQUALS = DIK_EQUALS,
IK_ESCAPE = DIK_ESCAPE,
IK_F1 = DIK_F1,
IK_F2 = DIK_F2,
IK_F3 = DIK_F3,
IK_F4 = DIK_F4,
IK_F5 = DIK_F5,
IK_F6 = DIK_F6,
IK_F7 = DIK_F7,
IK_F8 = DIK_F8,
IK_F9 = DIK_F9,
IK_F10 = DIK_F10,
IK_F11 = DIK_F11,
IK_F12 = DIK_F12,
IK_LSHIFT = DIK_LSHIFT,
IK_RSHIFT = DIK_RSHIFT,
IK_LCONTROL = DIK_LCONTROL,
IK_RCONTROL = DIK_RCONTROL,
IK_LALT = DIK_LMENU,
IK_RALT = DIK_RMENU,
IK_LWIN = DIK_LWIN,
IK_RWIN = DIK_RWIN,
IK_ACCENT = DIK_GRAVE,
IK_END = DIK_END,
IK_HOME = DIK_HOME,
IK_INSERT = DIK_INSERT,
IK_LBRACKET = DIK_LBRACKET,
IK_RBRACKET = DIK_RBRACKET,
IK_LEFT = DIK_LEFT,
IK_UP = DIK_UP,
IK_RIGHT = DIK_RIGHT,
IK_DOWN = DIK_DOWN,
IK_MINUS = DIK_MINUS,
IK_MULTIPLY = DIK_MULTIPLY,
IK_MUTE = DIK_MUTE,
IK_NUMLOCK = DIK_NUMLOCK,
IK_NP0 = DIK_NUMPAD0,
IK_NP1 = DIK_NUMPAD1,
IK_NP2 = DIK_NUMPAD2,
IK_NP3 = DIK_NUMPAD3,
IK_NP4 = DIK_NUMPAD4,
IK_NP5 = DIK_NUMPAD5,
IK_NP6 = DIK_NUMPAD6,
IK_NP7 = DIK_NUMPAD7,
IK_NP8 = DIK_NUMPAD8,
IK_NP9 = DIK_NUMPAD9,
IK_NPCOMMA = DIK_NUMPADCOMMA,
IK_NPENTER = DIK_NUMPADENTER,
IK_NPEQUALS = DIK_NUMPADEQUALS,
IK_PAUSE = DIK_PAUSE,
IK_PERIOD = DIK_PERIOD,
IK_PGDOWN = DIK_NEXT,
IK_PGUP = DIK_PRIOR,
IK_ENTER = DIK_RETURN,
IK_SCROLL = DIK_SCROLL,
IK_SEMICOLON = DIK_SEMICOLON,
IK_SLASH = DIK_SLASH,
IK_SPACE = DIK_SPACE,
IK_SUBTRACT = DIK_SUBTRACT,
IK_TAB = DIK_TAB,
};





char KeyIdToChar(int nKeyID)
{
bool bCapital = false;
if(dftin::DirectInput::GetSingleton()->KeyDown(dftin::IK_LSHIFT) || dftin::DirectInput::GetSingleton()->KeyDown(dftin::IK_RSHIFT))
bCapital = true;
if(GetKeyState(VK_CAPITAL))
bCapital = true;

static char szKeyChars[256];
static bool bSet = false;
if(!bSet)
{
bSet = true;
ZeroMemory(&szKeyChars, sizeof(char[256]));
szKeyChars[IK_0] = '0';
szKeyChars[IK_1] = '1';
szKeyChars[IK_2] = '2';
szKeyChars[IK_3] = '3';
szKeyChars[IK_4] = '4';
szKeyChars[IK_5] = '5';
szKeyChars[IK_6] = '6';
szKeyChars[IK_7] = '7';
szKeyChars[IK_8] = '8';
szKeyChars[IK_9] = '9';

szKeyChars[IK_A] = 'A';
szKeyChars[IK_B] = 'B';
szKeyChars[IK_C] = 'C';
szKeyChars[IK_D] = 'D';
szKeyChars[IK_E] = 'E';
szKeyChars[IK_F] = 'F';
szKeyChars[IK_G] = 'G';
szKeyChars[IK_H] = 'H';
szKeyChars[IK_I] = 'I';
szKeyChars[IK_J] = 'J';
szKeyChars[IK_K] = 'K';
szKeyChars[IK_L] = 'L';
szKeyChars[IK_M] = 'M';
szKeyChars[IK_N] = 'N';
szKeyChars[IK_O] = 'O';
szKeyChars[IK_P] = 'P';
szKeyChars[IK_Q] = 'Q';
szKeyChars[IK_R] = 'R';
szKeyChars[IK_S] = 'S';
szKeyChars[IK_T] = 'T';
szKeyChars[IK_U] = 'U';
szKeyChars[IK_V] = 'V';
szKeyChars[IK_W] = 'W';
szKeyChars[IK_X] = 'X';
szKeyChars[IK_Y] = 'Y';
szKeyChars[IK_Z] = 'Z';

szKeyChars[IK_SPACE] = ' ';
szKeyChars[IK_ADD] = '+';
szKeyChars[IK_APOSTROPHE] = '\'';
szKeyChars[IK_BACKSLASH] = '\\';
szKeyChars[IK_COMMA] = ',';
szKeyChars[IK_DECIMAL] = '.';
szKeyChars[IK_DIVIDE] = '/';
szKeyChars[IK_EQUALS] = '=';
szKeyChars[IK_ACCENT] = '`';
szKeyChars[IK_LBRACKET] = '[';
szKeyChars[IK_RBRACKET] = ']';
szKeyChars[IK_MINUS] = '-';
szKeyChars[IK_MULTIPLY] = '*';
szKeyChars[IK_NP0] = '0';
szKeyChars[IK_NP1] = '1';
szKeyChars[IK_NP2] = '2';
szKeyChars[IK_NP3] = '3';
szKeyChars[IK_NP4] = '4';
szKeyChars[IK_NP5] = '5';
szKeyChars[IK_NP6] = '6';
szKeyChars[IK_NP7] = '7';
szKeyChars[IK_NP8] = '8';
szKeyChars[IK_NP9] = '9';
szKeyChars[IK_NPCOMMA] = ',';
szKeyChars[IK_NPENTER] = '\n';
szKeyChars[IK_NPEQUALS] = '=';
szKeyChars[IK_PERIOD] = '.';
szKeyChars[IK_ENTER] = '\n';
szKeyChars[IK_SEMICOLON] = ';';
szKeyChars[IK_SLASH] = '/';
szKeyChars[IK_SUBTRACT] = '-';
szKeyChars[IK_TAB] = '\t';
szKeyChars[IK_BACKSPACE] = '\b';
}

if(nKeyID == IK_A || nKeyID == IK_B || nKeyID == IK_C || nKeyID == IK_D || nKeyID == IK_E || nKeyID == IK_F || nKeyID == IK_G || nKeyID == IK_H || nKeyID == IK_I || nKeyID == IK_J || nKeyID == IK_K || nKeyID == IK_L || nKeyID == IK_M || nKeyID == IK_N || nKeyID == IK_O || nKeyID == IK_P || nKeyID == IK_Q || nKeyID == IK_R || nKeyID == IK_S || nKeyID == IK_T || nKeyID == IK_U || nKeyID == IK_V || nKeyID == IK_W || nKeyID == IK_X || nKeyID == IK_Y || nKeyID == IK_Z)
{
if(bCapital)
return szKeyChars[nKeyID];
else
return szKeyChars[nKeyID] | 32;
}
else
{
if(bCapital)
{
switch(nKeyID)
{
case IK_0:
return ')';

case IK_1:
return '!';

case IK_2:
return '@';

case IK_3:
return '#';

case IK_4:
return '$';

case IK_5:
return '%';

case IK_6:
return '^';

case IK_7:
return '&';

case IK_8:
return '*';

case IK_9:
return '(';

case IK_SEMICOLON:
return ':';

case IK_LBRACKET:
return '{';

case IK_RBRACKET:
return '}';

case IK_APOSTROPHE:
return '\"';

case IK_COMMA:
return '<';

case IK_PERIOD:
return '>';

case IK_SLASH:
return '?';

case IK_MINUS:
return '_';

case IK_EQUALS:
return '+';

case IK_BACKSLASH:
return '|';

case IK_BACKSPACE:
return '\b';

case IK_SPACE:
return ' ';

default:
return szKeyChars[nKeyID];
}
}
else
return szKeyChars[nKeyID];
}
return '\0';
}






I've fully tested this function and I don't believe that I'm missing anything. So, if you notice anything missing, please say so!
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now