Dear All,
I have a C# program and I have an on KeyPress event. I want to test the value of the key and find out if it is the Enter key. My code is like this:
public viod MyKeyPress(KeyPressEventArgs e)
{
if(e.KeyChar )
}
Dear All,
I have a C# program and I have an on KeyPress event. I want to test the value of the key and find out if it is the Enter key. My code is like this:
public viod MyKeyPress(KeyPressEventArgs e)
{
if(e.KeyChar )
}
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Aug 29, 2007, 1:22 AM EDT
To test for the enter key you can use:
if (e.KeyChar == (char)13)
{
// do something
}
H Mroweh12 reputationPosted Aug 29, 2007, 1:37 AM EDT
thanks, but if I want to display the char code? For example, when the enter key is pressed, the value 13 is displayed, when the shift key is pressed, another value is displayed, I mean how do u get the value of the key (13 is for enter)?
Thank you
AlanRank #669 · 3.6K reputationPosted Aug 29, 2007, 1:50 AM EDT
You can display the value of the key pressed with this line:
MessageBox
.Show("The value of the key pressed is " + ((int)e.KeyChar).ToString());Notie that you need to cast e.KeyChar to 'int' otherwise it will just display as the character itself if it is printable.
Here's a table showing the ASCII values of the various keys (C# uses Unicode but the first 128 values are the same). You'll notice that 'carriage return' corresponds to 13:
http://www.asciitable.com/
H Mroweh12 reputationPosted Aug 29, 2007, 1:58 AM EDT