In Windows 10 Winforms app, Keys.Right and Key.Left are not handled in Textbox.KeyPress EventHandler

asked 2023-01-09T18:15:52.98+00:00
Don Baechtel 61 Reputation points

In Windows 10 Winforms app, Keys.Right and Key.Left are not handled in Textbox.KeyPress EventHandler
In Visual Studio 2022 CE, I have a Windows 10 Winforms application, and in a TextBox.KeyPress event handler, I am trying to handle the KeyPress of Keys.Right and/or Keys.Left keyPress. While debugging, I place a breakpoint on the first line of the KeyPress EventHandler. If I press the Left or Right keys, the breakpoint in the KeyPress EventHandler is not triggered, and yet the movement of the insertion pointer within the TextBox is correct (Left or Right) within the text in the TextBox.

No error messages are generated in the application.

Why are the Keys.Right and Keys.Left key presses not being handled in the TextBox.KeyPress EventHandler? Where are these KeyPress events of Left and Right being handled? What is causing the movement (Left or Right) of the TextBox insertion pointer on these events? How can I override this TextBox behavior?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,316 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
693 questions
No comments
{count} votes

Accepted answer
  1. answered 2023-01-10T07:52:59.087+00:00
    Castorix31 65,366 Reputation points

    Arrows keys are managed by controls in WM_GETDLGCODE message Then you can handle this message in the control or WM_KEYDOWN/WM_KEYUP

    For example, a test with WM_KEYDOWN:

    public partial class MYTextBox : TextBox
    {
        public MYTextBox()
        {
    
        }
    
        public const int WM_KEYDOWN = 0x0100;
        public const int WM_KEYUP = 0x0101;
    
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_KEYDOWN)
            {
                if (m.WParam == (IntPtr)Keys.Left || m.WParam == (IntPtr)Keys.Right)
                    Console.Beep(5000, 10);
                m.Result = IntPtr.Zero;
            }            
        }        
    }
    
    No comments

2 additional answers

Sort by: Most helpful
  1. answered 2023-01-14T12:30:51.34+00:00
    Reza Aghaei 3,011 Reputation points Microsoft MVP

    You can override WndProc like what suggested by Castorix31, but an easier approach as mentioned by Viorel's as well, is handing KeyDown event. Then to override the behavior, you need to set e.Handled = true. For example, to disable changing the caret position by arrow keys:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right ||
            e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
        {
            e.Handled = true;
            //e.SuppressKeyPress = true;
        }
    }
    

    For some keys, like shortcut keys, you need to set e.SuppressKeyPress = true as well. For example to disable Ctrl+X:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == (Keys.Control | Keys.X))
        {
            e.Handled = true;
            e.SuppressKeyPress = true;
        }
    }
    

    There are also some other useful events like PreviewKeyDown or some other useful methods to override, like ProcessCmdKy, ProcessDialogKey, IsInputKey, IsInputChar which have their own purpose.

    No comments

  2. answered 2023-01-09T18:41:37.823+00:00
    Viorel 82,476 Reputation points

    According to documentation, for non-character keys you can handle the KeyDown or KeyUp events. The value of e.KeyCode will be Keys.Left or Keys.Right.