简体   繁体   中英

C# - KeyDown Event, FunctionKeys and TextBox

Here is code for a form with a button(not shown) and a textbox. Pressing any key other than function keys causes the KeyDown Event to fire as expected, the textbox is selected, the cursor flashes and the character of the subsequent keystroke appears in the textbox. When pressing a function key however, although the textbox is selected, the cursor does not flash and the character of the very next keystroke does not appear in the textbox. The characters of subsequent keystrokes do appear as expected.

    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        textBox1.Select();
    }
}

I have been able to circumvent this problem by overriding the ProcessCmdKey method but I am curious to know how to do it with KeyDown.

Thanks!!

These days, the preferred method of reading F[1-12] key presses is to override the ProcessCmdKey method, as you said. However, one (now deprecated) way to allow KeyDown to handle F keys is to set the form's KeyPreview property to true. However, as you can see in this post, there are disadvantages to this approach, so it's safer to use ProcessCmdKey .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM