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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
H MrowehPosted Aug 29, 2007, 5:58 AM
AlanPosted Aug 29, 2007, 5:50 AM
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 MrowehPosted Aug 29, 2007, 5:37 AM
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
AlanPosted Aug 29, 2007, 5:22 AM
To test for the enter key you can use:
if (e.KeyChar == (char)13)
{
// do something
}