【问题标题】:Start TabTip with numpad view open在打开小键盘视图的情况下启动 TabTip
【发布时间】:2014-02-04 02:59:13
【问题描述】:

我基本上有启动键盘的代码,但它以字母数字部分打开,用于编辑的框是带有数字的 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 Desktop App: Open tabtip.exe to secondary keyboard (for numeric textbox)

【问题讨论】:

    标签: c# on-screen-keyboard


    【解决方案1】:

    目前在 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 窗口定位在屏幕周围没有任何成功。

    【讨论】:

    • 根据我的经验,这在 Win8.1 及以下版本中效果很好。但不是在 Win10 中,因为 TabTip.exe 似乎每次打开时都会重置注册表项。
    • 我也遇到过这个问题。我正在从事的这个项目开始在 win 8.1 设备上进行开发,该设备已升级到 win10。但是最近 tabtip 在某些页面上没有将小键盘显示为默认值。附言。我强烈建议在您的程序中添加 tabtip.exe 作为资源。这将避免偶尔的权限错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 2021-01-17
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多