在C#中,您可以使用SendKeys类来模拟键盘按键。以下是一个简单的示例,展示了如何使用SendKeys类发送“Hello, World!”:
using System;
using System.Windows.Forms;
namespace SendKeysExample
{
class Program
{
[STAThread]
static void Main()
{
// 发送 "Hello, World!" 到活动窗口
SendKeys.Send("Hello, World!");
}
}
}
在这个示例中,我们首先导入了System和System.Windows.Forms命名空间。然后,我们创建了一个名为SendKeysExample的命名空间和一个名为Program的类。在Main方法中,我们使用SendKeys.Send方法发送字符串“Hello, World!”到活动窗口。
请注意,SendKeys类主要用于模拟键盘按键,而不是与用户界面进行交互。如果您需要与用户界面进行交互,请考虑使用其他方法,如Control.Invoke或Control.BeginInvoke。