Determine if Windows 10 Touch Keyboard is Visible or Hidden

前端 未结 3 533
后悔当初
后悔当初 2020-12-06 14:22

I am trying to find out if the windows 10 virtual touch keyboard is visible or not to know whether to open it or not from my application. THe following code has worked fine

3条回答
  •  粉色の甜心
    2020-12-06 14:50

    I'm using this solution, and it is working on Windows 1607, 1709 and 1803 (check the Main method below on the code):

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApp1
    {
        class Program
        {
    
            [ComImport, Guid("D5120AA3-46BA-44C5-822D-CA8092C1FC72")]
            public class FrameworkInputPane
            {
            }
    
            [ComImport, System.Security.SuppressUnmanagedCodeSecurity,
            InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
            Guid("5752238B-24F0-495A-82F1-2FD593056796")]
            public interface IFrameworkInputPane
            {
                [PreserveSig]
                int Advise(
                    [MarshalAs(UnmanagedType.IUnknown)] object pWindow,
                    [MarshalAs(UnmanagedType.IUnknown)] object pHandler,
                    out int pdwCookie
                    );
    
                [PreserveSig]
                int AdviseWithHWND(
                    IntPtr hwnd,
                    [MarshalAs(UnmanagedType.IUnknown)] object pHandler,
                    out int pdwCookie
                    );
    
                [PreserveSig]
                int Unadvise(
                    int pdwCookie
                    );
    
                [PreserveSig]
                int Location(
                    out Rectangle prcInputPaneScreenLocation
                    );
            }
    
    
            static void Main(string[] args)
            {
                var inputPane = (IFrameworkInputPane)new FrameworkInputPane();
                inputPane.Location(out var rect);
                Console.WriteLine((rect.Width == 0 && rect.Height == 0) ? "Keyboard not visible" : "Keyboard visible");
            }
        }
    }
    

    It uses the IFrameworkInputPane interface (https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nn-shobjidl_core-iframeworkinputpane)

提交回复
热议问题