34.键盘1 C#例子 WPF例子

发布于:2025-02-11 ⋅ 阅读:(66) ⋅ 点赞:(0)

这是一个键盘,按下哪个键,哪个键就会变灰色

简单来说就是按键输入事件,根据不同输入跳转到不同的按钮,然后公用一个按钮事件。

使其变成灰色

C#代码:

using System.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace practice
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // 这里是按钮点击时要执行的逻辑
            Button temp = (Button)sender;

            // 创建一个新的SolidColorBrush对象,设置为你想要的颜色
            SolidColorBrush newBackground = new SolidColorBrush(Colors.Gray);
            temp.Background = newBackground;
        }
        
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.W:
                    // 处理 W 键

                    Button_Click(buttonW, null);
                    break;
                case Key.A:
                    // 处理 A 键

                    Button_Click(buttonA, null);
                    break;
                case Key.S:
                    // 处理 S 键

                    Button_Click(buttonS, null);
                    break;
                case Key.D:
                    // 处理 D 键

                    Button_Click(buttonD, null);
                    break;
                // ... 为其他键添加 case
                default:
                    // 处理未知或未指定的键
                    break;
            }
        }
    }



    
}

XAML代码:

<Window x:Class="practice.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:practice"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        KeyDown="Window_KeyDown">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>


        </Grid.RowDefinitions>
        <Button x:Name="buttonW" FontSize="72" Content="W" Grid.Row="0" Grid.Column="1" Foreground="White"  Background="LightGreen" />
        <Button x:Name="buttonA" FontSize="72" Content="A" Grid.Row="1" Grid.Column="0" Foreground="White"  Background="LightGreen"/>
        <Button x:Name="buttonS" FontSize="72" Content="S" Grid.Row="1" Grid.Column="1" Foreground="White"  Background="LightGreen"/>
        <Button x:Name="buttonD" FontSize="72" Content="D" Grid.Row="1" Grid.Column="2" Foreground="White"  Background="LightGreen"/>
    </Grid>
</Window>




网站公告

今日签到

点亮在社区的每一天
去签到