简体   繁体   中英

Focus should not move on arrow keys in C#

I am developing one application for the PTZ, i have taken one grid, and when i press Up Arrow key then on KeyDown event of UP Arroa key i am starting my movement and on KeyUp even im stopping the movement. But when i Pres Arrow Key its KeyDown event is called and then focus is getting moved to another control so its KeyUp is not getting called....so i want stop this focus movement on arrowkeys so that i can get both event.....so how to do that.

I'd suggest that you read up on the WPF Routed Events system . Specifically, if you look at tunneling or "Preview" events , you'll find that you should be able to capture and supress the key event that is causing you problems.

I guess you could override it.

this might help: How to disable navigation on WinForm with arrows in C#?

You can use this code snippet from Diamonddrake as follows: Paste snippet in your form body such as other methods

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((keyData == Keys.Right) || (keyData == Keys.Left) ||
                (keyData == Keys.Up) || (keyData == Keys.Down))
            {
                //Do custom stuff or nothing.
                //true if key was processed by control, false otherwise
                return true;
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }

Note that for some controls like ComboBox, using up and down keys to scroll up and down between Items, may be necessary, so if you want to utilize them, remove this part of snippet (keyData == Keys.Up) || (keyData == Keys.Down) (keyData == Keys.Up) || (keyData == Keys.Down) .

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