locked
KeyDown Event, FunctionKeys and TextBox RRS feed

  • Question

  •     public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            textBox1.Select();
        }
    }

    Here is code for a form with a button(not shown) and a textbox.  The KeyPreview property is set to true.  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.

    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 and/or why function keys do not behave as expected.  Replacing Select() with the Focus() method did not solve the problem.

    Thanks!

    Friday, April 11, 2014 12:10 AM

Answers

  • Hiya,

     Here I think you will like solution;

    private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
            {
                switch ( e.KeyCode )
                {
                    case Keys.F1 :
                        button1.Text = "F1";
                        break;
    
                    default :
                        break;
                }

    The above code will trap or catch any of the KeyCode listed in the switch/case statement.

    Two things to remember, when you assign an Event handler, insure the correct control is

    active on your Design Viewer, otherwise you may not get the expected results. And, keyboard

    scan codes are more than just single bytes, sometimes they are 2 bytes, especially for Function

    keys. But, no need to worry cause the library code already knows this.

    Hope this helps. Thanks :)

    • Proposed as answer by User3DX Friday, April 11, 2014 2:47 AM
    • Marked as answer by Caillen Thursday, April 17, 2014 7:33 AM
    Friday, April 11, 2014 1:19 AM
  • Hiya,

     The Windows msg loop fires all events, what you want to do is code the event

    handler of a specific trigger. So, if you want Form to catch Key events then place

    the handler there. Inside the handler code what to do, like focus changes, etc.

    and all other events ignored by the code for that event. As for the TextBox,

    same logic, catch anything and ignore the Function KeyCode. Is this what you

    was looking for? Hope this helps. 



    • Proposed as answer by User3DX Friday, April 11, 2014 4:44 PM
    • Unproposed as answer by User3DX Friday, April 11, 2014 4:55 PM
    • Edited by User3DX Friday, April 11, 2014 4:59 PM
    • Marked as answer by Caillen Thursday, April 17, 2014 7:33 AM
    Friday, April 11, 2014 4:21 PM

All replies

  • Hiya,

     Here I think you will like solution;

    private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
            {
                switch ( e.KeyCode )
                {
                    case Keys.F1 :
                        button1.Text = "F1";
                        break;
    
                    default :
                        break;
                }

    The above code will trap or catch any of the KeyCode listed in the switch/case statement.

    Two things to remember, when you assign an Event handler, insure the correct control is

    active on your Design Viewer, otherwise you may not get the expected results. And, keyboard

    scan codes are more than just single bytes, sometimes they are 2 bytes, especially for Function

    keys. But, no need to worry cause the library code already knows this.

    Hope this helps. Thanks :)

    • Proposed as answer by User3DX Friday, April 11, 2014 2:47 AM
    • Marked as answer by Caillen Thursday, April 17, 2014 7:33 AM
    Friday, April 11, 2014 1:19 AM
  • I have my KeyDown event on the form and it fires as expected and breakpoints work.  When I reset the KeyDown event on the form and create a PreviewKeyDown event instead, it does not do what is expected and breakpoint on the routine in debug does not interrupt the execution.  BTW, you have the PreviewKeyDown on the textbox, I need to capture the function key press from anywhere in the form and only THEN select/focus on the textbox.

    Thanks

    Friday, April 11, 2014 4:17 PM
  • Hiya,

     The Windows msg loop fires all events, what you want to do is code the event

    handler of a specific trigger. So, if you want Form to catch Key events then place

    the handler there. Inside the handler code what to do, like focus changes, etc.

    and all other events ignored by the code for that event. As for the TextBox,

    same logic, catch anything and ignore the Function KeyCode. Is this what you

    was looking for? Hope this helps. 



    • Proposed as answer by User3DX Friday, April 11, 2014 4:44 PM
    • Unproposed as answer by User3DX Friday, April 11, 2014 4:55 PM
    • Edited by User3DX Friday, April 11, 2014 4:59 PM
    • Marked as answer by Caillen Thursday, April 17, 2014 7:33 AM
    Friday, April 11, 2014 4:21 PM