3

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

I added a KeyDownEvent to a content control in xaml:

 <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)

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

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?

Dragon
  • 33
  • 6

1 Answers1

1

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)

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

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.

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);
    }
}
Jehof
  • 34,674
  • 10
  • 123
  • 155