-1

I have a 2D array containing all letters and their DirectInput key code:

string[,] DXKeyCodes = new string[,]
{
    {"a","0x1E"},
    {"b","0x30"},
    ...
};

Then I have a functions that returns from the array the hex key code based on the letter, if I send 'a' it returns '0x1E'.

This key code is then send as a keystroke to an external program by a function that require the key code to be specified as a short, but my array contains strings.

How can I convert this kind of string to a short ?

As an example, this is working but, of course, always sends the same key code :

Send_Key(0x24, 0x0008);

I need something like this to work so I can send any given key code:

Send_Key(keycode, 0x0008);

I have tried the following thing but it does not work either, just crashing my app.

Send_Key(Convert.ToInt16(keycode), 0x0008);

I really don't want to go to something like

if (keycode == "a")
{  
    Send_Key(0x1E, 0x0008);
}
else if (keycode == "b")
{  
    Send_Key(0x30, 0x0008);
}
...

I'm sure there is a much better way but I can't find it :(

Thanks for your help.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
  • 4
    The obvious question is: Why are you storing the hex values as strings instead of shorts in the first place? Another question would be: Why aren't you using a dictionary? – itsme86 Feb 16 '18 at 23:17
  • 2
    Sounds like a `Dictionary` – Jasen Feb 16 '18 at 23:18
  • oh... just because I didn't knew what a dictionary was and that I could have used that :) Sounds like it was a very stupid question with an easy fix... I'll try that right now... Thanks guys – MadeByVince Feb 16 '18 at 23:30
  • and, by the way, thanks for the -1... Always nice to see how people encourage newbies by throwing rocks to them, really appreciated – MadeByVince Feb 16 '18 at 23:32
  • 1
    Who brought rocks? Also don't complain about being down voted, please! Someone likely had a good reason. Reading your question for example was super easy in a couple places... – Austin T French Feb 16 '18 at 23:34
  • Super easy when you know coding... I never learned any programming language other than MS Basic in DOS many, many years ago ;) All I'm capable of doing is searching for different pieces of code I can modify and put together to get to the result I need and, you know what, I always get something to work at the end but, yes, indeed, sometimes I can have a question the seems very easy... A little bit of understanding and comprehension is then welcome. (guys, I guess I'll get other negative points for this post... how fast do you think I can drop to zero or negative reputation? Let's try) LOL – MadeByVince Feb 16 '18 at 23:47
  • 1
    Well, one way to avoid downvotes would be to learn the language. There are tons of free resources to teach you how to do this stuff. Instead, you ask very fundamental questions here and your expectation is that we're patient and understanding. What we understand is that you can't be bothered to take the time to read and instead take the easy way of poking at it and if you can't figure it out, you ask strangers. – itsme86 Feb 16 '18 at 23:56
  • Let's say that you have a point there... I'm coding maybe 3 things a year when I need a specific tailor made tool for my job. I do not practice enough to remember all this but, step by step I'm learning :) You guys are probably smarter and younger than I am too ;) – MadeByVince Feb 17 '18 at 00:02
  • I disagree with the others. Many years ago now, as a child, I started programming in a dialect of BASIC. I read the documentation cover to cover, knew every keyword and function off by heart, and made many nifty little programmes. But it's only with experience that you get the sense for which of several viable approaches is most appropriate for each problem. For years I did it all "wrong", even while it worked. Still more years later I TA'd physics students in C and C++. If you think something is "easy" try watching smart, motivated people with books, lectures and tutorials hit similar issues. – DeveloperInDevelopment Feb 17 '18 at 00:36

2 Answers2

2

As mentioned by itsme86 and Jasen in the question comments, you should use a Dictionary<string, short> instead of a 2D array. This way you can lookup the values by their key (rather than having to iterate over the array searching for the key when you want to find the corresponding value), and you don't have to do any conversions from strings. E.g.,

Dictionary<string, short> DXKeyCodes = new Dictionary<string,short>
{
  {"a", 0x1E},
  {"b", 0x30}
};
short theValue = DXKeyCodes["a"]; // don't need to loop over DXKeyCodes
                                  // don't need to convert from string

If for whatever reason you have to store these values as strings, then use the static method Convert.ToInt16(string, int):

short convertedValue = Convert.ToInt16("0x30", 16);

(In C#, short is an alias of System.Int16 and always has 16 bits.)

Joe Sewell
  • 6,067
  • 1
  • 21
  • 34
1

According to the DirectInput documentation, the API has a Key enumeration.

So, you can populate your dictionary like this:

var DXKeyCodes = new Dictionary<string,short>
{
   { "a", (short)Microsoft.DirectX.DirectInput.Key.A }, // enum value of A is 30 which == 0x1E
   { "b", (short)Microsoft.DirectX.DirectInput.Key.B }
};

Usage:

Send_Key(DXKeyCodes[keycode], 0x0008);
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108