473,795 Members | 3,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Aug 24 '08 #2
I found that Console.CapsLoc k 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.ToStr ing();

if (Console.CapsLo ck == false) sKey = sKey.ToLower();

//do what u need with sKey

}

DaveL
Tom P." <pa***********@ gmail.comwrote in message
news:82******** *************** ***********@a70 g2000hsh.google groups.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
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.

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******** *************** ***********@a70 g2000hsh.google groups.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
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.
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
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.
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 KeyboardMsgFilt er : IMessageFilter
{
public delegate void KeyboardEvent(o bject source, string
keyVal);
public event KeyboardEvent KeyPressed;

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

if (m.Msg == WM_KEYDOWN)
{

int keycode = Convert.ToInt32 (m.LParam.ToStr ing());
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.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (fals e);
KeyboardMsgFilt er kmf = new KeyboardMsgFilt er();
Application.Add MessageFilter(k mf);

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

Aug 24 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2994
by: mee-shell | last post by:
Hi, I am a python newbie. I have created a tool for capturing the screen shot. I want to be able to do the screen capture with the keydown event as well. The tool worked fine when it first opened. However, if I keep the tool open, and open another window to do the screen capture, the tool will lose its focus, and the keydown event will not work anymore. I wonder if any of you can help me to solve this problem of how to get the focus...
3
9238
by: bardo | last post by:
I have a Datagrid that is inside a panel. I want to use the keyDown event to reconize the arrow keys. But I have no luck at all. The problem is that the keydown event won't fire at all, unless I click on a row (withs will turn blue then) and then click on it again . Now if I press any key the event will fire (except for the arrow keys). I also tried to override the IsInputKey => no luck. I also tried to override the ProcessCmdKey => With...
0
1867
by: Peter | last post by:
I have a VB6 program which can receive Keydown events on an ActiveX control. The ActiveX control can't fire keydown events so I put a picturebox below the ActiveX control. I write codes in function picturebox_keydown in response to the keydown events on the ActiveX control. These can work well. But when I update these codes to vb.net I got a message 'UPGRADE_WARNING: PictureBox event Picture1.KeyDown can't be updated. Click to get more...
6
4248
by: MLH | last post by:
Using A97. Want to examine 17-char VIN entered by user. VIN codes are alphanumeric and do not contain Oh's to prevent the confusion that could result if zeros were misread as O's or o's. So, if a user types a 17-char VIN into the textbox that has an Oh in it (lower or upper case) - I would like to change it to a zero during the BeforeUpdate code. So far, I've not been able to accomplish this. I can examine
1
3310
by: fripper | last post by:
I have a VB 2005 windows app and I want to recognize keydown events. I have a form key down event handler but it does not get control when a key is depressed. In playing around I found that if I add a keydown event handler for some control on the form, say a textbox ... Private Sub txtBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBox.KeyDown) and then give that control focus the keydown...
5
3030
by: ApexData | last post by:
This follows a previous post, when I was trying to capture a key pressed during the immediate opening of a form (ie in the first 3-secs before processing any of the code that followed it. It was mentioned that the KeyDown event fires the instant a key is pressed down. So, if you place the following code in a form, you'll see my concern.
3
4303
by: win | last post by:
when the cursor is in a textbox, only coding in the keydown event of the textbox triggered, the coding in the keydown event of the form does not triggered! Problem: I need to change a VB6 program to .Net. It uses function key(e.g. F12) to close the form, now i need to write the coding in the keydown event of all controls form. Can anyone help me? Thanks a lot.
2
1949
by: sush_jd via DotNetMonster.com | last post by:
I am using managed VC++ code in a Win Form App. There is a text box - txtRONumber. I have defined a KeyDown event handler (non-default) for it, like below. InitializeComponent() is being called from Constructor of the Form Class. void InitializeComponent(void) { this->txtRONumber = (gcnew System::Windows::Forms::TextBox()); this->txtRONumber->Location = System::Drawing::Point(151, 35);
3
6321
maheshwag
by: maheshwag | last post by:
I have a databound combobox column on DGV I am trying to fire a keys on keydown eventhandller on DGV as below: private void Form1_Load(object sender, EventArgs e) { string connstr = "server=.;initial catalog=maa;uid=mah;pwd=mah"; SqlConnection con = new SqlConnection(connstr); con.Open(); string sql = "select name from dummy"; SqlDataAdapter dap = new...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.