Deriving the KeyChar on a KeyDown event.

P

Phil Jones

With a KeyDown/KeyUp events of a WinForms textbox:

Are there any useful utility objects, or approaches to derive the actual
key-character that was pressed from the event's KeyCode.

The character is only available on the KeyPress event - but I'm wanting to
work with all the data on the KeyDown event handler, plus the actual
key-character.

Thanks for any advice!
===
Phil : NZ
 
J

Jeffrey Tan[MSFT]

Hi Phil,

Based on my understanding, you want to know the actual keypress char in
Keydown/keyup event.

This is similar as Win32 keyboard messages:
Message Key or Code
WM_KEYDOWN Virtual key code for `A' (0x41)
WM_CHAR Character code for `a' (0x61)
WM_KEYUP Virtual key code for `A' (0x41)

After WM_KEYDOWN message, Windows use TransplateMessage win32 API to
translate the Virtual-Key code into character code, with a WM_CHAR message.
That is why we can not directly get the character code in keydown event,
because Windows did call TransplateMessage yet.

For this issue, we may use a private char variable to store the character
key press in KeyPress event, then we can refer it in KeyUp event.

For keydown event, there is not an easy way to translate, but we may use
KeyEventArgs.KeyCode.ToString() method to get the string representation of
key stroke, which is the same as the character press for "visible key
stroke" such as english character and numberic etc.
But keydown may also get some "invisible key stroke" such as F1~F12, Num
Lock, Insert, Delete etc.., we may store all the visible characters in a
string, then use String.IndesOf method to determine if the key stroke in
keydown is in this string(if in, it is visible key stroke).

=========================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi Jeffrey,

I am looking for similar solution. How do you handle combination key strokes
like "Ctrl R"?
With KeyDown it raises the event for each key.

I tried to handle this scenario using KeyPress,, but i cannot capture
function keys with that.

I need some functionality from KeyDown and KeyPress to solve my problem.

I need to capture function keys(F1..F12) and short cut keys(like combination
keys CTrl R ,Shift F2 etc..)

Any suggestions are appreciated.

Thanks
 
J

Jeffrey Tan[MSFT]

Hi Phil,

Does my reply make sense to you? Is your problem resolved?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi Jeffrey,

I'm encountering a similar dilemma. I want to provide an onscreen keyboard
in my application. (All the keys are given their respective virtual
keycodes). I want to be able to dynamically convert those key codes to
display characters using something similar to the MapVirtualKey. (This will
allow my user to switch languages).

(I tried using the KeysConverter class but that doesn't seem to work
properly).

Thanks for your help.
 
G

Guest

Jeffrey:

I tried your suggestion, however for keyCode.ToString() I am getting values
such as "OemPeriod" - clearly not what I want. Is there another option here?

Thanks - Daniel
 

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