SendKeys.SendWait(String) SendKeys.SendWait(String) SendKeys.SendWait(String) SendKeys.SendWait(String) Method


向活动应用程序发送给定的键,然后等待消息被处理。Sends the given keys to the active application, and then waits for the messages to be processed.

 static void SendWait(System::String ^ keys);
public static void SendWait (string keys);
static member SendWait : string -> unit
Public Shared Sub SendWait (keys As String)


String String String String

要发送的击键字符串。The string of keystrokes to send.


用于SendWait向活动应用程序发送击键或键组合, 并等待处理击键消息。Use SendWait to send keystrokes or combinations of keystrokes to the active application and wait for the keystroke messages to be processed. 您可以使用此方法将键击发送到应用程序, 并等待由击键启动的任何进程完成。You can use this method to send keystrokes to an application and wait for any processes that are started by the keystrokes to be completed. 如果其他应用程序必须先完成, 然后你的应用程序才能继续, 这一点可能很重要。This can be important if the other application must finish before your application can continue.


由于没有用于激活其他应用程序的托管方法, 您可以在当前应用程序中使用此类, 也可以使用本机 Windows 方法 ( FindWindowSetForegroundWindow和) 强制集中于其他应用程序。Because there is no managed method to activate another application, you can either use this class within the current application or use native Windows methods, such as FindWindow and SetForegroundWindow, to force focus on other applications.


SendKeys 类已更新为 .NET Framework 3.0,从而可用于在 Windows Vista 上运行的应用程序。The SendKeys class has been updated for the .NET Framework 3.0 to enable its use in applications that run on Windows Vista. Windows Vista 增强的安全性(称为用户帐户控件或 UAC)可避免以前的实现按预期运行。The enhanced security of Windows Vista (known as User Account Control or UAC) prevents the previous implementation from working as expected.

SendKeys 类容易遭受某些开发人员不得不解决的计时问题。The SendKeys class is susceptible to timing issues, which some developers have had to work around. 更新后的实现仍然容易遇到计时问题,但速度稍微快一些,并且可能需要更改解决方法。The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds. SendKeys 类先尝试使用以前的实现,失败后再使用新的实现。The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation. 因此, SendKeys 类在不同操作系统上的运行方式可能不同。As a result, the SendKeys class may behave differently on different operating systems. 此外,当 SendKeys 类使用新的实现时, SendWait 方法不会等到消息被处理后才将其发送至其他进程。Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process.

如果如论使用何种操作系统,你的应用程序均依赖于一致的行为,则可通过将以下应用程序设置添加至 app.config 文件强制执行 SendKeys 类以使用新的实现。If your application relies on consistent behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application setting to your app.config file.


<add key="SendKeys" value="SendInput"/>


若要强制执行 SendKeys 类以使用以前的实现,请改用 "JournalHook" 值。To force the SendKeys class to use the previous implementation, use the value "JournalHook" instead.


用于调用此方法的非托管代码。for unmanaged code to call this method. 关联的枚举:UnmanagedCodeAssociated enumeration: UnmanagedCode