471,317 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

KeyCode, KeyValue and KeyData

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
Nov 28 '05 #1
2 33448
Hi,

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

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Alberto" <al*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 28 '05 #2
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
"Alberto" <al*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 28 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Z | last post: by
1 post views Thread by Perttu Pulkkinen | last post: by
6 posts views Thread by rich_poppleton | last post: by
4 posts views Thread by Thomas Christensen | last post: by
2 posts views Thread by melanieab | last post: by
1 post views Thread by Peter Anthony | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.