【问题标题】:simulate keyboard events like Keyup and keydown in UWP app在 UWP 应用中模拟 Keyup 和 keydown 等键盘事件
【发布时间】:2022-03-18 23:54:38
【问题描述】:

如何在 UWP 应用程序中模拟 Keyup 和 keydown 等键盘事件。我不想使用手动键盘,而是制作了自己的键盘,但我想在其中添加键盘事件。 我在 uwp 中没有找到任何方法,并且 SendKeys.Send() 方法也不适用于 UWP。

【问题讨论】:

  • “自制键盘”是什么意思?
  • 我的意思是说我用字母和数字以编程方式创建了简单的键盘。
  • 那你为什么不使用那个控件的事件呢?在问题中附上您的代码
  • 我刚刚使用 XAML 中的按钮控件创建了键盘,它的实现是在 C# 中。 private void Letter_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)//按钮点击事件{键盘实现逻辑}
  • 你想做什么?

标签: c# uwp


【解决方案1】:

我不想使用手动键盘,而是自己制作了键盘,但我现在想在其中添加键盘事件。我在 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;
}

【讨论】:

  • 有没有办法触发 Windows 快捷方式,例如Windows 键与另一个键?
  • 这并没有真正回答如何模拟键盘事件的问题。例如,我的应用程序遇到了同样的问题,因为在字段中输入文本具有需要重新编程的特殊功能(如文本框选择索引行为和 Web 视图的关键事件检测)。
【解决方案2】:

您可以使用Input Injection。该文档有一个复杂的示例,但它基本上分解为声明一个功能并编写类似于以下代码的内容:

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

其中valueVirtualKeychar,可以转换为一个。更多详情the following answer

【讨论】:

    猜你喜欢
    • 2017-10-15
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2014-06-12
    • 2012-04-14
    • 1970-01-01
    相关资源
    最近更新 更多