2016-02-12 78 views
-5

我正在製作遊戲「2048」。我如何等待用戶按下其中一個箭頭,然後確定他按下了哪個箭頭。 謝謝閱讀密鑰Wpf

  bool game = true; 

      while (Game) 
      { 
      //Read The Key Here 
      if()//The Key is Up 
      { 

      } 
      else if()//The Key is down 
      { 

      } 
      } 
+0

查看如何訂閱KeyEventArgs ...瞭解如何在哲學上工作,您可以將其應用於其他語言.. – visc

回答

3

如果你有機會獲得XAML,嘗試OnKeyDownHandler方法。

XAML:

<StackPanel> 
    <TextBlock Width="300" Height="20"> 
    Type some text into the TextBox and press the Enter key. 
    </TextBlock> 
    <TextBox Width="300" Height="30" Name="textBox1" 
      KeyDown="OnKeyDownHandler"/> 
    <TextBlock Width="300" Height="100" Name="textBlock1"/> 
</StackPanel> 

C#:

private void OnKeyDownHandler(object sender, KeyEventArgs e) 
{ 
    if (e.Key == Key.Return) 
    { 
     textBlock1.Text = "You Entered: " + textBox1.Text; 
    } 
} 

Mark W's答案提供一個類似的例子。

+0

謝謝,它的工作原理 – tomer