I'm creating a winform application which require the windows on screen keyboard to fill up a text box. I have 4 text boxes. I created 4 textbox click event to pop up the keyboard when user click the textbox.
public Form1()
{
InitializeComponent();
textBox1.Click += textBox1_Click;
textBox2.Click += textBox2_Click;
textBox3.Click += textBox3_Click;
textBox4.Click += textBox4_Click;
}
private void textBox1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("osk");
}
private void textBox2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("osk");
}
private void textBox3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("osk");
}
private void textBox4_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("osk");
}
The problem I face is, if I first click on textbox1 after running the app, the keyboard will only able to type into textbox1. After that if I click other textBox, it won't be able to type anything in.
Same as if I first click on textbox2 after running the app, the keyboard will only able to type into textbox2. After that if I click other textBox, it won't be able to type anything in.
Do you guys know what's wrong?
I would do something like this:
Add the following class to your project:
class TouchKeyboardProvider
{
#region Private: Fields
private readonly string _virtualKeyboardPath;
#endregion
#region ITouchKeyboardProvider Methods
public TouchKeyboardProvider()
{
_virtualKeyboardPath = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles),
@"Microsoft Shared\ink\TabTip.exe");
if (!System.IO.File.Exists(_virtualKeyboardPath))
{
_virtualKeyboardPath = _virtualKeyboardPath.Replace(" (x86)", string.Empty);
}
}
public void ShowTouchKeyboard()
{
try
{
Process.Start(_virtualKeyboardPath);
}
catch (Exception ex)
{
// TODO: Log error.
Debug.WriteLine(ex.ToString());
}
}
public void HideTouchKeyboard()
{
var nullIntPtr = new IntPtr(0);
const uint wmSyscommand = 0x0112;
var scClose = new IntPtr(0xF060);
var keyboardWnd = FindWindow("IPTip_Main_Window", null);
if (keyboardWnd != nullIntPtr)
{
SendMessage(keyboardWnd, wmSyscommand, scClose, nullIntPtr);
}
}
#endregion
#region Private: Win32 API Methods
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr FindWindow(string sClassName, string sAppName);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
#endregion
}
Then update your form to look something like this:
private readonly TouchKeyboardProvider touckKeyboardProvider = new TouchKeyboardProvider();
public Form1()
{
InitializeComponent();
textBox1.Click += textBox1_Click;
textBox2.Click += textBox2_Click;
textBox3.Click += textBox3_Click;
textBox4.Click += textBox4_Click;
}
private void textBox1_Click(object sender, EventArgs e)
{
touckKeyboardProvider.ShowTouchKeyboard();
}
....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.