Solved

keycode to keychar

Posted on 2004-04-22
15
2,833 Views
Last Modified: 2012-06-27
Is there a way to convert a keycode recieved in a KeyDown event to a character?
0
Comment
Question by:PLavelle
  • 6
  • 4
  • 3
  • +1
15 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10892428
Chr(e.KeyCode)

Bob
0
 

Author Comment

by:PLavelle
ID: 10892667
With that function, f becomes F
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 10892726
The computer I'm on right now doesn't have .NET but try this:

Convert.ToChar(e.KeyCode)

If that doesn't work then try this:

Convert.ToChar(Convert.ToInteger(e.KeyCode))
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:PLavelle
ID: 10892747
f still becomes F
0
 
LVL 2

Expert Comment

by:TeddyZero
ID: 10892918
The KeyDown event aways return the letter in upper case, if you want the lower case you must use KeyPress event or you can try to create a function that converts the characters to you, testing if the Shift Key is pressed or the Caps Lock is off and simple add 32 to value to get the lower case.

I hope this help you. :)
0
 

Author Comment

by:PLavelle
ID: 10893100
what if a character like $,%, or & is pressed? Will adding 32 to 4,5, and 8 respectively yield the correct character?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10894971
Yeah, I agree with TeddyZero, it would have to be the KeyPress event.  I didn't notice that it only returned the Upper case value.  Verrrry interrresting!!!!

Bob
0
 

Author Comment

by:PLavelle
ID: 10899413
The reason that I am trying to use it in the keydown event is that I have to put a grid into edit mode. If I put the grid into edit mode in the keypress event, I get some weird results.
0
 
LVL 2

Accepted Solution

by:
TeddyZero earned 250 total points
ID: 10901168
Simple, check keycode value for a valid character

If CInt(e.KeyCode) >= 65 and CInt(e.KeyCode) <= 90 Then     ' This is a valid letter A-Z
    DoMyStuffHere()
    ...
Else    ' It's another char
   DoSomethingUseful()
End If

Try to do this, if I have another ideia, I'll put here.
0
 

Author Comment

by:PLavelle
ID: 10901192
The only thing is I don't want the user to be limited to only typing letters. I was hoping there was a function that would convert any keycode to its corresponding keychar so I wouldn't have to limit what the user types in.
0
 
LVL 2

Expert Comment

by:TeddyZero
ID: 10901328
This test is only for you to identify the letters, not to limit your user, this is to get the letter and convert it to lower case if it's necessary or not, remember KeyDown event only return the upper case letter, it's not return de keychar only the key, for ex.:
Windows Logo Key, only in KeyDown event you can get that key and some others keys

This event is not intend to to get the character ASCII, it's intended to get the Key on the keyboard that as pressed, because of this the code above converted the char f to F.

If you want to use it, use it, but remember you need to get CAPS LOCK state and Shift modifier to detect the key pressed by the user, if your user press f and him/her get F, your user will say: Hmmmmmmm something is wrong.
And him/her will try to press CAPS LOCK or Shift thinking that it is on.

Sorry for bad english or something wrong.
I hope this help's you.
0
 
LVL 2

Expert Comment

by:TeddyZero
ID: 10901442
Some more tips.

KeyDown event send to you the Extended Keys, keys that need more than 1 byte to be represented, like F1-F12, Print Screen, WinKey, Menu Key, etc...

KeyPress event send to you the standard keys, keys that as represented in ASCII table, these chars use only 1 byte to be represented.

Because of this in the KeyDown event you must use Keys Enumeration, but in the KeyPress you use Chr ou ChrW to test the key.

this is some differences that I know, if I'm wrong, please correct me.

Have a nice day. :-)
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 250 total points
ID: 10903626
You could probably use a Select Case for the A-Z and process the adding 32 for just those key presses by checking the Shift key, and ignore it for everything else.

Bob
0
 

Author Comment

by:PLavelle
ID: 11127769
I was looking for a simple solution that would easily convert the keycode to a character but I guess its not possible. Thanks for the help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question