简体   繁体   English

按下键时激活按钮C#Universal App

[英]Activate button when key is pressed C# Universal App

I develop a game on Windows Universal App in C#. 我使用C#在Windows Universal App上开发了一个游戏。 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 ? 我的问题是:如何也使用键盘箭头激活我的功能Move()?

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. 您可以使用KeyDown激活键盘。

The xaml is XAML是

<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. 它将上下移动btn使用按钮。

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. 您应该使用Grid.KeyDown来获取密钥并使btn移动。

If have no notion of the code ,please talk me. 如果没有代码概念,请谈谈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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