You could exploit the fact that the lower word of the Keys
enumeration encodes the keycode, which is designed to correspond to the ASCII character codes of the capital letters.
For example, Keys.A
has a value of 0x41 (decimal 65), which is (char)'A'
.
So basically, you could do:
var key = (Keys)e.KeyChar.ToUpper();
which will work for the umodified, non-accented latin letters. For the numbers, you would have to decide whether you would rather map back to the numpad keys, or the horizontally arranged ones. The latter are also mapped to their ASCII codes, for example Keys.D0
is 0x30, decimal 48, (char)'0'
.
You could also determine whether the keychar is lower case or upper case, and encode that into the shift bit of the key code modifier:
if(Char.IsUpper(e.KeyChar)) key |= Keys.Shift
Finally, use this answer based on this function to map the System.Windows.Forms.Keys
value to the System.Windows.Input.Key
you need:
var pressed_key = KeyInterop.KeyFromVirtualKey((int)key);
But this is no valid solution for the whole range of characters, let alone different alphabets, and I think what you really want to do is to subscribe to the KeyDown
event instead, which tells you the key codes right away.