在UWP应用程序中模拟Keyup和keydown之类的键盘事件
问题描述:
如何在UWP应用程序中模拟Keyup和keydown之类的键盘事件。我不想使用手动键盘,而是使用我自己的键盘,但是我想现在添加键盘事件它。 我在uwp中找不到任何方法,并且SendKeys.Send()方法也不适用于UWP。在UWP应用程序中模拟Keyup和keydown之类的键盘事件
答
我不想使用手动键盘,而是我做了我自己的键盘,但我想现在添加键盘事件。我在uwp中找不到任何方法,并且SendKeys.Send()方法也不适用于UWP。
为了您的要求,您可以创建自定义事件处理程序来模拟键盘KeyDown
,在您的自定义用户控件KeyUp
事件。关键是何时调用您的自定义事件处理程序。我已经创建了一个简单的示例,您可以参考。
CustomKeyBoard.xaml
<UserControl
x:Class="UIKeyBoardTest.CustomKeyBoard"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UIKeyBoardTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:control ="using:Microsoft.Toolkit.Uwp.UI.Controls"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<GridView VerticalAlignment="Center" HorizontalAlignment="Center" ItemClick="GridView_ItemClick" IsItemClickEnabled="True">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid MaximumRowsOrColumns="3" Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Background="White"
BorderBrush="Black"
BorderThickness="1">
<TextBlock Text="{Binding }" HorizontalAlignment="Center" TextAlignment="Center" VerticalAlignment="Center" FontSize="40" Width="55" />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<x:String>1</x:String>
<x:String>2</x:String>
<x:String>3</x:String>
<x:String>4</x:String>
<x:String>5</x:String>
<x:String>6</x:String>
<x:String>7</x:String>
<x:String>8</x:String>
<x:String>9</x:String>
</GridView>
</Grid>
</UserControl>
CustomKeyBoard.xaml.cs
public sealed partial class CustomKeyBoard : UserControl
{
public delegate void BoilerLogHandler(string value);
public event BoilerLogHandler BoilerEventLog;
public CustomKeyBoard()
{
this.InitializeComponent();
}
private void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
if (BoilerEventLog != null)
{
BoilerEventLog(e.ClickedItem.ToString());
}
}
}
用法
<TextBox x:Name="box" PlaceholderText="Please input something" Margin="0,50,0,60" />
<local:CustomKeyBoard
HorizontalAlignment="Center"
VerticalAlignment="Center"
BoilerEventLog="CustomKeyBoard_BoilerEventLog" />
private void CustomKeyBoard_BoilerEventLog(string value)
{
box.Text = value;
}
你是什么意思的“做我自己的键盘”? –
我的意思是说我用字母和数字编程创建了简单的键盘。 – user3309634
那你为什么不使用该控件的事件?将代码附加到问题 –