Click here to Skip to main content
15,790,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Visual Studio 2019 Visual Basic VB.NET: WPF Applications

Running KeyDown Event (KeyPress is not available) on a TextBox.

Need to test for letters A-Z (upper case) and Horizonal Tab key.

Since the e.KeyChar is not available, using e.Key for this event.

The e.Key value for a lower case "a" is 52; Expecting ASCII Dec 97...

The TxtBoxPuzzleLine1Pos01.Text shows a lower case "a"

How can I get the correct corresponding ASCII Dec number for a character entered into the TextBox field?

What I have tried:

Entered upper and lower case letters and Tab key.
Updated 23-Jun-21 5:55am

Basically, don't.
If you are using the KeyDown event, then that's using a code that is "pre translation" and hasn't been changed to actual Unicode values. You can't assume that that will be "lowercase 'a'" because the effects of Caps and Shift keys aren't considered at that point.
The KeyPress event has the translated KeyChar value, the KeyDown and KeyUp events do not.

Use the Keys enum to work out which key it is: Keys Enum (System.Windows.Forms) | Microsoft Docs[^] - you will see that there are no lower case keys in there at all!
Share this answer
Thank you for your reply.

Per your suggestion, I will work around using Keys enum 
since the KeyPress event and translated KeyChar are not 
available to me.

My guess why this is that I am using core Xaml and 
WPF.  If  you can give me a reason why the KeyPress event
and translated KeyChar are not available to me let me know.

Thanks again.

P.S.  I don't know why it shows "Reject Solution" it should
        be "Accepted Solution".
Share this answer
Richard MacCutchan 23-Jun-21 11:29am    
You should not post this as a solution. If you want to comment on the above solution then use the Have a Question or Comment? link below the posted message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900