当前位置: 动力学知识库 > 问答 > 编程问答 >

windows form - show windows 8 keyboard when touch textbox

问题描述:

I've create a windows form application. I'd like that when the application is installed on a pc with Windows 8 and touchscren, if I click on a textbox, the virtual keyboard is automatic showed.

I've seen something like

private void textbox_Enter(object sender, EventArgs e)

{

System.Diagnostics.Process.Start("TabTip.exe");

}

but it works also when I use the mouse. I'd like that the virtual keyboards apppear just when I use touchscreen.

Thanks

网友答案:

I've found a "rude" solution. I've seen that when I touch, the "587" WM Message occurs, followed by 2 "33" WM Message (the same when you click using mouse).

So I've seen that: before than "mouseclick" or "focus enter" event on textbox:

a) if you have used mouse, there is 1 "33" Wm Message

b) if you have used touch, there is 1 "587" message followed by 2 "33" Wm Message.

So, about the code.

In MyForm.cs I have a public list of bool, that save "touch status". If i never touch (or my pc doesn't support touchscreen) the status will never fill

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);
      }
    }
}

In all myUserControls, I need for each textbox, add moouseclick, focus enter and focus leave events. MouseClick or focus enter call a function that enable virtual keybooard checking "touch status". Instead, focus leave event will kill keyboard.

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();
        }
    }
  }
}

We can write disableVirtualKeyboard and enableVirtualKeyboard in a common class, so we can use the 2 methods in all texbbx of each control of the application. Also, it's good use disableVirtualKeyboard on Form_Closing event of MyForm

分享给朋友:
您可能感兴趣的文章:
随机阅读: