如何禁用按箭头键更改焦点 [英] How to disable focus changes by arrow keys

查看:223
本文介绍了如何禁用按箭头键更改焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个控件(按钮,groupboxes等),WPF窗口一大 Viewport3D A 境内

I have a WPF window with a few controls (buttons, groupboxes, etc) and one big Viewport3D within a Border.

视口显示了一个3D场景,我想箭头键移动周围的摄像头。问题:方向键总是将焦点移到另一个的UIElement

The viewport shows a 3D scene and I want the arrow keys to move its camera around. The problem: the arrow keys always change the focus to another UIElement.

如何禁用由焦点变化箭头键,让他们改变摄像机的位置呢?

How can I disable focus changes by the arrow keys and have them change the camera position instead?

推荐答案

它总是很难,而无需一些代码来实际测试来回答,因为如果没有它,我们都只是猜测真的。无论哪种方式,我不能对你的具体情况发表评论,但一般来说,如果我们想从一个事件发生阻止一些预先定义的动作,那么我们通常处理该事件,并设置即办理属性真正

It always difficult to answer without having some code to actually test, because without it, we are just guessing really. Either way, I can't comment on your particular situation, but in general, if we want to stop some pre-defined action from happening in an event, then we typically handle that event and set the e.Handled property to true.

看到,因为你已经要处理的的KeyDown 事件来检测使用箭头键,那么你可以在 e.Handled 属性设置为真正在同一时间。然而,你应该处理 PreviewKeyDown 事件,而不是,因为它发生的的的的KeyDown 事件所以更可能有你想要的效果。尝试是这样的:

Seeing as you already want to handle the KeyDown event to detect the use of the arrow keys, then you could set the e.Handled property to true at the same time. However, you should handle the PreviewKeyDown event instead, because it occurs before the KeyDown event and so is more likely to have the effect that you want. Try something like this:

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Left || e.Key == Key.Right)
    {
        // Move your camera here
        e.Handled = true;
    }
}

这篇关于如何禁用按箭头键更改焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆