?
Solved

Determining the entered character on the KeyDown event

Posted on 2009-04-17
5
Medium Priority
?
281 Views
Last Modified: 2013-11-07
Hi all,

Can anyone tell me how to extract the character that was entered on a Windows Forms control's KeyDown event?

I can sort of get it working - but not quite. In particular, the code snippet I'll provide converts numpad numbers into various letters and characters such as /., into other random symbols.

Clearly the mapping isn't quite correct.

Can anyone advise if its possible to get the entered characters including punctuation and numpad values out correctly?

Many thanks

Simon
protected override void OnKeyDown(KeyEventArgs e) {
            char proposedCharacter;
 
				// Doesn't work for numpad or various punctuation characters!:
            proposedCharacter = (char)e.KeyValue;
            Console.Out.WriteLine(proposedCharacter);
 
        }

Open in new window

0
Comment
Question by:sh856531
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Nirmalan Nagenthiran
ID: 24168503

Use e.KeyCode instead of e.KeyValue
 
 
protected override void OnKeyDown(KeyEventArgs e) 
{
	char proposedCharacter;
 
	// Doesn't work for numpad or various punctuation characters!:
	proposedCharacter = (char)e.KeyCode;
	Console.Out.WriteLine(proposedCharacter);
	label1.Text = proposedCharacter.ToString();
}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1500 total points
ID: 24168930
You will have to do some catching in your code as key mappings are not the same as character mappings. You could do something like:
if (e.KeyCode == Keys.NumPad0)
    Console.WriteLine('0');
else if (e.KeyCode == Keys.NumPad1)
    Console.WriteLine('1');
//etc...

Open in new window

0
 

Author Comment

by:sh856531
ID: 24169328
Hi Kaufmed

Thanks for that. Do you know if I'm going to have to do this for all numeric characters and punction characters one by one?

I'm really surprised that there isn't some mapping class that can figure it out.

Thanks again
0
 

Author Comment

by:sh856531
ID: 24169360
Hi NNirmalan

Thanks for your reply

Are you sure that will work for all numeric values and punctuation? The only reason I ask is that there's another helpful chap on another forum suggesting that KeyCode and KeyValue amount to the same thing when cast to a char

Thanks again

S
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24170934
Sorry.. Dr.'s appt.

As far as I can tell, yes you will have to check each code of interest (the normal alpha characters should not be an issue). The reason is that there are different keyboard languages and layouts. I'm sure it would not be that difficult to make a class to do the conversion for you--then at least you would have some reusable code :)
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 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