Solved

keycode to keychar

Posted on 2004-04-22
15
2,815 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
Comment Utility
Chr(e.KeyCode)

Bob
0
 

Author Comment

by:PLavelle
Comment Utility
With that function, f becomes F
0
 
LVL 15

Expert Comment

by:Timbo87
Comment Utility
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
 

Author Comment

by:PLavelle
Comment Utility
f still becomes F
0
 
LVL 2

Expert Comment

by:TeddyZero
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:PLavelle
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
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 this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now