Detect Input Key, Convert value to DWORD

Posted on 2011-04-26
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
    LVL 11

    Expert Comment

    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

    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.
    LVL 5

    Assisted Solution

    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

    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

    Wonderful, thanks for noticing my ignorance :)!

    Featured Post

    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.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    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…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 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

    20 Experts available now in Live!

    Get 1:1 Help Now