Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Detect Input Key, Convert value to DWORD

Posted on 2011-04-26
Medium Priority
Last Modified: 2012-05-11
I'm trying to enable user input to configure hotkeys for an application, and I'm struggling with trying to detect when a key is pressed and subsequently retrieve the DWORD value pertaining to said pressed key.

I'm trying something like this:
        private void textBox1_TextChanged(object sender, KeyEventArgs e)
            string result = String.Format("%d", e.KeyValue);

Open in new window

When I type in textBox1, nothing happens at all.

Ideally, I'd like the user to be able to click a button, and then enter in their input, which will be recorded as a DWORD value.

Any help would be great, thanks.
Question by:JoeD77
  • 2
  • 2
LVL 11

Expert Comment

ID: 35473106
The TextChanged event only fires when you leave focus of the text box not when you are actually typing each key. For that you should use KeyDown, KeyUp or KeyPress events.

That is not the correct syntax for String.Format. You just use place holders like string result = String.Format("{0}", e.KeyValue); The conversion of what you pass in will get converted to a string automatically (using the ToString() method of object internally)


Author Comment

ID: 35473134
Thanks for your help, brutaldev.

Changed code to:
private void textBox1_KeyPress(object sender, KeyEventArgs e)

Open in new window

Still displays nothing. _KeyDown has the same result.

Assisted Solution

sameer_goyal earned 200 total points
ID: 35473293
Hey Joe,

I assume you have properly generated the KeyPress for the TextBox and not merely changed the name of the TextChanged event?

This link might help you further:


Let me know.
LVL 11

Accepted Solution

brutaldev earned 1800 total points
ID: 35473823
Yup, looks like you just renamed the method (because the event for KeyPress does not contain the second argument KeyEventArgs, it should be KeyPressEventArgs).

Either attach the event (generate the method) from the front-end by double clicking it:
 textBox1 properties from VS designer.
Or add the following type of code in the constructor of your Form to manually create the event code (won't work in .NET 2.0):
public Form1()
  textBox1.KeyPress += (s, e) => MessageBox.Show(e.KeyChar.ToString());

Open in new window

Note: KeyChar is the property you want to look at.

Author Closing Comment

ID: 35477492
Wonderful, thanks for noticing my ignorance :)!

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Integration Management Part 2
Screencast - Getting to Know the Pipeline

571 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