【问题标题】:windows form - show windows 8 keyboard when touch textboxwindows 窗体 - 触摸文本框时显示 windows 8 键盘
【发布时间】:2014-08-17 11:00:58
【问题描述】:

我创建了一个 Windows 窗体应用程序。我希望当应用程序安装在装有 Windows 8 和触摸屏的电脑上时,如果我点击文本框,虚拟键盘会自动显示。

我见过类似的东西

private void textbox_Enter(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("TabTip.exe");
}

但是当我使用鼠标时它也可以工作。我希望虚拟键盘在我使用触摸屏时出现。

谢谢

【问题讨论】:

    标签: windows forms textbox keyboard touch


    【解决方案1】:

    我找到了一个“粗鲁”的解决方案。 我看到当我触摸时,会出现“587” WM 消息,然后是 2 个“33” WM 消息(使用鼠标单击时相同)。

    所以我已经看到:在文本框上的“mouseclick”或“focus enter”事件之前:

    a) 如果您使用过鼠标,则有 1 个“33”Wm 消息

    b) 如果您使用过触摸,则有 1 个“587”消息后跟 2 个“33”Wm 消息。

    那么,关于代码。

    在 MyForm.cs 中,我有一个 bool 的公共列表,用于保存“触摸状态”。如果我从不触摸(或者我的电脑不支持触摸屏),状态将永远不会填充

    namespace MyForm
    {
      public partial class MyForm: Form
      {
        public List<bool> v_touch = new List<bool>();
        protected override void WndProc(ref Message msg)
        {
             switch (msg.Msg)
             {
                        //touch
                        case 587:
                            v_touch.Add(true);
                            break;
    
                        //mouse or touch (on mouse click message 33 occurs 1 time; on touch tap message 587 occurs 1 time and message 33 occurs 2 times)
                        case 33:
    
                            //add just if some touch event (587) has occured (never fill v_touch for not touch devices)
                            if (v_touch.Count > 0)
                            {
                                v_touch.Add(false);
                            }
                            break;
              }
    
              base.WndProc(ref msg);
          }
        }
    }
    

    在所有 myUserControls 中,我需要为每个文本框添加鼠标单击、焦点输入和焦点离开事件。 MouseClick 或 focus enter 调用启用虚拟键盘检查“触摸状态”的功能。相反,焦点离开事件会杀死键盘。

    namespace MyForm
    {
        public partial class MyUserControl : UserControl
        {
            public void enableVirtualKeyboard(List<bool> v_touch)
            {
    
                //check if came from "touch". When I use touch, the last v_touch values are (true, false, false)
                if (v_touch.Count >= 3 && v_touch[v_touch.Count - 3] == true)
                {
    
                        string progFiles = @"C:\Program Files\Common Files\Microsoft Shared\ink";
                        string onScreenKeyboardPath = System.IO.Path.Combine(progFiles, "TabTip.exe");
                        Process.Start(onScreenKeyboardPath);
    
                }
    
            }
    
            public void disableVirtualKeyboard()
            {
                Process[] oskProcessArray = Process.GetProcessesByName("TabTip");
                foreach (Process onscreenProcess in oskProcessArray)
                {
                    onscreenProcess.Kill();
                }
    
            }
    
            private void textbox_MouseClick(object sender, MouseEventArgs e)
            {
    
                this.enableVirtualKeyboard(((MyForm)this.ParentForm).v_touch);
            }
    
            private void textbox_Enter(object sender, EventArgs e)
            {
                this.enableVirtualKeyboard(((MyForm)this.ParentForm).v_touch);
            }
    
            private textbox_Leave(object sender, EventArgs e)
            {
                this.disableVirtualKeyboard();
            }
        }
      }
    }
    

    我们可以将disableVirtualKeyboard和enableVirtualKeyboard写在一个通用类中,这样我们就可以在应用程序的每个控件的所有texbbx中使用这2个方法。另外,最好在 MyForm 的 Form_Closing 事件上使用 disableVirtualKeyboard

    【讨论】:

      猜你喜欢
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多