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.