feat: WPF路由事件-5-处理按键事件

This commit is contained in:
Bunny 2025-01-02 18:49:03 +08:00
parent 7368afd24c
commit b42604860a
7 changed files with 115 additions and 0 deletions

View File

@ -60,6 +60,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF路由事件-3-冒泡路
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF路由事件-4-生命周期", "WPF路由事件-4-生命周期\WPF路由事件-4-生命周期.csproj", "{B5A965AA-9C4C-4EB3-8515-DF03F948EC14}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF路由事件-4-生命周期", "WPF路由事件-4-生命周期\WPF路由事件-4-生命周期.csproj", "{B5A965AA-9C4C-4EB3-8515-DF03F948EC14}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF路由事件-5-处理按键事件", "WPF路由事件-5-处理按键事件\WPF路由事件-5-处理按键事件.csproj", "{DD14128A-A563-48B8-9ED2-201C0553B8D2}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -186,5 +188,9 @@ Global
{B5A965AA-9C4C-4EB3-8515-DF03F948EC14}.Debug|Any CPU.Build.0 = Debug|Any CPU {B5A965AA-9C4C-4EB3-8515-DF03F948EC14}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5A965AA-9C4C-4EB3-8515-DF03F948EC14}.Release|Any CPU.ActiveCfg = Release|Any CPU {B5A965AA-9C4C-4EB3-8515-DF03F948EC14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5A965AA-9C4C-4EB3-8515-DF03F948EC14}.Release|Any CPU.Build.0 = Release|Any CPU {B5A965AA-9C4C-4EB3-8515-DF03F948EC14}.Release|Any CPU.Build.0 = Release|Any CPU
{DD14128A-A563-48B8-9ED2-201C0553B8D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD14128A-A563-48B8-9ED2-201C0553B8D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD14128A-A563-48B8-9ED2-201C0553B8D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD14128A-A563-48B8-9ED2-201C0553B8D2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -0,0 +1,9 @@
<Application x:Class="WPF路由事件_5_处理按键事件.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF路由事件_5_处理按键事件"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

View File

@ -0,0 +1,12 @@
using System.Configuration;
using System.Data;
using System.Windows;
namespace WPF路由事件_5_处理按键事件;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

View File

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

View File

@ -0,0 +1,33 @@
<Window x:Class="WPF路由事件_5_处理按键事件.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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Margin="3" HorizontalAlignment="Left">Home:</Label>
<TextBox Grid.Row="0" Grid.Column="1" Margin="3" Padding="2" Height="Auto"
PreviewKeyDown="UIElement_OnPreviewKeyDown" PreviewTextInput="UIElement_OnPreviewTextInput"/>
</Grid>
<ListBox Grid.Row="1" Name="ListBox" />
<StackPanel Grid.Row="2">
<CheckBox Name="CheckBox">忽略长按key</CheckBox>
<Button HorizontalAlignment="Right" Click="ButtonBase_OnClick"> 清除列表</Button>
</StackPanel>
</Grid>
</Window>

View File

@ -0,0 +1,33 @@
using System.Windows;
using System.Windows.Input;
namespace WPF路由事件_5_处理按键事件;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void UIElement_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
// 忽略键盘长按结果
if ((bool)CheckBox.IsChecked! && e.IsRepeat) return;
ListBox.Items.Add($"Event:{e.RoutedEvent} Key: {e.Key}");
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
ListBox.Items.Clear();
}
private void UIElement_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
Console.WriteLine(e.Text);
}
}

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>WPF路由事件_5_处理按键事件</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>