KeyCode, KeyValue and KeyData

A

Alberto

Could you tell me witch is the difference between keycode, keyvalue and
keydata in a keydown event?
keyvalue is always the ascii code?
thank you
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Yes , in is the one you probably use, KeyData return an enumeration (
Key )

cheers,
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Alberto,

There are KeyUp, KeyDown and KeyPress event. Only the KeyPress event
supplies the character code. I don't say ASCII because this code depends on
the current language settings; even one code can be generated as a result of
pressing a sequence of keys. Even in the simplest cases with English
language settings there is no 1:1 mapping between keys and character codes
e.g. key A produces ASCII for 'a' or key A produces ASCII for 'A' if
cpaslock is on or shift is pressed or 'a' of both capslock is on and shift
is pressed, etc. Even more there are keys that don't produce ASCII codes -
ctrl, alt, shift, F1, F2., Fx, windows keys, etc

As you can see keyboards cannot generate character codes they rather
generates special codes that gives 1:1 mapping between key and some code.
Windows internally uses special codes called VK_* (virtual) codes. They are
not related to any language and represent the keys themselves. Thus VK for
the key A is the same regardless whether shift is pressed or capslock is on.
In .NET the values of those keyboards key codes are the values of the Kyes
enumeration. Last time I checked they were the same as Windows VK codes.

Anyways all data that comes along with KeyUp and KeyDown are not ASCII
codes, but rather some form of *Keys* enum values or combination between
them. It just happens that the codes for letter keys match the ASCII codes.



One more thing related to key codes. There are two types of keys - regular
keys and modifier keys. Regular keys are all the keys that have some meaning
when pressed alone. Modifier keys are SHIFT, CTRL and ALT that normally is
pressed in combination with other keys to change their normal behavior. Keys
enumeration has two codes for modifier keys. One is to handle the key itself
(e.g Keys.ControlKey, KeysLCotnrolKey and Keys.RControlKey) and the other is
modifier code (e.g. Keys.Control). The modifier code is a bit value that can
be combined with regular key codes in order to produce a new code that
contains information about the key itself as well as all pressed modifier
keys at the moment of event generation.



KeyData - a value that is bitwise OR combination between one Key enum value
and 0 or more *Keys* modifier values if any of the modifier keys has been
pressed at the moment of event generation. Since this value might be a
result of bitwise OR operation it might not have corresponding member in the
Keys enum.



KeyCode - *Keys* enum value that represents pressed or released key without
with all modifier keys information stripped out. This property always
contains value form the Keys enum and is the key part of the KeyData.



Modifiers - only the modifier part of the KeyData.



KeyValue - the same as KeyData, but converted to an integer value. It is
meant to be used probably when the value needs to be passed to a unmanaged
method via PInvoke.



And there are couple of Boolean properties for easy checking the modifier
keys.




HTH

Stoitcho Goutsev (100) [C# MVP]


There are KeyUp, KeyDown and KeyPress event. Only KeyPress event suplies the
the character code. I don't say ASCII code because for non english keyboards
setting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top