On Aug 24, 11:59 am, "Tom P." <padilla.he...@ gmail.comwrote:
You wouldn't think this would be as hard as it is but for some reason
I can't find a way to translate any of the codes in the
KeyDownEventArg s into the actual characters they represent.
The best I can do is get the uppercase character using
Convert.ToChar( ) but if the user types a lowercase character I still
end up with the uppercase character.
What I need to know is how do I get the correct character during the
KeyDown event?
Tom P.
There isn't an easy way to accomplish this. As you've discovered the
KeyValue data returns ASCII codes for upper case letters even if caps
lock is on. I would advise putting some code in your event handler to
detect if caps lock is on -or- to detect if the shift key is being
used. If that criteria is not met then you can convert to lower if the
ASCII code falls within a alphabetic range (65-90)
Code example of determing if caps lock is on/off in C#
using System;
using System.Runtime. InteropServices ;
using System.Windows. Forms;
public class CapsLockControl
{
[DllImport("user 32.dll")]
static extern void keybd_event(byt e bVk, byte bScan, uint
dwFlags,UIntPtr dwExtraInfo);
const int KEYEVENTF_EXTEN DEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
public static void Main()
{
if (Control.IsKeyL ocked(Keys.Caps Lock))
{
Console.WriteLi ne("Caps Lock key is ON. We'll turn it
off");
keybd_event(0x1 4, 0x45, KEYEVENTF_EXTEN DEDKEY, (UIntPtr)
0);
keybd_event(0x1 4, 0x45, KEYEVENTF_EXTEN DEDKEY |
KEYEVENTF_KEYUP ,
(UIntPtr) 0);
}
else
{
Console.WriteLi ne("Caps Lock key is OFF");
}
}
}
NOTE: The above code is not an original creation, see reference below
for credit.
http://cboard.cprogramming.com/showthread.php?p=776372 http://www.zelos.org.uk/Resources/ASCII/