Solved

keycode to keychar

Posted on 2004-04-22
15
2,828 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

778 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