UserControl.KeyDown Not Firing?
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?
... 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?
Adding a Button to the UserControl allows UserControl.KeyDown to work -- after the button is focused.
-
-
sfernandez
Site Admin
- Posts: 3197
- Joined:
Re: UserControl.KeyDown Not Firing?
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?
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?
I'm wondering if this behavior is specific to how UserControl vs Window is implemented?
(This works in WPF)
MainWindow.xaml
MainWindow.xaml.cs
(This does not work in Noesis)
MainWindow.xaml
MainWindow.xaml.cs
(This works in WPF)
MainWindow.xaml
Code: Select all
<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>
Code: Select all
using System.Diagnostics;
using System.Windows;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.KeyDown += (sender, e) => Debug.WriteLine(e.Key.ToString()); // works!
}
}
MainWindow.xaml
Code: Select all
<UserControl
x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid Background="Red"/>
</UserControl>
Code: Select all
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!
}
}
-
-
sfernandez
Site Admin
- Posts: 3197
- Joined:
Re: UserControl.KeyDown Not Firing?
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:
You can make it work by setting Focusable="True" in the UserControl:
Code: Select all
<UserControl
x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Focusable="True">
<Grid Background="Red"/>
</UserControl>
Code: Select all
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?
Thank you, sir!
-
-
sfernandez
Site Admin
- Posts: 3197
- Joined:
Re: UserControl.KeyDown Not Firing?
You're welcome, marking this as solved.
Who is online
Users browsing this forum: Ahrefs [Bot] and 13 guests