K4tn1x
K4tn1x

Reputation: 133

Activate button when key is pressed C# Universal App

I develop a game on Windows Universal App in C#. I have on my interface four buttons (left, right, up, down) to move the character on my map.

My question is : how to activate my function Move() with the keyboard arrows too ?

I tried a lot of solution from the web to get keys are pressed but most oh them concern only input forms...

Upvotes: 0

Views: 133

Answers (1)

lindexi
lindexi

Reputation: 4327

You can use KeyDown to get the keyboard active.

The xaml is

<Page
    x:Class="ktbkwbconcern.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ktbkwbconcern"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" KeyDown="Grid_OnKeyDown">
        <Button x:Name="btn" Content="K">
            <Button.RenderTransform>
                <CompositeTransform></CompositeTransform>
            </Button.RenderTransform>
        </Button>
        <Grid VerticalAlignment="Bottom">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Row="0" Grid.Column="0" Content="left" Click="Button_OnClick"></Button>
            <Button Grid.Row="1" Grid.Column="0" Content="up" Click="Button_OnClick"></Button>
            <Button Grid.Row="0" Grid.Column="1" Content="down" Click="Button_OnClick"></Button>
            <Button Grid.Row="1" Grid.Column="1" Content="right" Click="Button_OnClick"></Button>
        </Grid>
    </Grid>
</Page>

It will move the btn use button up and down.

And you should write code :

   private void Grid_OnKeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == VirtualKey.Left)
        {
            Move(-1, 0);
        }
        else if (e.Key == VirtualKey.Right)
        {
            Move(1, 0);
        }
        else if (e.Key == VirtualKey.Up)
        {
            Move(0, -1);
        }
        else if (e.Key == VirtualKey.Down)
        {
            Move(0, 1);
        }
    }

    private void Move(int x, int y)
    {
        var temp = btn.RenderTransform as CompositeTransform;
        temp.TranslateX += x;
        temp.TranslateY += y;
    }

    private void Button_OnClick(object sender, RoutedEventArgs e)
    {
        var b = sender as Button;
        if (b != null)
        {
            string str = b.Content as string;
            if (str == "up")
            {
                Move(0, -1);
            }
            else if (str == "down")
            {
                Move(0, 1);
            }
            else if (str == "left")
            {
                Move(-1, 0);
            }
            else if (str == "right")
            {
                Move(1, 0);
            }
        }
    }
}

You should use the Grid.KeyDown to get the key and make btn to move.

If have no notion of the code ,please talk me.

Upvotes: 1

Related Questions