469,267 Members | 1,029 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

How to get a string during the KeyDown event

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.
Aug 24 '08 #1
4 10881
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
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/

Aug 24 '08 #2
I found that Console.CapsLock returns true or false
when either application is in Console Type Or Windows Type

you could
private void txName_KeyDown(object sender, KeyEventArgs e)

{

string sKey = e.KeyData.ToString();

if (Console.CapsLock == false) sKey = sKey.ToLower();

//do what u need with sKey

}

DaveL
Tom P." <pa***********@gmail.comwrote in message
news:82**********************************@a70g2000 hsh.googlegroups.com...
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.

Aug 24 '08 #3
I wonder if the problem is that your using the wrong event.

The key down event will give you the key code of the key but not the decoded
keypress combination.

This is good for detecting keys in a way that you may in a video game for
move left or right for example.

To detect the actual key after decoding, use the KeyPress event.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Tom P." <pa***********@gmail.comwrote in message
news:82**********************************@a70g2000 hsh.googlegroups.com...
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.
Aug 24 '08 #4
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
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.
I wanted to add that KeyDown doesn't handle all keys. If you're trying
to implement a KeyDown handler on a Form you'll likely encounter
problems with it appearing to be inconsistent in handling key presses
(for more info on why this is see reference link below). If this is a
problem for you, there are a few solutions. In this past I've done
this using an a class implementing IMessageFilter (see code sample
below). In my case I was only mapping a few "hot keys" there may be a
better approach to resolving the the character from the API message.

Also, have a look at this article:
http://msdn.microsoft.com/en-us/library/ms171535.aspx
public class KeyboardMsgFilter : IMessageFilter
{
public delegate void KeyboardEvent(object source, string
keyVal);
public event KeyboardEvent KeyPressed;

public bool PreFilterMessage(ref Message m)
{
string key = "";
const int WM_KEYDOWN = 0x0100;

if (m.Msg == WM_KEYDOWN)
{

int keycode = Convert.ToInt32(m.LParam.ToString());
switch (keycode)
{
case 1179649 :
key = "E";
break;
case 1245185 :
key = "R";
break;
case 2031617 :
key = "S";
....
break;

}

if (key != "")
{
KeyPressed(null, key);
}

}
return false;
}
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
KeyboardMsgFilter kmf = new KeyboardMsgFilter();
Application.AddMessageFilter(kmf);

Application.Run(new Form1(kmf));
}
}

Aug 24 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by mee-shell | last post: by
3 posts views Thread by bardo | last post: by
1 post views Thread by fripper | last post: by
5 posts views Thread by ApexData | last post: by
3 posts views Thread by win | last post: by
2 posts views Thread by sush_jd via DotNetMonster.com | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.