简体   繁体   中英

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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