User avatar
stonstad
Topic Author
Posts: 241
Joined: 06 Jun 2016, 18:14
Location: Lesser Magellanic Cloud
Contact:

UserControl.KeyDown Not Firing?

08 Mar 2019, 04:26

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?
 
User avatar
stonstad
Topic Author
Posts: 241
Joined: 06 Jun 2016, 18:14
Location: Lesser Magellanic Cloud
Contact:

Re: UserControl.KeyDown Not Firing?

08 Mar 2019, 04:46

Adding a Button to the UserControl allows UserControl.KeyDown to work -- after the button is focused.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: UserControl.KeyDown Not Firing?

08 Mar 2019, 11:26

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?
 
User avatar
stonstad
Topic Author
Posts: 241
Joined: 06 Jun 2016, 18:14
Location: Lesser Magellanic Cloud
Contact:

Re: UserControl.KeyDown Not Firing?

10 Mar 2019, 17:11

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!
        }
}
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: UserControl.KeyDown Not Firing?

10 Mar 2019, 18:49

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");
    }
}
 
User avatar
stonstad
Topic Author
Posts: 241
Joined: 06 Jun 2016, 18:14
Location: Lesser Magellanic Cloud
Contact:

Re: UserControl.KeyDown Not Firing?

10 Mar 2019, 19:42

Thank you, sir!
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: UserControl.KeyDown Not Firing?

10 Mar 2019, 20:04

You're welcome, marking this as solved.

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 7 guests