C# WPF Windows 10 (1803) TouchKeyboard 不可靠问题 (Prism ClickOnce)

标签 c# wpf windows-10 prism clickonce

我正在尝试解决我们的 C# Prism WPF 应用程序中的屏幕键盘问题,目标是通过 ClickOnce 部署的 .Net 4.5.2,在 Windows 10 版本 1803 上运行。

我们的应用程序有一个触发事件的 TimeOut 函数,在该事件中,我想切换键盘的可见性或在键盘打开时将其关闭。下面类中的 Close() 函数有时不起作用,我在日志中看到(未在下面的代码中显示)应用程序找到句柄(> 0)并在其上发出关闭命令但键盘没有关闭。

这个问题与下面提到和链接的 2 个问题有关!但我无法发表评论,所以我不得不提出一个新问题。

我们之前在 v1709 中有一种工作解决方案,由 Determine if Windows 10 Touch Keyboard is Visible or Hidden 提出

现在我正在尝试实现该问题中的最新帖子,以便能够在超时或应用程序关闭事件中切换键盘的可见性,如果它检测到键盘处于打开状态,我正在尝试将其与您的键盘类结合使用见下文,但我遇到了麻烦。

所以我们从上面的帖子和 Show touch keyboard (TabTip.exe) in Windows 10 Anniversary edition 中组合了触摸键盘类:

static class TouchKeyboard
{


    private static void StartTabTip()
    {
        var p = Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");
        int handle = 0;
        while ((handle = NativeMethods.FindWindow("IPTIP_Main_Window", "")) <= 0)
        {
            Thread.Sleep(100);
        }
    }

    public static void ToggleVisibility()
    {
        var type = Type.GetTypeFromCLSID(Guid.Parse("4ce576fa-83dc-4F88-951c-9d0782b4e376"));
        var instance = (ITipInvocation)Activator.CreateInstance(type);
        instance.Toggle(NativeMethods.GetDesktopWindow());
        Marshal.ReleaseComObject(instance);
    }

    public static void Show()
    {
        int handle = NativeMethods.FindWindow("IPTIP_Main_Window", "");
        if (handle <= 0) // nothing found
        {
            StartTabTip();                
            Thread.Sleep(100);                
        }
        // on some devices starting TabTip don't show keyboard, on some does  ¯\_(ツ)_/¯
        if (!IsOpen())
        {
            ToggleVisibility();
        }
    }

    public static void Hide()
    {
        if (IsOpen())
        {
            ToggleVisibility();
        }
    }        


    public static bool Close()
    {
        // find it
        int handle = NativeMethods.FindWindow("IPTIP_Main_Window", "");
        bool active = handle > 0;
        if (active)
        {
            // don't check style - just close
            NativeMethods.SendMessage(handle, NativeMethods.WM_SYSCOMMAND, NativeMethods.SC_CLOSE, 0);
        }
        return active;
    }

   public static bool GetIsOpen()
   {
      return GetIsOpen1709() ?? GetIsOpenLegacy();
   }

private static bool? GetIsOpen1709()
{
    var parent = IntPtr.Zero;
    for (;;)
    {
        parent = FindWindowEx(IntPtr.Zero, parent, WindowParentClass1709);
        if (parent == IntPtr.Zero)
            return null; // no more windows, keyboard state is unknown

        // if it's a child of a WindowParentClass1709 window - the keyboard is open
        var wnd = FindWindowEx(parent, IntPtr.Zero, WindowClass1709, WindowCaption1709);
        if (wnd != IntPtr.Zero)
            return true;
    }
}

private static bool GetIsOpenLegacy()
{
    var wnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, WindowClass);
    if (wnd == IntPtr.Zero)
        return false;

    var style = GetWindowStyle(wnd);
    return style.HasFlag(WindowStyle.Visible)
        && !style.HasFlag(WindowStyle.Disabled);
}

private const string WindowClass = "IPTip_Main_Window";
private const string WindowParentClass1709 = "ApplicationFrameWindow";
private const string WindowClass1709 = "Windows.UI.Core.CoreWindow";
private const string WindowCaption1709 = "Microsoft Text Input Application";

private enum WindowStyle : uint
{
    Disabled = 0x08000000,
    Visible = 0x10000000,
}

private static WindowStyle GetWindowStyle(IntPtr wnd)
{
    return (WindowStyle)GetWindowLong(wnd, -16);
}

[DllImport("user32.dll", SetLastError = false)]
private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr after, string className, string title = null);

[DllImport("user32.dll", SetLastError = false)]
private static extern uint GetWindowLong(IntPtr wnd, int index);

这在 1709 年工作得很好,但在 1803 年和新客户要求在超时时关闭键盘时就不行了。 所以现在我已经将该 stackoverflow 问题的最新帖子中的代码作为函数添加到上面的类中(我认为这不是正确的方法):

   static class TouchKeyboard
   {
    ///Above code omitted

    public static bool GetIsOpen1803()
    {
        // do this once:
        var brokerClass = new ImmersiveShellBroker();
        var broker = (IImmersiveShellBroker)brokerClass;
        var ihm = broker.GetInputHostManagerBroker();
        Marshal.ReleaseComObject(broker);

        // now ihm reference can be cached and used later:
        Rect rect;
        DisplayMode mode;
        ihm.GetIhmLocation(out rect, out mode);

    }

    [ComImport, Guid("228826af-02e1-4226-a9e0-99a855e455a6")]
    class ImmersiveShellBroker
    {
    }

    [ComImport, Guid("9767060c-9476-42e2-8f7b-2f10fd13765c")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IImmersiveShellBroker
    {
        void Dummy();
        IInputHostManagerBroker GetInputHostManagerBroker();
    }

    [ComImport, Guid("2166ee67-71df-4476-8394-0ced2ed05274")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IInputHostManagerBroker
    {
        void GetIhmLocation(out Rect rect, out DisplayMode mode);
    }

    [StructLayout(LayoutKind.Sequential)]
    struct Rect
    {
        public int Left, Top, Right, Bottom;
    }

    enum DisplayMode
    {
        NotSupported = 0,
        Floating = 2,
        Docked = 3,
    }
    }

我认为我没有正确实现它,因为它并非一直有效,当我从上述超时事件调用 GetIsOpen1803 时,它有时会返回矩形 0,0,0,0,而我看到键盘已启动。 在代码中,它在注释中说“只执行一次”和“可以缓存并在以后使用”,但我似乎无法掌握如何在我们拥有的 Touchkeyboard 类中执行此操作。

到目前为止,如果键盘从应用程序中的任何位置出现在屏幕上并且能够关闭它,那么让 GetIsOpen 方法可靠地返回是我无法做到的。即使终止所有 tabtip 进程也不总是关闭键盘!唯一可行的是终止服务,但我不能这样做,因为需要提升权限。

因此,绝对让我开心的是找到一种可靠的方法来查看键盘是否为所有 Windows 10 版本打开,以及一种可靠的方法来关闭它或切换它的可见性(如果有的话!)

如有任何帮助,我们将不胜感激!

最佳答案

你能试试我的库的更新变体吗 - https://github.com/AlexeiScherbakov/osklib

您可以将 OnScreenKeyboardWatcher/DispatcherOnScreenKeyboardWatcher/WinFormsKeyboardWatcher 用于: 1.检测当前键盘状态 2.观察键盘打开多长时间(超时)

我已经在旧版本(1709、1703 和 1607)上测试了这段代码。在我的 1803(内部版本 17134)上它也有效

关于C# WPF Windows 10 (1803) TouchKeyboard 不可靠问题 (Prism ClickOnce),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50623154/

相关文章:

c# - 已知分布的最佳列表容量

c# - 使用 LINQ-to-SQL 处理 where 子句中的空值

c# - 使用 WPF 和 PRISM 的模块化 MVVM 应用程序

.net - WPF RichTextBox 文档创建线程问题

c# - Windows 10 中虚拟桌面的编程控制

c# - WPF C# 如何为 HighlightBrush 颜色设置动画?

c# - 如何将字节数组转换为uint64并返回C#?

WPF - 如何在按下 CTRL+SHIFT 时捕获?

delphi - Delphi 西雅图的 Windows 10 TNotification 内存泄漏?

deployment - 无法使用Windows 10 Mobile Tech Preview将通用应用程序部署到手机