Note that there are some explanatory texts on larger screens.

plurals
  1. POKeyDown Event on a C# WPF Window Not Working
    primarykey
    data
    text
    <p>I'm trying to receive key strokes in a Window (Not a Form). I receive event, until a button is pressed. After that no matter what I do, the key down event doesn't fire anymore. Is there any solution? I have searched for it, seems like every one is suggesting a solution using</p> <pre><code>this.KeyPreview = true; </code></pre> <p>but this can't work here, as Window doesn't have such an attribute. Helps much appreciated. I have already set all the children Focusable to False, and Window is set to be focusable. But this hasn't helped.</p> <p>The XAML is</p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="463" Width="726" AllowsTransparency="False" PreviewKeyDown="Window_PreviewKeyDown" KeyDown="Window_KeyDown_1" WindowStartupLocation="CenterScreen" Focusable="True"&gt; &lt;Window.Background&gt; &lt;RadialGradientBrush&gt; &lt;GradientStop Color="#FF3EB5FF" Offset="1" /&gt; &lt;GradientStop Color="White" Offset="0" /&gt; &lt;/RadialGradientBrush&gt; &lt;/Window.Background&gt; &lt;Grid Name="grid1"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="434*" /&gt; &lt;ColumnDefinition Width="270*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid Margin="10,10,0,12" Name="EquiGrid" Focusable="False"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="2*" /&gt; &lt;ColumnDefinition Width="198*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Image Grid.ColumnSpan="2" Name="EquiImage" Stretch="Uniform" Margin="0,0,0,6" /&gt; &lt;Grid Grid.Column="1" Height="100" HorizontalAlignment="Left" Margin="489,90,0,0" Name="grid2" VerticalAlignment="Top" Width="200" /&gt; &lt;/Grid&gt; &lt;Label Content="Label" Height="28" Margin="14,12,12,0" Name="longLabel" VerticalAlignment="Top" Grid.Column="1" OpacityMask="White" BorderBrush="Red" Focusable="False"&gt; &lt;Label.Background&gt; &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt; &lt;GradientStop Color="White" Offset="0.025" /&gt; &lt;GradientStop Color="#FFDBDBDB" Offset="1" /&gt; &lt;/LinearGradientBrush&gt; &lt;/Label.Background&gt; &lt;/Label&gt; &lt;Label Content="Label" Height="28" Margin="14,46,12,0" Name="latLabel" VerticalAlignment="Top" Grid.Column="1" Focusable="False"&gt; &lt;Label.Background&gt; &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt; &lt;GradientStop Color="White" Offset="0.025" /&gt; &lt;GradientStop Color="#FFDBDBDB" Offset="1" /&gt; &lt;/LinearGradientBrush&gt; &lt;/Label.Background&gt; &lt;/Label&gt; &lt;TextBlock Margin="14,80,12,54" Name="descriptionText" Padding="10" Text="" Grid.Column="1" Focusable="False"&gt;&lt;TextBlock.Background&gt;&lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;&lt;GradientStop Color="White" Offset="0.025" /&gt;&lt;GradientStop Color="#FFDBDBDB" Offset="1" /&gt;&lt;/LinearGradientBrush&gt;&lt;/TextBlock.Background&gt;&lt;/TextBlock&gt; &lt;Button Content="Load Image" Grid.Column="1" Margin="14,0,0,12" Name="button1" Height="36" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="124" Click="button1_Click" Focusable="False" /&gt; &lt;Button Content="Load XML" Grid.Column="1" Margin="0,0,12,12" Name="button2" Height="36" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="114" Click="button2_Click" Focusable="False" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>and the handler:</p> <pre><code> private void Window_KeyDown_1(object sender, System.Windows.Input.KeyEventArgs e) { Console.WriteLine("k"); } </code></pre> <p>I have also found out that the problem is in the focus. The children, although set to be not focusable, steal the focus on the window and stop the event firing.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload