5

我基本上有启动键盘的代码,但它以字母数字部分打开,用于编辑的框是带有数字的 NumericUpDown。所以,我想在 Windows 8.1 中打开 tabtip.exe 又名屏幕键盘,并以数字键盘为焦点。这是我当前打开 tabtip 的代码,但默认情况下它不会使用小键盘打开:

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 中显示 Taptip 键盘的小键盘部分:

Windows 8 桌面应用程序:打开 tabtip.exe 到辅助键盘(用于数字文本框)

4

1 回答 1

5

目前在 Windows 8.1 中,似乎没有多少功能以编程方式公开。下面的代码会导致tabtip.exe读取注册表,因为原来的进程被杀掉了。它并不完全可靠,但它是一种响应某些注册表值的方法。关于停靠的部分是可选的,它每次通过注册表更改强制它停靠。进程.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 窗口定位在屏幕周围没有任何成功。

于 2014-05-02T14:23:11.843 回答