Key preview - arrow keys

AndyAinscow
AndyAinscow used Ask the Experts™
on
I have a form with a splitter container.  In one pane there are some buttons.

I want to provide specific functionality based on the left and right arrow keys so I have set the form KeyPreview to true and the following for the KeyDown event of the form (it does what I want most of the time):
        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                MovePrev();
                e.Handled = true;
            }

Now if any of the buttons on the pane have the focus then this code is never reached, instead the focus cycles through the buttons.  This NOT what I want.

I want the arrow keys to always be handled at the form level - how do I do that?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Trying to change navigation is often difficult and unrealiable. First, always think through the idea and make sure it's really what you want and that it's worth it.

Now ...  I haven't tried this before. A quick search seems to indicate that panels don't have any keyboard processing handlers. This link is interesting, dealing with giving a panel the gift of default keyboard processing.

http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.languages.csharp/2004-03/3364.html

You might have to add key down handlers to the controls in the panel and have them all forward the events to the form.
I'm guessing that MovePrev() cycles the focus through the buttons? If not please share that code, too.

If so, it looks like the code is doing exactly what it was intended: Each time you press a key, the handler is called. If it's the left key then focus is moved to the previous(?) button. Then the line "e.Handled = true" says to not send the key press to the control (a button in this case).

Why do you think this code is never reached because it looks like it is doing what it was programmed to do.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Just override ProcessCmdKey...KeyPreview does NOT have to be true for this to work:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Left:
                    MovePrev();
                    return true;
 
            }
 
            return base.ProcessCmdKey(ref msg, keyData);
        }
 
        private void MovePrev()
        {
            Console.WriteLine("MovePrev()..");
        }
 
    }

Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

AndyAinscowFreelance programmer / Consultant

Author

Commented:
Thanks to all for responding.

@ToddBeaulieu - seems to be so, pity the documentation seems to say the form should have first stab at the keypress.

@dericstone - if it was working the way I wanted then I wouldn't have noticed the code section was not being called.

@IdleMind - Spot on.
AndyAinscowFreelance programmer / Consultant

Author

Commented:
Some extra points added and split.
I never said the code was working as you wanted, only working as programmed. If you had removed the call to MovePrev(), would it have cycled focus through the buttons? Well at least you have the code next time you want to handle the arrow keys at the form level, and cycle through some buttons. ;-)
AndyAinscowFreelance programmer / Consultant

Author

Commented:
>>I never said the code was working as you wanted, only working as programmed.

Sorry, not even as programmed.  (ps. think of a database, move navigates through records, not buttons to manipulate contents of a record).

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial