【问题标题】:Custom In App Keyboard in UWPUWP 中的自定义应用内键盘
【发布时间】:2016-12-18 08:35:49
【问题描述】:

我想在 UWP 中创建自定义屏幕键盘。它需要成为应用程序的一部分,因为它将用于大型桌面或板设备,因此完全控制键盘位置非常重要(在桌面上旋转)。

在 WPF 中,我已经通过创建带有 Target 属性的键盘控件来制作这样的自定义键盘。当按下某个键时,它会使用 UIElement.RaiseEvent(...) 在目标上引发 KeyEvent 或 TextComposition。 但是在 UWP 中,没有 RaiseEvent 函数,似乎没有办法为开发者引发路由事件。

我想使用本机文本事件(KeyDown 事件、TextComposition 事件等),因此手动编辑 TextBox 的 Text 属性(like this one)的解决方案是不可接受的。

This page 解释了如何创建一个监听文本服务框架的控件。我认为一种解决方案是创建自定义文本服务,但我没有找到任何相关文档。

【问题讨论】:

  • 您找到解决方案了吗?
  • @DanNeely 不抱歉。暂时什么都没有。我们目前不开发 UWP。

标签: c# uwp windows-10-universal uwp-xaml


【解决方案1】:

使用Windows.UI.Input.Preview.Injection 命名空间中的类和受限的inputInjectionBrokered 功能,您至少可以完成部分工作。

这适用于 KeyUpKeyDownPreviewKeyUpPreviewKeyDown 事件,只要您不需要将击键发送到应用程序之外的任何内容。

非拉丁脚本超出了我的工作范围,所以我不知道它是否可以扩展到 IME,从而生成 TextComposition 事件。

Martin Zikmund 演示了这样做here,并在github 上提供了示例解决方案。

关键点是您需要编辑您的Package.appxmanifest 文件(作为代码而不是通过设计器)以包含:

<Package>
    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    IgnorableNamespaces="rescap"
</Package>

<Capabilities>
    <rescap:Capability Name="inputInjectionBrokered" />
<Capabilities>

您可以通过以下方式模拟打字并引发原生按键事件:

private async void TypeText()
{
    Input.Focus(FocusState.Programmatic);
    //we must yield the UI thread so that focus can be acquired
    await Task.Delay(100); 

    InputInjector inputInjector = InputInjector.TryCreate();
    foreach (var letter in "hello")
    {
        var info = new InjectedInputKeyboardInfo();
        info.VirtualKey = (ushort)((VirtualKey)Enum.Parse(typeof(VirtualKey), 
                                   letter.ToString(), true));
        inputInjector.InjectKeyboardInput(new[] { info });

        //and generate the key up event next, doing it this way avoids
        //the need for a delay like in Martin's sample code.
        info.KeyOptions = InjectedInputKeyOptions.KeyUp;
        inputInjector.InjectKeyboardInput(new[] { info });
    }
}

【讨论】:

  • 谢谢!最后是一个遮阳篷。我稍后会对此进行测试。
  • @Gael 如果您的测试中有任何有趣的事情出现,请在此处跟进。我还没有超越这个原型来证明当应用程序达到这一点时我将能够制作自定义键盘。不过,在达到这一点之前,我还有很多其他事情需要完成。
  • 您可以轻松地将其扩展到非拉丁脚本:stackoverflow.com/a/59755604/130060
猜你喜欢
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 2015-07-19
相关资源
最近更新 更多