Solved

keycode to keychar

Posted on 2004-04-22
15
2,845 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

690 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