在小键盘视图打开的情况下启动TabTip

时间:2014-01-15 15:02:37

标签: c# on-screen-keyboard

我基本上有启动键盘的代码,但它以字母数字部分打开,编辑框是带数字的NumericUpDown。因此,我想打开tabtip.exe,即在Windows 8.1中使用numerpad聚焦的屏幕键盘。这是我当前打开tabtip的代码,但默认情况下它也不会以numpad打开:

using System.Runtime.InteropServices; //added for keyboard closure
using System.Windows.Interop; //Keyboard closure - must add reference for WindowsBase

//Added for keyboard closure
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);

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

//open keyboard
void openKeyboard()
{
                ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                Process.Start(startInfo);
}

//close keyboard
void closeKeyboard()
{
uint WM_SYSCOMMAND = 274;
                uint SC_CLOSE = 61536;
                IntPtr KeyboardWnd = FindWindow("IPTip_Main_Window", null);
                PostMessage(KeyboardWnd.ToInt32(), WM_SYSCOMMAND, (int)SC_CLOSE, 0);
}

似乎还有一些注册表编辑你可以做,但我似乎无法让它在Windows 8.1中的taptp键盘的小键盘部分显示:

Windows 8 Desktop App: Open tabtip.exe to secondary keyboard (for numeric textbox)

1 个答案:

答案 0 :(得分:5)

目前在Windows 8.1中,似乎没有太多功能以编程方式公开。 下面的代码将导致tabtip.exe读取注册表,因为原始进程被终止。 它不是完全可靠的,但它是一种回应某些注册表值的方法。 有关对接的部分是可选的,它会强制每次通过注册表更改进行停靠。 process.Kill();应该在try / catch中,因为它偶尔没有权限并且可以抛出异常。

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

    [DllImport("user32.dll")]
    public static extern IntPtr PostMessage(int hWnd, uint msg, int wParam, int lParam);

    private static void KillTabTip()
    {
        // Kill the previous process so the registry change will take effect.
        var processlist = Process.GetProcesses();

        foreach (var process in processlist.Where(process => process.ProcessName == "TabTip"))
        {
            process.Kill();
            break;
        }
    }

    public void ShowTouchKeyboard(bool isVisible, bool numericKeyboard)
    {
        if (isVisible)
        {
            const string keyName = "HKEY_CURRENT_USER\\Software\\Microsoft\\TabletTip\\1.7";

            var regValue = (int) Registry.GetValue(keyName, "KeyboardLayoutPreference", 0);
            var regShowNumericKeyboard = regValue == 1;

            // Note: Remove this if do not want to control docked state.
            var dockedRegValue = (int) Registry.GetValue(keyName, "EdgeTargetDockedState", 1);
            var restoreDockedState = dockedRegValue == 0;

            if (numericKeyboard && regShowNumericKeyboard == false)
            {
                // Set the registry so it will show the number pad via the thumb keyboard.
                Registry.SetValue(keyName, "KeyboardLayoutPreference", 1, RegistryValueKind.DWord);

                // Kill the previous process so the registry change will take effect.
                KillTabTip();
            }
            else if (numericKeyboard == false && regShowNumericKeyboard)
            {
                // Set the registry so it will NOT show the number pad via the thumb keyboard.
                Registry.SetValue(keyName, "KeyboardLayoutPreference", 0, RegistryValueKind.DWord);

                // Kill the previous process so the registry change will take effect.
                KillTabTip();
            }

            // Note: Remove this if do not want to control docked state.
            if (restoreDockedState)
            {
                // Set the registry so it will show as docked at the bottom rather than floating.
                Registry.SetValue(keyName, "EdgeTargetDockedState", 1, RegistryValueKind.DWord);

                // Kill the previous process so the registry change will take effect.
                KillTabTip();
            }

            Process.Start("c:\\Program Files\\Common Files\\Microsoft Shared\\ink\\TabTip.exe");
        }
        else
        {
            var win8Version = new Version(6, 2, 9200, 0);

            if (Environment.OSVersion.Version >= win8Version)
            {
                const uint wmSyscommand = 274;
                const uint scClose = 61536;
                var keyboardWnd = FindWindow("IPTip_Main_Window", null);
                PostMessage(keyboardWnd.ToInt32(), wmSyscommand, (int)scClose, 0);
            }
        }
    }

您可以从TextBox的自定义版本调用上述方法,其中重写了OnTouchDown,并创建了一个额外的DependencyProperty来指示该字段是否使用NumericKeyboard:

    #region NumericKeyboard
    public static readonly DependencyProperty NumericKeyboardProperty = DependencyProperty.Register("NumericKeyboard", typeof(bool), typeof(CustomTextBox), new FrameworkPropertyMetadata(false));

    /// <summary> Returns/set the "NumericKeyboard" state of the CustomTextBox. </summary>
    public bool NumericKeyboard
    {
        get { return (bool)GetValue(NumericKeyboardProperty); }
        set { SetValue(NumericKeyboardProperty, value); }
    }
    #endregion


    protected override void OnTouchDown(TouchEventArgs e)
    {
        base.OnTouchDown(e);
        Focus();

        if (IsReadOnly == false)
            ShowTouchKeyboard(true, NumericKeyboard);
    }

目前,在浮动(非停靠)状态下,使用类似技术在屏幕上定位TabTip窗口时,我没有取得任何成功。