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

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

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

使其变成灰色

C#代码:

cs 复制代码
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代码:

XML 复制代码
<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>
相关推荐
沐知全栈开发2 小时前
HTML DOM 访问
开发语言
脑袋大大的3 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
二进制person4 小时前
Java SE--方法的使用
java·开发语言·算法
OneQ6664 小时前
C++讲解---创建日期类
开发语言·c++·算法
码农不惑5 小时前
2025.06.27-14.44 C语言开发:Onvif(二)
c语言·开发语言
Coding小公仔6 小时前
C++ bitset 模板类
开发语言·c++
小赖同学啊7 小时前
物联网数据安全区块链服务
开发语言·python·区块链
shimly1234567 小时前
bash 脚本比较 100 个程序运行时间,精确到毫秒,脚本
开发语言·chrome·bash
葬歌倾城7 小时前
JSON的缩进格式方式和紧凑格式方式
c#·json