简体   繁体   English

C#WPF窗口上的KeyDown事件不起作用

[英]KeyDown Event on a C# WPF Window Not Working

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 我已经搜索过它,似乎每个人都建议使用解决方案

this.KeyPreview = true;

but this can't work here, as Window doesn't have such an attribute. 但这不能在这里工作,因为Window没有这样的属性。 Helps much appreciated. 非常感谢。 I have already set all the children Focusable to False, and Window is set to be focusable. 我已将所有子项Focusable设置为False,并将Window设置为可聚焦。 But this hasn't helped. 但这没有帮助。

The XAML is XAML是

<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">
    <Window.Background>
        <RadialGradientBrush>
            <GradientStop Color="#FF3EB5FF" Offset="1" />
            <GradientStop Color="White" Offset="0" />
        </RadialGradientBrush>
    </Window.Background>
    <Grid Name="grid1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="434*" />
            <ColumnDefinition Width="270*" />
        </Grid.ColumnDefinitions>
        <Grid Margin="10,10,0,12" Name="EquiGrid"  Focusable="False">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*" />
                <ColumnDefinition Width="198*" />
            </Grid.ColumnDefinitions>
            <Image Grid.ColumnSpan="2" Name="EquiImage" Stretch="Uniform" Margin="0,0,0,6" />
            <Grid Grid.Column="1" Height="100" HorizontalAlignment="Left" Margin="489,90,0,0" Name="grid2" VerticalAlignment="Top" Width="200" />
        </Grid>
        <Label Content="Label" Height="28" Margin="14,12,12,0" Name="longLabel" VerticalAlignment="Top" Grid.Column="1" OpacityMask="White" BorderBrush="Red" Focusable="False">
            <Label.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="White" Offset="0.025" />
                    <GradientStop Color="#FFDBDBDB" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
        </Label>
        <Label Content="Label" Height="28" Margin="14,46,12,0" Name="latLabel" VerticalAlignment="Top" Grid.Column="1" Focusable="False">
            <Label.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="White" Offset="0.025" />
                    <GradientStop Color="#FFDBDBDB" Offset="1" />
                </LinearGradientBrush>
            </Label.Background>
        </Label>
        <TextBlock Margin="14,80,12,54" Name="descriptionText" Padding="10" Text="" Grid.Column="1" Focusable="False"><TextBlock.Background><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="White" Offset="0.025" /><GradientStop Color="#FFDBDBDB" Offset="1" /></LinearGradientBrush></TextBlock.Background></TextBlock>
        <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" />
        <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" />
    </Grid>
</Window>

and the handler: 和处理程序:

 private void Window_KeyDown_1(object sender, System.Windows.Input.KeyEventArgs e)
 { 
       Console.WriteLine("k"); 
 }

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. 这些孩子尽管不能集中注意力,但却将注意力集中在窗户上并阻止事件发生。

Try using the KeyDown event. 尝试使用KeyDown事件。

In codebehind 在代码隐藏中

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Window_KeyDown(object sender, KeyEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("KeyDown");
        System.Diagnostics.Debug.WriteLine(e.Key);
    }

    private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("PreviewKeyDown");
        System.Diagnostics.Debug.WriteLine(e.Key);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Button clicked");
    }
}

XAML XAML

<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="350" Width="525" KeyDown="Window_KeyDown" PreviewKeyDown="Window_PreviewKeyDown">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Grid.Row="0"></TextBox>
        <Button Grid.Row="1" Content="Click me!" Click="Button_Click"></Button>
    </Grid>
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM