SendKeys SendKeys SendKeys SendKeys Class

定义

提供将键击发送到应用程序的方法。Provides methods for sending keystrokes to an application.

public ref class SendKeys
public class SendKeys
type SendKeys = class
Public Class SendKeys
继承
SendKeysSendKeysSendKeysSendKeys

示例

下面的代码示例演示如何使用 Send 方法。The following code example demonstrates how to use the Send method. 若要运行该示例,以下代码粘贴到名为 Form1 的窗体中包含一个按钮名为 Button1。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. Button 控件的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

注解

使用SendKeys发送键击和击键组合到活动的应用程序。Use SendKeys to send keystrokes and keystroke combinations to the active application. 此类无法实例化。This class cannot be instantiated. 若要将击键发送到一个类并立即使用的程序流程继续操作,使用SendTo send a keystroke to a class and immediately continue with the flow of your program, use Send. 若要等待的击键启动的任何进程,请使用SendWaitTo wait for any processes started by the keystroke, use SendWait.

每个键均由一个或多个字符表示。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".

加号 (+)、 脱字号 (^)、 百分号 (%)、 颚化符 (~) 和括号 () 对有特殊意义SendKeysThe 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 或选项卡上,以及表示操作而不是字符键时使用下表中的代码。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 {退格符},{BS} 或 {BKSP}{BACKSPACE}, {BS}, or {BKSP}
中断BREAK {BREAK}{BREAK}
大写锁定CAPS LOCK {CAPSLOCK}{CAPSLOCK}
DEL 或 DELETEDEL or DELETE {删除} 或 {DEL}{DELETE} or {DEL}
向下键DOWN ARROW {DOWN}{DOWN}
EndEND {最终}{END}
EnterENTER 输入} 或 ~{ENTER} or ~
EscESC {ESC}{ESC}
帮助HELP {帮助}{HELP}
HomeHOME {HOME}{HOME}
项或 INSERTINS or INSERT {插入} 或 {项}{INSERT} or {INS}
向左键LEFT ARROW {LEFT}{LEFT}
NUM LOCKNUM LOCK {NUMLOCK}{NUMLOCK}
PAGE DOWNPAGE DOWN {PGDN}{PGDN}
PAGE UPPAGE UP {PGUP}{PGUP}
打印屏幕PRINT SCREEN {PRTSC}(保留供将来使用){PRTSC} (reserved for future use)
向右键RIGHT ARROW {RIGHT}{RIGHT}
滚动锁SCROLL LOCK {SCROLLLOCK}{SCROLLLOCK}
TabTAB {TAB}{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
SHIFTSHIFT +
CTRLCTRL ^
ALTALT %

若要指定 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)". 若要指定以按住 SHIFT 并按 E,跟 C 不按 shift 键,使用"+ EC"。To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

若要指定重复键,使用窗体 {密钥号}。To specify repeating keys, use the form {key number}. 必须将键和数字之间留一个空格。You must put a space between key and number. 例如,{左侧 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.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Flush() Flush() Flush() Flush()

处理消息队列中当前的所有 Windows 消息。Processes all the Windows messages currently in the message queue.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Send(String) Send(String) Send(String) Send(String)

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

SendWait(String) SendWait(String) SendWait(String) SendWait(String)

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

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于