开发者

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 moveme开发者_StackOverflownt and on KeyUp even i m 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜