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>
相关推荐
听风的码32 分钟前
Vue2封装Axios
开发语言·前端·javascript·vue.js
dotent·1 小时前
一个 WPF 文档和工具窗口布局容器
wpf
c#上位机1 小时前
wpf之ComboBox
wpf
素界UI设计2 小时前
建筑行业变革:用Three.js构建BIM数据可视化孪生平台
开发语言·javascript·信息可视化
王廷胡_白嫖帝2 小时前
Qt个人通讯录项目开发教程 - 从零开始构建联系人管理系统
开发语言·qt
疯狂的代M夫3 小时前
C++对象的内存布局
开发语言·c++
mit6.8243 小时前
Linux下C#项目构建
开发语言·c#
群联云防护小杜3 小时前
从一次 DDoS 的“死亡回放”看现代攻击链的进化
开发语言·python·linq
霸敛4 小时前
好家园房产中介网后台管理完整(python+flask+mysql)
开发语言·python·flask
Momentary_SixthSense4 小时前
RESP协议
java·开发语言·javascript·redis·后端·python·mysql