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
KeyDownEventArgs 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("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint
dwFlags,UIntPtr dwExtraInfo);
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
public static void Main()
{
if (Control.IsKeyLocked(Keys.CapsLock))
{
Console.WriteLine("Caps Lock key is ON. We'll turn it
off");
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)
0);
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY |
KEYEVENTF_KEYUP,
(UIntPtr) 0);
}
else
{
Console.WriteLine("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/