Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Facts: Keyboard has keys. Some keys represent numbers and some do not.</p> <p>Problem (rephrased): Yield a numeric value represented by a key, if the key represents a number.</p> <p>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.</p> <p>To my knowledge there is no an easy way to get such a mapping from the framework.</p> <p>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.</p> <p>So solution would be:</p> <ol> <li>Determine if given key represents a number.</li> <li>Return numeric value of the key if key represents a number.</li> </ol> <hr> <pre><code>private static readonly IDictionary&lt;Keys, int&gt; NumericKeys = new Dictionary&lt;Keys, int&gt; { { 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; } </code></pre> <p>Arguably, not the simplest solution, but the one that models the solution closely.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload