Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a "Keys" enum value to an "int" character in C#?

This seems like something that should be easy, but I am having a tough time figuring out what needs to happen here.

In the "KeyDown" eventhandler, if the "e.KeyValue" is a number, I want to treat it as a number and store it as an int. So, if I hit "8" on the number pad, I don't want "Numpad8" I want the int value 8 that I can add or subtract or whatever.

So, how do I convert from the KeyValue to an int?

like image 836
Andy Stampor Avatar asked Apr 20 '09 18:04

Andy Stampor


4 Answers

I'd go with this solution:

int value = -1;
if (e.KeyValue >= ((int) Keys.NumPad0) && e.KeyValue <= ((int) Keys.NumPad9)) { // numpad
    value = e.KeyValue - ((int) Keys.NumPad0);
} else if (e.KeyValue >= ((int) Keys.D0) && e.KeyValue <= ((int) Keys.D9)) { // regular numbers
    value = e.KeyValue - ((int) Keys.D0);
}

...if the point was to get the numeric value of the label of they key that was punched in.

like image 118
Tommi Forsström Avatar answered Nov 19 '22 06:11

Tommi Forsström


Something like this should work well: (Edited)

int keyVal = (int)e.KeyValue;
int value = -1;
if ((keyVal >= (int)Keys.D0 && keyVal <= (int)Keys.D9)
{
    value = (int)e.KeyValue - (int)Keys.D0;
}
else if (keyVal >= (int)Keys.NumPad0 && keyVal <= (int)Keys.NumPad9)
{
    value = (int)e.KeyValue - (int)Keys.NumPad0;
}
like image 39
John Fisher Avatar answered Nov 19 '22 07:11

John Fisher


Facts: Keyboard has keys. Some keys represent numbers and some do not.

Problem (rephrased): Yield a numeric value represented by a key, if the key represents a number.

To solve the problem it is necessary to know which keys (out of set of all keys) represent numbers as well as exact numeric value each (number) key represents.

To my knowledge there is no an easy way to get such a mapping from the framework.

Note: The fact D0-D9 and NumPad0-NamPad9 are sequential in the Keys enum is accidental and relying on these values being ordered sequentially is unfounded.

So solution would be:

  1. Determine if given key represents a number.
  2. Return numeric value of the key if key represents a number.

private static readonly IDictionary<Keys, int> NumericKeys = 
    new Dictionary<Keys, int> {
        { Keys.D0, 0 },
        { Keys.D1, 1 },
        { Keys.D2, 2 },
        { Keys.D3, 3 },
        { Keys.D4, 4 },
        { Keys.D5, 5 },
        { Keys.D6, 6 },
        { Keys.D7, 7 },
        { Keys.D8, 8 },
        { Keys.D9, 9 },
        { Keys.NumPad0, 0 },
        { Keys.NumPad1, 1 },
        { Keys.NumPad2, 2 },
        { Keys.NumPad3, 3 },
        { Keys.NumPad4, 4 },
        { Keys.NumPad5, 5 },
        { Keys.NumPad6, 6 },
        { Keys.NumPad7, 7 },
        { Keys.NumPad8, 8 },
        { Keys.NumPad9, 9 }
   };

private int? GetKeyNumericValue(KeyEventArgs e) {
    if (NumericKeys.ContainsKey(e.KeyCode)) return NumericKeys[e.KeyCode];
    else return null;
}

Arguably, not the simplest solution, but the one that models the solution closely.

like image 5
THX-1138 Avatar answered Nov 19 '22 08:11

THX-1138


Depending on how many keys you are keeping track of, the simplest method would be for you to create a select case (or switch statement in C# I guess?) that would check the value of your keydown and depending upon that value assign a value to an integer that is appropriate.

like image 2
TheTXI Avatar answered Nov 19 '22 08:11

TheTXI