通过


SendKeys.Send(String) 方法

定义

将击键发送至活动应用程序。

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

参数

keys
String

要发送的击键字符串。

例外

没有要向其发送击键的活动应用程序。

keys 不表示有效的击键。

示例

下面的代码示例演示如何使用 Send 该方法。若要运行该示例,请将以下代码粘贴到名为 Form1 的窗体中,其中包含名为 Button1 的按钮。 确保单击事件与此示例中的事件处理方法相关联。 按钮控件 TabIndex 的属性应设置为 0。 当示例正在运行时,双击窗体以触发按钮的单击事件。

private:
   // Clicking Button1 causes a message box to appear.
   void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      MessageBox::Show( "Click here!" );
   }

   // Use the SendKeys.Send method to raise the Button1 click event 
   // and display the message box.
   void Form1_DoubleClick( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      
      // Send the enter key; since the tab stop of Button1 is 0, this
      // will trigger the click event.
      SendKeys::Send( "{ENTER}" );
   }

// Clicking Button1 causes a message box to appear.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
    MessageBox.Show("Click here!");
}

// Use the SendKeys.Send method to raise the Button1 click event 
// and display the message box.
private void Form1_DoubleClick(object sender, System.EventArgs e)
{

    // Send the enter key; since the tab stop of Button1 is 0, this
    // will trigger the click event.
    SendKeys.Send("{ENTER}");
}

' Clicking Button1 causes a message box to appear.
Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    MessageBox.Show("Click here!")
End Sub


' Use the SendKeys.Send method to raise the Button1 click event 
' and display the message box.
Private Sub Form1_DoubleClick(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.DoubleClick

    ' Send the enter key; since the tab stop of Button1 is 0, this
    ' will trigger the click event.
    SendKeys.Send("{ENTER}")
End Sub

注解

每个键由一个或多个字符表示。 若要指定单个键盘字符,请使用字符本身。 例如,若要表示字母 A,请将字符串“A”传递给该方法。 若要表示多个字符,请将每个附加字符追加到前面的字符。 若要表示字母 A、B 和 C,请将参数指定为“ABC”。

加号(+)、插入符号(^)、百分号(%)、平铺(~)和括号()具有特殊含义 SendKeys。 若要指定其中一个字符,请将它括在大括号 ({}) 中。 例如,若要指定加号,请使用“{+}”。 若要指定大括号字符,请使用“{{}”和“{}}”。 括号([ ])没有特殊含义 SendKeys,但你必须将它们括在大括号中。 在其他应用程序中,括号确实具有特殊含义,在发生动态数据交换(DDE)时可能很重要。

注意

如果应用程序旨在通过各种键盘进行国际使用,则使用 Send 可能会产生不可预知的结果,应避免使用。

若要指定在按下某个键(如 Enter 或 TAB)时未显示的字符,以及表示操作而不是字符的键,请使用下表中的代码。

密钥 Code
Backspace {BACKSPACE}、{BS}或 {BKSP}
BREAK {BREAK}
Caps Lock {CAPSLOCK}
Del 或 Delete {DELETE} 或 {DEL}
向下键 {DOWN}
结束 {END}
Enter {ENTER}或 ~
Esc {ESC}
帮助 {HELP}
首页 {HOME}
INS 或 INSERT {INSERT} 或 {INS}
向左键 {LEFT}
Num Lock {NUMLOCK}
向下翻页 {PGDN}
PAGE UP(向上翻页) {PGUP}
打印屏幕 {PRTSC}(保留供将来使用)
向右键 {RIGHT}
Scroll Lock {SCROLLLOCK}
Tab {TAB}
向上键 {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}
键盘添加 {ADD}
键盘减去 {SUBTRACT}
键盘乘法 {MULTIPLY}
键盘划分 {DIVIDE}

若要指定键与 Shift、CTRL 和 ALT 键的任意组合组合,请在键代码前面加上以下一个或多个代码。

密钥 Code
Shift +
Ctrl ^
Alt %

若要指定应按住 Shift、Ctrl 和 Alt 的任意组合,同时按下其他几个键,请将这些键的代码括在括号中。 例如,若要指定在按下 E 和 C 时按住 SHIFT,请使用“+(EC)”。 若要指定在按下 E 时按住 SHIFT,后跟 C 而不使用 SHIFT,请使用“+EC”。

若要指定重复键,请使用 {key number} 格式。 必须在键和数字之间放置空格。 例如,{LEFT 42} 表示按向左键 42 次;{h 10} 表示按 H 10 次。

注释

由于没有用于激活其他应用程序的托管方法,因此可以在当前应用程序中使用此类或使用本机 Windows 方法(例如 FindWindow ,以及 SetForegroundWindow)强制关注其他应用程序。

注释

SendKeys 类已针对 .NET Framework 3.0 进行了更新,以便在 Windows Vista 上运行的应用程序中使用。 Windows Vista(称为用户帐户控制或 UAC)的增强安全性可防止以前的实现按预期工作。

SendKeys 类容易受到计时问题的影响,一些开发人员必须解决这些问题。 更新后的实现仍然容易受到计时问题的影响,但速度稍快,可能需要更改解决方法。 该 SendKeys 类首先尝试使用以前的实现,如果失败,则使用新实现。 因此,该 SendKeys 类在不同操作系统上的行为可能不同。 此外,当类使用新实现时 SendKeysSendWait 该方法不会等待消息在发送到另一个进程时进行处理。

如果应用程序依赖于一致的行为,无论操作系统如何,都可以通过将以下应用程序设置添加到 app.config 文件来强制 SendKeys 该类使用新实现。

<appSettings>

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

</appSettings>

若要强制 SendKeys 类使用以前的实现,请改用该值 "JournalHook"

适用于