When handling Control.OnKeyPress
event, there is a KeyPressEventArgs
that contains a KeyChar
.
For usability reasons I need exactly the same KeyChar
but when handling OnKeyDown
event.
The KeyEventArgs
does not contains any char related data. I mean, if press the A
key with or without Shift
its not affecting the KeyCode
, KeyData
or KeyValue
. The same when using another language, i still getting capital english values.
How to get a KeyPressEventArgs.KeyChar
inside KeyDown
event?
Thanks.
If the reason you're wanting to use KeyDown instead of KeyPress is to capture the extra information that's given to a KeyDown event, could you perhaps capture that extra information in a KeyDown event and then use it when you get the KeyPress? Rather a hokey approach, to be sure, but I'm not sure of any other approach that will really work in a keyboard mapping that has "dead keys" (i.e. two-key sequences that produce a single character). I don't know why Microsoft didn't carry along more information through to the KeyPress event (or a lot of its events, for that matter) since trying to match up cause-and-effect events isn't perfect, but I don't really know any better alternative.
Convert it. Here is simple function for converting 'A' to 'a'. It converts only capital chars from [A-Z] set. The value 32 is diff between 'A' and 'a'.. Sure you can extend it to your requirements, or ask for feature here.
if you need it to works with your current culture you should override ProcessKeyMessage Control method:
Hope it helpful.
I think that what you are looking for is in fact the KeyCode property.
If you are just looking for certain keys you could setup a switch statment or whatever your heart desires.