SendKeys.Send(String) SendKeys.Send(String) SendKeys.Send(String) SendKeys.Send(String) Method

定义

向活动应用程序发送击键。Sends keystrokes to the active application.

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 String String String

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

异常

没有可向其发送击键的活动应用程序。There is not an active application to send keystrokes to.

keys 并不表示有效击键keys does not represent valid keystrokes

示例

下面的代码示例演示如何使用Send方法。若要运行该示例, 请将以下代码粘贴到一个名为 Form1 的窗体中, 其中包含一个名为 "Button1"The following code example demonstrates how to use the Send method.To run the example, paste the following code in a form called Form1 containing a button called Button1. 请确保单击事件与其在此示例中的事件处理方法相关联。Ensure the click events are associated with their event-handling methods in this example. 按钮控件的TabIndex属性应设置为0。The button control's TabIndex property should be set to 0. 当示例运行时, 双击窗体以触发按钮的 click 事件。When the example is running, double-click the form to trigger the button's click event.

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

注解

每个键由一个或多个字符表示。Each key is represented by one or more characters. 若要指定单个键盘字符, 请使用字符本身。To specify a single keyboard character, use the character itself. 例如, 若要表示字母 A, 请将字符串 "A" 传递给方法。For example, to represent the letter A, pass in the string "A" to the method. 若要表示多个字符, 请将每个附加字符追加到其前面的字符。To represent more than one character, append each additional character to the one preceding it. 若要表示字母 A、B 和 C, 请将参数指定为 "ABC"。To represent the letters A, B, and C, specify the parameter as "ABC".

加号 (+)、插入符号 (^)、百分号 (%)、颚化符 (~) 和括号 () 对SendKeys具有特殊意义。The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. 若要指定其中一个字符, 请将其括在{}大括号 () 中。To specify one of these characters, enclose it within braces ({}). 例如, 若要指定加号, 请使用 "{+}"。For example, to specify the plus sign, use "{+}". 若要指定大括号字符, 请{}使用 "{{}" 和 "}"。To specify brace characters, use "{{}" and "{}}". 方括号 ([]) 对SendKeys不具有特殊含义, 但必须将它们括在大括号中。Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. 在其他应用程序中, 括号确实具有在动态数据交换 (DDE) 出现时可能很重要的特殊含义。In other applications, brackets do have a special meaning that might be significant when dynamic data exchange (DDE) occurs.

注意

如果你的应用程序旨在用于全球各种键盘,使用 Send 可能会产生不可预知的结果,应当避免。If your application is intended for international use with a variety of keyboards, the use of Send could yield unpredictable results and should be avoided.

若要指定按下某个键时未显示的字符 (如 ENTER 或 TAB) 以及表示操作而不是字符的键, 请使用下表中的代码。To specify characters that aren't displayed when you press a key, such as ENTER or TAB, and keys that represent actions rather than characters, use the codes in the following table.

Key 代码Code
BACKSPACE {BACKSPACE}、{BS.1770} 或 {BKSP}{BACKSPACE}, {BS}, or {BKSP}
BREAK {BREAK}{BREAK}
大写锁定CAPS LOCK {CAPSLOCK}{CAPSLOCK}
DEL 或 DELETEDEL or DELETE {DELETE} 或 {DEL}{DELETE} or {DEL}
向下键DOWN ARROW {DOWN}{DOWN}
EndEND 端面{END}
EnterENTER {ENTER} 或 ~{ENTER}or ~
ESCESC {ESC}{ESC}
帮助HELP 帮助{HELP}
HomeHOME 回家{HOME}
INS 或 INSERTINS or INSERT {INSERT} 或 {INS}{INSERT} or {INS}
向左键LEFT ARROW 左中{LEFT}
NUM LOCKNUM LOCK {NUMLOCK}
Page DownPAGE DOWN {PGDN}{PGDN}
Page UpPAGE UP {PGUP}{PGUP}
打印屏幕PRINT SCREEN {PRTSC}(保留以供将来使用){PRTSC} (reserved for future use)
向右键RIGHT ARROW 然后{RIGHT}
滚动锁SCROLL LOCK {SCROLLLOCK}{SCROLLLOCK}
TabTAB "选项卡{TAB}
向上键UP ARROW {UP}{UP}
F1F1 {F1}{F1}
F2F2 {F2}{F2}
F3F3 {F3}{F3}
F4F4 {F4}{F4}
F5F5 {F5}{F5}
F6F6 {F6}{F6}
F7F7 {F7}{F7}
F8F8 {F8}{F8}
F9F9 {F9}{F9}
F10F10 {F10}{F10}
F11F11 {F11}{F11}
F12F12 {F12}{F12}
F13F13 {F13}{F13}
F14F14 {F14}{F14}
F15F15 {F15}{F15}
F16F16 {F16}{F16}
键盘添加Keypad add {ADD}{ADD}
键盘减Keypad subtract 减去{SUBTRACT}
键盘乘法Keypad multiply {MULTIPLY}
小键盘分隔Keypad divide {DIVIDE}{DIVIDE}

若要指定与 SHIFT、CTRL 和 ALT 键的任意组合组合的键, 请在键代码前面加上一个或多个以下代码。To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes.

Key 代码Code
SHIFT +
CTRLCTRL ^
替代ALT %

若要指定在按多个其他键的同时按下 SHIFT、CTRL 和 ALT 的任意组合, 请将这些键的代码括在括号中。To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. 例如, 若要指定在按 E 和 C 的同时按住 SHIFT, 请使用 "+ (EC)"。For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". 若要指定在按 E 的同时按住 SHIFT, 请在不带 SHIFT 的情况下使用 C, 并使用 "+ EC"。To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

若要指定重复键, 请使用格式 {key number}。To specify repeating keys, use the form {key number}. 必须在键和数字之间添加一个空格。You must put a space between key and number. 例如, {LEFT 42} 表示按左箭头键42次;{h 10} 表示按 H 10 次。For example, {LEFT 42} means press the LEFT ARROW key 42 times; {h 10} means press H 10 times.

备注

由于没有用于激活其他应用程序的托管方法, 您可以在当前应用程序中使用此类, 也可以使用本机 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.

<appSettings>

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

</appSettings>

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

安全性

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

适用于