Page 1 of 1

UserControl.KeyDown Not Firing?

Posted: 08 Mar 2019, 04:26
by stonstad
I tested in WPF and confirmed this works. Not sure if it is user error, but in Noesis this doesn't fire.

... inside a user control

this.MouseDown += (sender, e) => UnityEngine.Debug.Log("MouseDown"); // works
this.KeyDown += (sender, e) => UnityEngine.Debug.Log("KeyDown"); /does not work


Separately, is there an equivalent to Window.Current.CoreWindow.KeyDown?

Re: UserControl.KeyDown Not Firing?

Posted: 08 Mar 2019, 04:46
by stonstad
Adding a Button to the UserControl allows UserControl.KeyDown to work -- after the button is focused.

Re: UserControl.KeyDown Not Firing?

Posted: 08 Mar 2019, 11:26
by sfernandez
KeyDown is a routed event that is started from the element that has focus, so to be able to reach the UserControl registered handler it should be raised from an element inside the UserControl or the control itself. That is why it worked once the button inside had focus.

In 2.2 version we fixed a couple of problems related to how focus was set initialily on View creation or when focused elements where removed/hidden. Maybe there is still something wrong, could you please elaborate a bit the exact scenario where it didn't work? it was just when started playing? was the UserControl the root of the View? was it focusable?

Re: UserControl.KeyDown Not Firing?

Posted: 10 Mar 2019, 17:11
by stonstad
I'm wondering if this behavior is specific to how UserControl vs Window is implemented?

(This works in WPF)

MainWindow.xaml
<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid Background="Red"/>
</Window>
MainWindow.xaml.cs
using System.Diagnostics;
using System.Windows;
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.KeyDown += (sender, e) => Debug.WriteLine(e.Key.ToString()); // works!
    }
}
(This does not work in Noesis)

MainWindow.xaml
<UserControl
    x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Grid Background="Red"/>
</UserControl>
MainWindow.xaml.cs
    public partial class MainWindow: UserControl
    {
        public MainWindow()
        {
            this.Initialized += OnInitialized;
            this.InitializeComponent();
        }

        private void InitializeComponent()
        {
            GUI.LoadComponent(this, "MainWindow.xaml");
            FrameworkElement root = this.Content as FrameworkElement;
            this.KeyDown += (sender, e) => UnityEngine.Debug.Log(e.Key); // does not fire!
        }
}

Re: UserControl.KeyDown Not Firing?

Posted: 10 Mar 2019, 18:49
by sfernandez
That's it, a Window is a control that is focusable by default, but UserControl is not.
You can make it work by setting Focusable="True" in the UserControl:
<UserControl
    x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    Focusable="True">
    <Grid Background="Red"/>
</UserControl>
public partial class MainWindow: UserControl
{
    public MainWindow()
    {
        this.InitializeComponent();
        this.KeyDown += (sender, e) => UnityEngine.Debug.Log(e.Key);
    }

    private void InitializeComponent()
    {
        GUI.LoadComponent(this, "MainWindow.xaml");
    }
}

Re: UserControl.KeyDown Not Firing?

Posted: 10 Mar 2019, 19:42
by stonstad
Thank you, sir!

Re: UserControl.KeyDown Not Firing?

Posted: 10 Mar 2019, 20:04
by sfernandez
You're welcome, marking this as solved.