2012-04-03 15 views
3

キーストロークをフォーム(フォームではありません)で受け取ろうとしています。ボタンが押されるまで、私はイベントを受け取ります。その後、私が何をしても、キーダウンイベントはもはや発生しません。解決策はありますか?私は、一人一人がC#WPFウィンドウのKeyDownイベントが機能しない

this.KeyPreview = true; 

を使用してソリューションを提案しているが、ウィンドウは、そのような属性を持っていないとして、これは、ここでは動作しないことができるように思える、それを検索しました。感謝します。私はすでにすべての子をFocusableにFalseに設定しており、Windowはフォーカス可能に設定されています。しかし、これは助けにはなりません。私が問題に焦点が合っていることも判明している

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

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> 

とハンドラです。子供たちは、フォーカスできないように設定されていますが、窓に焦点を当ててイベントの発射を止めます。

+0

uがあなたのアプリケーションの外部キーストロークを意味ですか? – PresleyDias

+0

いいえ、内部にあります。それはかなり簡単なことだと思われる!しかし、動作しません。 –

+0

よろしいですか、コードを入力できますか?それはどのキーですか? – PresleyDias

答えて

2

KeyDownイベントを使用してみてください。

分離コードで

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

<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> 
+0

ありがとうございます。しかし、私のコメントを見れば、まさに私がやったことです。 –

+0

私は気づいた。あなたは矢印キーの使用について言及することを忘れてしまいます。 –

+0

更新された回答を参照 –

関連する問題