简体   繁体   English

在 UWP 应用程序中模拟 Keyup 和 keydown 等键盘事件

[英]simulate keyboard events like Keyup and keydown in UWP app

How to simulate keyboard events like Keyup and keydown in UWP app.I do not want to use manual keyboard instead i have made my own keyboard but i want to add now keyboard events in it.如何在 UWP 应用程序中模拟 Keyup 和 keydown 等键盘事件。我不想使用手动键盘,而是制作了自己的键盘,但我现在想在其中添加键盘事件。 I did not find any way in uwp and SendKeys.Send() method is also not available for UWP.我在 uwp 中没有找到任何方法,SendKeys.Send() 方法也不适用于 UWP。

You can use Input Injection .您可以使用输入注入 The documentation has a complicated example, but it basically breaks down into declaring a capability and writing something like the following code:该文档有一个复杂的示例,但它基本上分解为声明功能和编写类似以下代码的内容:

        _inputInjector = InputInjector.TryCreate();
        var input = new InjectedInputKeyboardInfo();
        input.VirtualKey = value;
        input.KeyOptions = InjectedInputKeyOptions.None;
        _inputInjector.InjectKeyboardInput(new[] { input });

Where value is either a VirtualKey or a char that can be cast to one.其中valueVirtualKey或可以转换为 one 的char More details in the following answer .以下答案中的更多详细信息。

I do not want to use manual keyboard instead i have made my own keyboard but i want to add now keyboard events in it. 我不想使用手动键盘,但是我已经制作了自己的键盘,但是现在我想在其中添加键盘事件。 I did not find any way in uwp and SendKeys.Send() method is also not available for UWP. 我在uwp中找不到任何方式,SendKeys.Send()方法也不适用于UWP。

For your requirement, you could create custom event handler to simulate keyboard KeyDown , KeyUp event in your custom User Control. 根据您的要求,您可以创建自定义事件处理程序,以在自定义用户控件中模拟键盘KeyDownKeyUp事件。 The key point is when to invoke your custom event handler. 关键是何时调用自定义事件处理程序。 I have create a sample simple you could have a reference. 我创建了一个简单的示例,您可以参考。

CustomKeyBoard.xaml 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 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());
        }
    }
}

Usage 用法

 <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;
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM