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>
相关推荐
梵得儿SHI7 分钟前
Java 反射机制深度剖析:性能与安全性的那些坑
java·开发语言·安全·反射·动态代理·性能·反射机制
fsnine12 分钟前
Python图形化界面——pyqt5教程
开发语言·python·qt
嵌入式-老费21 分钟前
Easyx图形库应用(和lua结合使用)
开发语言·lua
AsiaLYF22 分钟前
kotlin中MutableStateFlow和MutableSharedFlow的区别是什么?
android·开发语言·kotlin
Asuncion00733 分钟前
Docker核心揭秘:轻量级虚拟化的革命
服务器·开发语言·docker·云原生
gc_22991 小时前
C#编写的WebApi接口直接返回byte数组引发的问题
c#·byte数组
深思慎考1 小时前
RabbitMQ 入门:基于 AMQP-CPP 的 C++ 实践指南与二次封装
开发语言·c++·分布式·rabbitmq·api
catchadmin1 小时前
PHP8.5 的新 URI 扩展
开发语言·后端·php
似水流年 光阴已逝2 小时前
从Excel姓名匹配案例学Python:由点及面的系统化学习指南
开发语言·python·excel
重生之我要当java大帝2 小时前
java微服务-尚医通-管理平台前端搭建-医院设置管理-4
java·开发语言·前端