如何发送字符串到当前在前台的应用程序?(就像Windows的屏幕键盘)



这个示例代码发送一个字符串到记事本:

// import the function in your class
[DllImport ("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);
//...
Process p = Process.GetProcessesByName("notepad").FirstOrDefault();
if( p != null)
{
   IntPtr h = p.MainWindowHandle;
   SetForegroundWindow(h);
   SendKeys.SendWait("k");
}

但是我想发送一个字符串给当前在前台的应用程序——就像Windows的屏幕键盘一样。我需要模拟Windows屏幕键盘(OSK)

Process[] all = Process.GetProcesses();
foreach(process p in all)
{
    IntPtr h = p.MainWindowHandle;
    SetForegroundWindow(h);
    SendKeys.SendWait("k");
}

用于发送密钥到所有进程。

希望有所帮助

假设s是您想要发送给具有焦点的应用程序的字符串:

 Clipboard.SetDataObject(s, false, 5, 100);
                    SendKeys.SendWait("^V");

上面的代码模拟了将字符串s复制粘贴到具有focus的应用程序的操作。

相关内容

最新更新