How do I make the virtual keyboard enter text into a textbox in WinForms application?

StackOverflow https://stackoverflow.com/questions/21536840

  •  06-10-2022
  •  | 
  •  

سؤال

I have a windows forms application containing a button and a textbox. At "button_click_event" this code is executed:

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

The keyboard shows, yet no text appears on the textbox as I type on said keyboard.

هل كانت مفيدة؟

المحلول

What is happening is that the on screen keyboard is sending information to the Focused Control which in your case is the Button that initiated the Keyboard. Try setting focus to to your textbox after creating the keyboard.

private void button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("osk.exe");
    //SetFocus to your TextBox here
    textBox1.Focus();
}

To close it do something like this

private void button2_Click(object sender, EventArgs e)
{
    var procs = Process.GetProcessesByName("osk");
    if (procs.Length != 0)
        procs[0].Kill();

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top