1

I am developing dekstop app, that will be used on windows tablet(C#) and at some point want to find out if TabTip.exe on-screen keyboard is visible or not. The problem is, behavior is different when I test it on a computer that runs Win 8.1 and a tablet with Win 8.1. I will copy small code parts from my test application.

What I do is I user user32.dll to find this out

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

    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool IsWindowVisible(IntPtr hWnd);

I have a button in my example app, where, when I press button it prints out if TabTip is visible or nor

    public void IsVisible(object sender, RoutedEventArgs e)
    {
        IntPtr KeyboardWnd = FindWindow("IPTip_Main_Window", null);
        Console.WriteLine("Keyboard is visible :" + IsWindowVisible(KeyboardWnd));
    }

The thing is... When I start this on a computer that runs Windows 8.1 I get correct value from IsWindowVisible.

On a tablet... I ALWAYS get true. Anyone has any clue why?

UPDATE

I tried using suggested GetWindowRect. The thing is the same as with isWindowVisible. On a tblet, no matter the keyboard is seen or not GetWindowRect return TRUE. Values Top, Bottom, Left, Right aleays has value. On regular computer GetWindowRect return FALSE, when TabTip is not visible. Can anyone explain how I can detect if TabTip is visible(Is seen in current window) on a TABLET?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Gerda
  • 31
  • 2
  • 6
  • 1
    Actually `IsWindowVisible` just checks flag WS_VISIBLE. Try to use `GetWindowRect`, `GetWindowLong`. May be there is a clue, something like: window actually is not hiding, but just moves somewere you don't see it... – user3449857 Apr 02 '14 at 11:43
  • Error checking is inadequate. What IsWindowVisible(null) will return is a guess since a NULL window handle can be a substitute for the desktop window. – Hans Passant Apr 02 '14 at 13:51
  • GetWindowRect seems just as usable as IsWindowVisible. I can detect it on computer that runs on 8.1 but not on tablet, for example GetWindowRect returns TRUE, Top, bottom always has value(not 0) on a tablet... It seems that a tablet does not recognize close(hide?) event for TabTip. Anyone has any idea how I can detect on a tablet, the TabTip is visible or not? – Gerda Apr 03 '14 at 11:39
  • Possible duplicate: http://stackoverflow.com/questions/20892311/detect-if-on-screen-keyboard-is-open-tabtip-exe – Daniel A. White Apr 03 '14 at 11:48

1 Answers1

2

I posted an answer to this in another thread but here it is again to save you a click:

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

namespace CSharpTesting
{
    class Program
    {
        /// <summary>
        /// The window is disabled. See http://msdn.microsoft.com/en-gb/library/windows/desktop/ms632600(v=vs.85).aspx.
        /// </summary>
        public const UInt32 WS_DISABLED = 0x8000000;

        /// <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_DISABLED) != WS_DISABLED);
            }

            return visible;
        }
    }
}
Community
  • 1
  • 1
Barrie
  • 1,444
  • 19
  • 26