键事件不被箭头键激活

时间:2022-05-05 17:26:21

first of I'm working with WPF and i'm new to it.

首先我和WPF合作,我是新手。

I added a KeyDownEvent to a content control in xaml:

我在xaml的内容控件中添加了一个KeyDownEvent:

 <ContentControl Grid.Row="1" Name="dockingContentControl" KeyDown="dockingContentControl_KeyDown"/>

I know, probably there is a better way to add a KeyDown event. (Would also help)

我知道,可能有更好的方法来添加一个KeyDown事件。(也会有所帮助)

But the event is working. So I used a simple Console.WriteLine command like so:

但事情正在发生变化。所以我用了一个简单的控制台。WriteLine命令如下所示:

private void dockingContentControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    Console.WriteLine(e.Key);
}

and it showed me the buttons I pressed.

它显示了我按下的按钮。

But somehow it did not trigger with the Arrowkeys.

但不知何故,它并没有用箭来触发。

I don't know why all other (I think all, didn't test all) buttons are working, but the Arrowkeys not.

我不知道为什么其他的(我认为所有的,没有测试所有的)按钮都能工作,但是箭头键不能。

Did I miss something or did I have to add some kind of extra code.

是我漏掉了什么,还是我需要添加一些额外的代码。

Commend: All other buttons are recognized, with the exception of the fn-button. The background chart is moving, could it be that the chart has the focus and doesn't "forward" the input?

推荐:除了fn-button之外,所有其他按钮都是可识别的。背景图正在移动,可能是图表有焦点而不是“正向”输入吗?

1 个解决方案

#1


0  

It depends what Controls you have placed inside your ContentControl and what control has the focus. Some keys like TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses. The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown (for a Button as an example)

这取决于你在ContentControl中放置了什么控件,以及控件的焦点。有些键(如TAB、RETURN、ESC和arrow)通常会被某些控件忽略,因为它们不被视为输入键。箭头键被认为是导航键,按下这些键通常不会按下键(例如按钮)

You can try to use the PreviewKeyDown-Event for this case.

对于这种情况,可以尝试使用previewkeydown事件。

If I use your provided code in a minimal solution I receive the KeyDown-Events for all keys, but it is required that the ContentControl has the focus.

如果我在一个最小的解决方案中使用您提供的代码,那么我将为所有的键接收关键事件,但是需要对ContentControl进行关注。

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        dockingContentControl.Focus();
    }

    private void dockingContentControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        Console.WriteLine(e.Key);
    }
}

#1


0  

It depends what Controls you have placed inside your ContentControl and what control has the focus. Some keys like TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses. The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown (for a Button as an example)

这取决于你在ContentControl中放置了什么控件,以及控件的焦点。有些键(如TAB、RETURN、ESC和arrow)通常会被某些控件忽略,因为它们不被视为输入键。箭头键被认为是导航键,按下这些键通常不会按下键(例如按钮)

You can try to use the PreviewKeyDown-Event for this case.

对于这种情况,可以尝试使用previewkeydown事件。

If I use your provided code in a minimal solution I receive the KeyDown-Events for all keys, but it is required that the ContentControl has the focus.

如果我在一个最小的解决方案中使用您提供的代码,那么我将为所有的键接收关键事件,但是需要对ContentControl进行关注。

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        dockingContentControl.Focus();
    }

    private void dockingContentControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        Console.WriteLine(e.Key);
    }
}