Equivalent ASCII code value for key pressed
In .Net framework 3.5 (WPF application), when I try to cast entered key into byte or int, I am getting wrong ASCII value. If I press A, I am getting 44 as casted value but it should be 65. Please let me know if am I doing anything wrong?
Prabu- Moved byYiChun ChenMSFTMonday, November 02, 2009 10:18 AMWPF issue (From:.NET Framework Setup)
Answers
- Hi Prabu.P,
Sorry for the late reply.
KeyDown events correspond to WM_KEYDOWN messages, which are raw key input—roughly corresponding to physical key presses. Character/text input occurs after translation of raw key events, which can be quite non-trivial in the case of IMEs. You should handle PreviewTextInput events instead, and the utilize the e.Text property to get the character you are just pressing.
Here’s some good background reading:http://msdn.microsoft.com/en-us/library/ms646267(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms754010.aspx#text_inputReal
Thanks.
Sincerely.
Jim Zhou -MSFT- Marked As Answer byJim Zhou - MSFTModeratorWednesday, November 18, 2009 9:52 AM
All Replies
Hi
Plz post the code sample .....
Refer this examples
Does this help
The Visual Basic Title Convert a character to and from its ASCII code
Keywords ASCII, value, character
Categories Utilities
Use Asc to get a character's ASCII code. Use Chr to convert an ASCII code into a character.
Thanks to Adan Rivas
Private Sub Command2_Click()
'Code the character
Label3 = Asc(Text1)
'clears text box
Text1 = ""
' adds the coded character to textbox (label3)
LABEL6 = LABEL6 + "," + Label3
' set focus to textbox
Text1.SetFocus
'decodes the coded character
Text2 = Text2 + Chr(Label3)
End SubPrivate Sub Command3_Click()
'this part decodes the coded number
Label3 = Text3
Text3 = ""
LABEL6 = LABEL6 + "," + Label3
Text2 = Text2 + Chr(Label3)
Text3.SetFocus
End Sub
Chr function takes an ASCII character code and returns the character.Print Chr(65) ' prints "A"
Thks
anliS
Best Regards,
anliS
BIC
Please remember to mark the correct replies as answers- Did you run your code in .Net framework 3.5 (WPF application), when I try to cast entered key into either byte or int , I am getting wrong ASCII value. If I press A, I am getting 44 as casted value but it should be 65 . Please let me know if am I doing anything wrong?
This is the code I am using in my application.
private void TextBox_KeyDown(object sender, KeyEventArgs e) { MessageBox.Show(string.Format("Key : {0} \nKeyCode : {1}", e.Key, (byte)e.Key)); }
Prabu - Hi Prabu,
I am moving this thread from Base ".NET Framework Setup" forum to the "Windows Presentation Foundation (WPF)" forum, since the issue is related to WPF. There are more WPF experts in the "Windows Presentation Foundation (WPF)" forum.
Thanks
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. Hi Prabu.P,
You can first get the Char type from the string, and then use Convert.ToInt32 method to do the converter as shown below.
Code snippet:
string s =e.Key.ToString();
char[] chars = s.ToCharArray();
char chr = chars[0];
Console.WriteLine (System.Convert.ToInt32(chr));
Thanks.
Sincerely.
Jim Zhou -MSFT- Marked As Answer byJim Zhou - MSFTModeratorWednesday, November 04, 2009 8:02 AM
- Unmarked As Answer byPrabu.P Wednesday, November 04, 2009 10:48 AM
- Thanks for the code snippet. But this makes no difference between Upper and lowercase. How do I differenciate this?
The ASCII value of the charactor should be common for all launguage rite? Why does it was changes in .Net 3.5 framework.? That is my actual question.
Thanks for your time and reply.
Prabu - Hi Prabu.P,
-->But this makes no difference between Upper and lowercase. How do I differenciate this?
It is not because there is no difference between the uppercase and the lowercase, when you press keys on keyboard, the e.Key value will always be the uppercase, so you just see the ASCII is the same. For the sample below, you can see that for "a", the ASCII is 97, and for "A", the ASCII is 65.
Code snippet:
string s = "a";
char[] chars = s.ToCharArray();
char chr = chars[0];
Console.WriteLine(System.Convert.ToInt32(chr)); //output 97
string s1 = "A";
char[] chars1 = s1.ToCharArray();
char chr1 = chars1[0];
Console.WriteLine(System.Convert.ToInt32(chr1)); //output 65
-->The ASCII value of the charactor should be common for all launguage rite? Why does it was changes in .Net 3.5 framework.?
You are right, the ASCII values are the same for all kinds of languages, actually, I just ran the code above on my machine which has .Net 3.5 Framework install.
Thanks.
Sincerely.
Jim Zhou -MSFT - Hello,
The code you were given is rite. My actual question is, how do I get the actual key's ASCII value and how to differentiate it. I should get 65 when I press "A" and "97" when I press "a". I want the code snippet for this.
Hope you should clear now.
Thanks for your time.
Prabu - Hi Prabu.P,
I got it, in this case, one workaround is that you can check the status of Key.Capital, since it's a little late in our time zone and now I afraid I have to go home, I will continually see to this case and let you know the result if I get a better idea.
Have a nice weekend.
Thanks.
Sincerely.
Jim Zhou -MSFT - Hi,
I hope this is not an expected resultant for my query. Could you please post the workaround or solution for this query?
Thanks for your time.
Prabu - Hi Prabu.P,
Sorry for the late reply.
KeyDown events correspond to WM_KEYDOWN messages, which are raw key input—roughly corresponding to physical key presses. Character/text input occurs after translation of raw key events, which can be quite non-trivial in the case of IMEs. You should handle PreviewTextInput events instead, and the utilize the e.Text property to get the character you are just pressing.
Here’s some good background reading:http://msdn.microsoft.com/en-us/library/ms646267(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms754010.aspx#text_inputReal
Thanks.
Sincerely.
Jim Zhou -MSFT- Marked As Answer byJim Zhou - MSFTModeratorWednesday, November 18, 2009 9:52 AM
Hi Prabu.P,
If you are still having any issues with this, please feel free to ask.
Thanks.
Sincerely.
Jim Zhou -MSFT


