简体   繁体   English

焦点不应该在C#中的箭头键上移动

[英]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. 我正在为PTZ开发一个应用程序,我已经采取了一个网格,当我按向上箭头键然后在UP Arroa键的KeyDown事件我开始我的运动和在KeyUp甚至我停止运动。 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. 但是当我按下Pres键时它的KeyDown事件被调用,然后焦点被移动到另一个控件,所以它的KeyUp没有被调用....所以我想在arrowkeys上停止这个焦点运动,这样我就可以得到这两个事件...... ..那怎么做。

I'd suggest that you read up on the WPF Routed Events system . 我建议您阅读WPF路由事件系统 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#? 这可能会有所帮助: 如何在C#中使用箭头禁用WinForm上的导航?

You can use this code snippet from Diamonddrake as follows: Paste snippet in your form body such as other methods 您可以使用Diamonddrake中的此代码段,如下所示:在表单正文中粘贴代码段,例如其他方法

    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) 请注意,对于某些控件(如ComboBox),可能需要使用向上和向下键在项目之间向上和向下滚动,因此如果要使用它们,请删除此部分代码段(keyData == Keys.Up) || (keyData == Keys.Down) (keyData == Keys.Up) || (keyData == Keys.Down) . (keyData == Keys.Up) || (keyData == Keys.Down)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM