-6

I am working on the game "2048". How can I wait till the user pressed one of the arrows and than identify what arrow did he pressed. Thank You

            bool game = true;

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

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

            }
            }
tomer
  • 23
  • 6
  • Look up how to subscribe to KeyEventArgs... Learn how this works philosophically and you can apply it in other languages.. – visc Feb 12 '16 at 20:41

1 Answers1

3

If you have access to XAML, try the OnKeyDownHandler method.

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 answer provides a similar example.

Community
  • 1
  • 1
Eric S
  • 1,336
  • 15
  • 20