Solved

keycode to keychar

Posted on 2004-04-22
15
2,838 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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