How to convert the value of the "Keys" enumeration to the "int" character in C #?

It sounds like it should be easy, but it's hard for me to determine what should happen here.

In the KeyDown event handler, if "e.KeyValue" is a number, I want to treat it as a number and store it as an int. So, if I press “8” on the numeric keypad, I don't want “Numpad8”, I want the value 8, which I can add or subtract, or something else.

So how do I convert from KeyValue to int?

+7
c # winforms keydown
source share
11 answers

I would 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 were to get the numerical value of the label of the key that was punched.

+15
source share

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; } 
+5
source share

Facts: The keyboard has keys. Some keys represent numbers, and some do not.

Problem (rephrased): Consider the numerical value represented by the key if the key represents a number.

To solve the problem, you need to know which keys (from the set of all keys) represent numbers, as well as the exact numerical value that each (number) represents.

As far as I know, there is no easy way to get such a mapping from the framework.

Note: the fact that D0-D9 and NumPad0-NamPad9 are sequential in the enumeration of keys are random and rely on the fact that these values, ordered sequentially, are unreasonable.

Such a solution would be:

  • Determine if a given key matches a number.
  • Returns the numeric value of a key if the 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; } 

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

+5
source share

Depending on how many keys you are tracking, the easiest method would be to create a selection case (or a switch statement in C #, I think?) That will check the value of your keydown and assign a value to an integer, which is appropriate.

+2
source share

If you need NUMERIC values, you will need to create a switch(e.KeyCode) and evaluate the key and create your own int. There is no easy way to turn Keys into a numeric value that represents the number on the key. Closest to you may be the equivalent of ASCII, which will still need to be translated.

+2
source share

Could you just listen to the KeyPress event? This will give the character that was actually pressed.

+1
source share

This function will do what you want:

 private int GetKeyValue(int keyValue) { if (keyValue >= 48 && keyValue <= 57) { return keyValue - 48; } else if (keyValue >= 96 && keyValue <= 105) { return keyValue - 96; } else { return -1; // Not a number... do whatever... } } 

Call it like this:

 private void Form1_KeyDown(object sender, KeyEventArgs e) { int num = GetKeyValue(e.KeyValue); } 
+1
source share

KeyValue is the character code for pressing a key. To get its numerical value, you have to find the character to press the key, then parse it from character to integer.

Something like Convert.ToInt32 (e.KeyCode.ToString ())

0
source share

Or create an extension method and name it as

 private void Form1_KeyDown(object sender, KeyEventArgs e) { int num = e.KeyValue.GetKeyValue(); } 

where is the extension method

 public static class MyExtensions { public static int GetKeyValue(this int keyValue) { if (keyValue >= 48 && keyValue <= 57) { return keyValue - 48; } else if (keyValue >= 96 && keyValue <= 105) { return keyValue - 96; } else { return -1; // Not a number... do whatever... } } } } 
0
source share

I used this simple two-line code:

 string key = e.KeyCode.ToString(); if (key.Length > 1) key = key.Replace("NumPad", "").Replace("D", ""); 
0
source share

int i = (int) e.KeyValue;

-3
source share

All Articles