c# - 检测屏幕键盘是否打开(TabTip.exe)

标签 c# windows-8 keyboard touch on-screen-keyboard

我正在开发用于完成表单的 WPF/C# 应用程序。我正在尝试找到一种方法来确定 TapTip 键盘(TabTip.exe/类似 metro 的 windows 8 桌面键盘)在 windows 8 中是否最小化/不可见。

我已经能够检测到 osk 键盘(osk.exe/windows 辅助功能屏幕键盘)是否被最小化,但同样的过程似乎不适用于 TabTip 键盘。

检测键盘是否最小化I:
1.找到键盘进程
2.获取主窗口句柄
3. 使用 WINDOWPLACEMENT 的 showCmd 属性(使用 MainWindowHandle 找到)
4.使用showCmd值判断窗口是否最小化

我遇到的问题是:
- TabTip 进程的 MainWindowHandle 为 0(所以我不能用它来查找 WINDOWPLACEMENT 信息)
- WINDOWPLACEMENT.showCmd 的值在 TabTip 打开和最小化时相同

为了找到 TabTip 窗口的句柄,我使用 ENUMWINDOWS 获取所有窗口句柄,使用 GETWINDOWTHREADPROCESSID 获取进程 ID,然后将这些 ID 与 TabTip 进程 ID 进行比较。

如有任何帮助,我们将不胜感激。这也是我的第一篇文章。我认为我这样做是正确的,但如果不正确,请告诉我如何解决。

最佳答案

在找到一种有效的方法之前,我尝试了几种不同的方法。使用 IsWindowVisible() 不起作用,而且我对 GetWindowPlacement()GetIconic() 也没有任何兴趣。最后,我使用了 GetWindowLong() 并检查了返回的 WS_VISIBLE。用于演示的快速控制台应用程序如下:

using System;
using System.Diagnostics;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Threading;

namespace CSharpTesting
{
    class Program
    {
        /// <summary>
        /// The window is initially visible. See http://msdn.microsoft.com/en-gb/library/windows/desktop/ms632600(v=vs.85).aspx.
        /// </summary>
        public const UInt32 WS_VISIBLE  = 0X94000000;
        /// <summary>
        /// Specifies we wish to retrieve window styles.
        /// </summary>
        public const int GWL_STYLE = -16;

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(String sClassName, String sAppName);

        [DllImport("user32.dll", SetLastError = true)]
        static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);

        static void Main(string[] args)
        {
            // Crappy loop to poll window state.
            while (true)
            {
                if (IsKeyboardVisible())
                {
                    Console.WriteLine("keyboard is visible");
                }
                else
                {
                    Console.WriteLine("keyboard is NOT visible");
                }

                Thread.Sleep(1000);
            }
        }

        /// <summary>
        /// Gets the window handler for the virtual keyboard.
        /// </summary>
        /// <returns>The handle.</returns>
        public static IntPtr GetKeyboardWindowHandle()
        {
            return FindWindow("IPTip_Main_Window", null);
        }

        /// <summary>
        /// Checks to see if the virtual keyboard is visible.
        /// </summary>
        /// <returns>True if visible.</returns>
        public static bool IsKeyboardVisible()
        {
            IntPtr keyboardHandle = GetKeyboardWindowHandle();

            bool visible = false;

            if (keyboardHandle != IntPtr.Zero)
            {
                UInt32 style = GetWindowLong(keyboardHandle, GWL_STYLE);
                visible = (style == WS_VISIBLE);
            }

            return visible;
        }
    }
}

关于c# - 检测屏幕键盘是否打开(TabTip.exe),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20892311/

相关文章:

c# - 代码中的网址不会破坏构建

c# - Windows 7 和 Windows XP 中的文本框之间是否存在根本差异

android - 键盘显示困惑的元素位置

JavaScript 和 ctrl 键冲突

c# - 使用 lucene.net 搜索 "mvc2"时没有命中

c# - 如何取消 Azure 主题中的预定消息

c# - Microsoft.Reporting 在命名空间中不存在

windows-8 - Windows 8 准备站点以进行固定

c++ - Windows 8 开发可以使用 native c++ 吗?

ruby-on-rails - 用户#index 中的 ExecJS::RuntimeError (RoR)