Converting Virtual Keys to Scan Codes (for DirectInput)

Started by
12 comments, last by izzo 19 years, 3 months ago
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Advertisement
Wait, I'm still kind of confused... [Sorry]
How would I use the ToAsciiEx() function to convert scan codes to virtual keys?
For example, how would I take VK_UP and get DIK_UP?
Argh! This hurts my head...
To go further, here is the code that defines the key names (it was in Game Maker Language, so ignore sytax)
//Defining Key Names//NOTE://string[256] c;//BYTE a;//FUNCTION string string(x) { returns x as a string }//FUNCTION string chr(x) { returns the character of Ascii value x }c[0]="?Unknown?"for (a=1; a<=255; a+=1;) c[a] = "<"+string(a)+">?";//Long listc[8]="Backspace";c[13]="Enter";c[16]="Shift";c[17]="Control";c[18]="Alt";c[19]="Pause/Break?";c[20]="CAPS Lock";c[27]="Escape";c[32]="Space";c[33]="Page Up";c[34]="Page Down";c[35]="End Key";c[36]="Home Key";c[37]="Left";c[38]="Up";c[39]="Right";c[40]="Down";c[45]="Insert";c[46]="Delete";for (b=48; b<=57; b+=1;) c="Number "+chr(b); //Number 0-9for (d=65; d<=90; d+=1;) c[d]=chr(d); //A-Zfor (e=96; e<=105; e+=1;) c[e]="Numpad "+string(e-96) //Numpad 0-9c[106]="Numpad Asterik '*'"c[107]="Numpad Plus '+'"c[109]="Numpad Dash '-'"c[110]="Numpad Period '.'"c[111]="Numpad Slash '/'"for (f=112; f<=123; f+=1;) c[f]="F"+string(f-111) //F1-11

About the '& 0x80', in other tutorials, it returns the highest bit in the byte.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Quote:Original post by deadimp
Well, I found the tutorial here that describes it, but I'm running into one problem: This function doesn't work with the arrow keys (I haven't tested many other keys though). I know it works with the letters, and the shift key, but when I use VK_UP or VK_DOWN, it always returns false. Here's the function:
BYTE Keys[256]; //Keys is a buffer for a LPDIRECTINPUTDEVICE8bool keyboard_check(UINT key) { return (Keys[ MapVirtualKeyEx(key,0,GetKeyboardLayout(0)) ] & 0x80);}

I'm not sure what I did wrong. (Yes, I am looping)


I have just written my input library and I came across the same problem. It so happens that certain keys, (VK_ UP,DOWN,LEFT,RIGHT,KeyPad Enter) do not translate correctly with any MapVirtualKey - here was my simple fix that I will eventuallly try and make better, but it works for now:


// Convert to ascii
BYTE ASCII = MapVirtualKeyEx( m_keyboardbuffer [ dwCurbuffer ].dwOfs, 1, m_layout );
if( ASCII == 0 )
{
int temp = m_keyboardbuffer [ dwCurbuffer ].dwOfs;
if( temp == DIK_LEFT )
ASCII = INPUT_LEFT;
else if( temp == DIK_RIGHT )
ASCII = INPUT_RIGHT;
else if( temp == DIK_UP )
ASCII = INPUT_UP;
else if( temp == DIK_DOWN
ASCII = INPUT_DOWN;
...
}
...


Basically I am checking if the key I convert from DI is 0, than see if it is something that does not translate, such as those DIK_ arrow keys. The INPUT_ keys are my own defines that translate to VK_'s.

I hope this helps some.
Quote:Original post by deadimp
Well, I found the tutorial here that describes it, but I'm running into one problem: This function doesn't work with the arrow keys (I haven't tested many other keys though). I know it works with the letters, and the shift key, but when I use VK_UP or VK_DOWN, it always returns false. Here's the function:
BYTE Keys[256]; //Keys is a buffer for a LPDIRECTINPUTDEVICE8bool keyboard_check(UINT key) { return (Keys[ MapVirtualKeyEx(key,0,GetKeyboardLayout(0)) ] & 0x80);}

I'm not sure what I did wrong. (Yes, I am looping)


I have just written my input library and I came across the same problem. It so happens that certain keys, (VK_ UP,DOWN,LEFT,RIGHT,KeyPad Enter) do not translate correctly with any MapVirtualKey - here was my simple fix that I will eventuallly try and make better, but it works for now:

// Convert to ascii   BYTE ASCII = MapVirtualKeyEx( m_keyboardbuffer [ dwCurbuffer ].dwOfs, 1, m_layout );   if( ASCII == 0 )   {      int temp = m_keyboardbuffer [ dwCurbuffer ].dwOfs;      if( temp == DIK_LEFT )      ASCII = INPUT_LEFT;      else if( temp == DIK_RIGHT )            ASCII = INPUT_RIGHT;      else if( temp == DIK_UP )	    ASCII = INPUT_UP;      else if( temp == DIK_DOWN                   ASCII = INPUT_DOWN;       ...   }   ...


Basically I am checking if the key I convert from DI is 0, than see if it is something that does not translate, such as those DIK_ arrow keys. The INPUT_ keys are my own defines that translate to VK_'s.

I hope this helps some.

- Drew
You don't even really need to do that. Just use the DIK constants. The snippet in that article is only useful if you're using DirectInput and you want the user to be able to type strings in your game (e.g. type in their name). The DIK constants don't map easily to ASCII characters so you'd either need a massive switch statement or you'd use the function that the article presents. But for general keyboard use (i.e. movement, firing, etc) you can just use the DIK constants.

cheers
sam.

This topic is closed to new replies.

Advertisement