How to disable focus changes by arrow keys

后端 未结 3 691
栀梦
栀梦 2021-02-14 22:28

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

The viewport shows a 3D scene and I

3条回答
  •  暖寄归人
    2021-02-14 22:49

    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.

    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;
        }
    }
    

提交回复
热议问题