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>
相关推荐
BD_Marathon13 分钟前
sbt 编译打包 scala
开发语言·后端·scala
雾岛听蓝21 分钟前
C++ 入门核心知识点(从 C 过渡到 C++ 基础)
开发语言·c++·经验分享·visual studio
7***374541 分钟前
Java设计模式之工厂
java·开发语言·设计模式
上不如老下不如小1 小时前
2025年第七届全国高校计算机能力挑战赛初赛 Python组 编程题汇总
开发语言·python·算法
程序员小白条1 小时前
你面试时吹过最大的牛是什么?
java·开发语言·数据库·阿里云·面试·职场和发展·毕设
小年糕是糕手1 小时前
【C++】类和对象(二) -- 构造函数、析构函数
java·c语言·开发语言·数据结构·c++·算法·leetcode
豐儀麟阁贵1 小时前
8.2异常的抛出与捕捉
java·开发语言·python
权泽谦1 小时前
PHP 版羊了个羊完整开发实战:逻辑解析 + 三消算法 + 全套接口(附源码)
开发语言·php
程序员西西1 小时前
SpringBoot无感刷新Token实战指南
java·开发语言·前端·后端·计算机·程序员
Coding_Doggy2 小时前
链盾shieldchiain | 团队功能、邀请成员、权限修改、移除成员、SpringSecurity、RBAC权限控制
java·开发语言·数据库