Detect Input Key, Convert value to DWORD

Hello.
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);
            MessageBox.Show(result);
        }

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.
JoeD77Asked:
Who is Participating?
 
brutaldevCommented:
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()
{
  InitializeComponent();
  textBox1.KeyPress += (s, e) => MessageBox.Show(e.KeyChar.ToString());
}

Open in new window

Note: KeyChar is the property you want to look at.
0
 
brutaldevCommented:
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)

0
 
JoeD77Author Commented:
Thanks for your help, brutaldev.

Changed code to:
private void textBox1_KeyPress(object sender, KeyEventArgs e)
        {
            MessageBox.Show(e.KeyCode.ToString());
        }

Open in new window


Still displays nothing. _KeyDown has the same result.
0
 
sameer_goyalCommented:
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:

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/b1f8a08e-a419-44f5-837b-31369dc6ac63

Let me know.
0
 
JoeD77Author Commented:
Wonderful, thanks for noticing my ignorance :)!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.