Best Interface Between Sets of Defined Values

Started by
4 comments, last by Drew_Benton 19 years, 3 months ago
Ok guys here is what I'm trying to do: * In Windows - keys are defined with the VK_ as well as the ascii values * In DirectInput - keys are defined with the DIK_ definition Now both of these types are incompatible with each other - if you use DIK_ESCAPE in place of VK_ESCAPE - it won't work. I have two input plugin classes representing each API - both dervived from a base class. The only way I can think of to be able to use both sets of keys - the class used is unknown to the program b/c it's a plugin - is to make a key array in the base input class - then defined the keys in each plugin. However - this seems way too lengthy and kind of inefficient. An example:

// #define VK_ESCAPE         0x1B
// #define DIK_ESCAPE        0x01

class CInput
{
   int INPUT_ESCAPE;
}

class W32
{
   INPUT_ESCAPE = VK_ESCAPE;
}

class DI
{
   INPUT_ESCAPE = DIK_ESCAPE;
}
In program...

if( keydown( INPUT_ESCAPE ) )
   exit(-1);
Of course I won't write out every key like that - but it's to show what I would like to accompish. Would anyone have any ideas of how I could easily interface these two different sets of defined values without all that work? Thanks for your time! - Drew [Edited by - Drew_Benton on May 25, 2005 9:47:12 PM]
Advertisement
If I understand your question I hope this answer will help.

If you could define two array's with each set, so
an AscII array and an DirectInput array in a specified order
so that the AscII[1] array equals the DirectInput[1] array, you can write a Translation class to see whether or not the keys are equal.
namespace InputConstants
{
namespace Win32
{
const int Escape = VK_ESCAPE;
}

namespace DirectInput
{
const int Escape = DIK_ESCAPE;
}
}

Then in your code,
using namespace Keys = InputConstants::Win32;

if(key == Keys::Escape)
{
}
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
If your app needs to work with either plugin, then hard coding it to work with one or the other won't help much. What you need to do is define a standard set of key codes, and map from VK_/DIK_/etc to that. Both VK_ and DIK_ look like they're in the range of 0..255 (from a quick glance), so a simple lookup table should suffice. You could save yourself some time by chosing VK_ or DIK_ as your 'standard' key codes, but I'd recommend making your own codes up so you don't depend on their API.
These might be of some help:

// Convert a Windows VK code to a DirectInput Scan Codeint VKToDIK( const int iVK ){	return MapVirtualKeyEx( iVK, 0, GetKeyboardLayout(GetCurrentThreadId()) );}// Convert a DirectInput Scan Code to a Windows VK Codeint DIKToVK( const int iDIK ){	return MapVirtualKeyEx( iDIK, 1, GetKeyboardLayout(GetCurrentThreadId()) );}


Creating some sort of look-up table would probably be faster though. Note that using scan codes (like DirectInput) would be a better bet if you require portability as these codes are actual system codes for the keys that get sent from the keyboard to the OS. The VK codes are Windows specific...

James Bird
Thank you guys for all your replies! Now:

Starik1974 - Thanks for the suggestion about the two arrays - but I did not want to have to manually set all that info.

antareus - Thanks for that suggestion as well - but I would have to manually make two sets of 256 custom defines + any others if the user wants a different type of input class. Namespaces will not work well in my case.

hh10k - I agree and that is exactly what I'm trying to do, but I cannot figure out how to make them compatible per se.

birdjames - Thank you for that code! I messed around with it some and it is a 50% solution to my problem. When I use a DIK_ key, I do this:

// just an example// 0x1E = 30 (DIK_A)// dikey = 65int dikey = MapVirtualKeyEx( 0x1E, 1, GetKeyboardLayout(GetCurrentThreadId()) );


This is exactly what I need! As for the VK_ I can use it in a similar way. I took a look at MSDN to see the specifics of that function. Now that I can freely convert between the keys - DIK_A == 'A' == keys[65] - I just need to solve the other half of the problem - making a set of defines to use them right.

Here is an example:

Right now I have:

if( input->KeyDown( VK_ESCAPE ) ) for W32

in the game loop and
if( input->KeyDown( DIK_ESCAPE ) ) for DI


I would like to just have like:
if( input->KeyDown( ENGINE_ESCAPE ) )

so that no matter wheter they choose W32 or DI, it will be the correct key - in which I would not have to be dependent on the specifc keys. This is so they can change the input plugin at any time during development and it still works as is - they will not need to change anything.

Right now, the best thing I can think of goes along the lines of hh10k, Starik1974, and antareus posts. I know I will have to define my own keys - ENGINE_ESCAPE, etc.. but I', trying to difure out the best way. I can't define them in the header files b/c they are plugins and the header files are not used for the dervived classes, so I was thinking along the lines of maybe using an enum.

Thanks for all the input!

- Drew

[Edited by - Drew_Benton on May 25, 2005 9:45:33 PM]

This topic is closed to new replies.

Advertisement