共用方式為


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 {退格}、{BS}或{BKSP}
中斷 {休息}
CAPS LOCK {大寫}
DEL 或 DELETE {DELETE} 或 {DEL}
向下鍵 {下}
END {結束}
ENTER {進入}或~
ESC {ESC}
HELP {救命}
HOME {家}
INS 或 INSERT {插入}或{INS}
向左鍵 {左}
納姆鎖 {NUMLOCK}
翻頁 {PGDN}
上翻頁 {PGUP}
列印螢幕 {PRTSC}(預留供未來使用)
向右鍵 {對}
捲軸鎖定 {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}
按鍵減法 {減去}
按鍵乘法 {乘}
按鍵分割法 {分開}

若要指定與任意組合的 SHIFT、CTRL 和 ALT 鍵,請在鍵碼前加上一個或多個以下代碼。

鑰匙 Code
SHIFT +
CTRL ^
ALT %

若要指定在按下其他多個按鍵時,應按住 SHIFT、CTRL 和 ALT 的任意組合,請用括號內附上這些鍵的程式碼。 例如,若要指定在按下 E 和 C 時按住 SHIFT,請使用 “+(EC)”。 若要指定在按住 E 鍵時按住 SHIFT,接著按 C 但不按 SHIFT,請使用 “+EC”。

若要指定重複鍵,請使用{key number}的形式。 你必須在 key 和 number 之間加個空格。 例如,{LEFT 42} 表示按左箭頭鍵 42 次;{h 10} 表示按 H 10 次。

備註

由於沒有受管理的方法來啟用其他應用程式,你可以選擇在目前應用程式中使用此類別,或使用原生 Windows 方法,如 FindWindowSetForegroundWindow,強制聚焦於其他應用程式。

備註

SendKeys 類別已針對 .NET Framework 3.0 進行更新,以允許在 Windows Vista 上執行的應用程式中使用。 Windows Vista 強化的安全性(稱為使用者帳號控制,UAC)會阻止先前的實作如預期運作。

這個 SendKeys 職業容易受到時間限制的問題,有些開發者不得不透過這個方法來解決。 更新後的實作仍可能出現時序問題,但速度稍快,可能需要調整變通方法。 類別 SendKeys 嘗試先使用先前的實作,若失敗,則使用新的實作。 因此,該 SendKeys 類別在不同作業系統上的行為可能不同。 此外,當 SendKeys 類別使用新實作時, SendWait 方法不會等待訊息被處理,當訊息被送往另一個程序時。

如果你的應用程式無論作業系統如何都依賴一致的行為,你可以透過在 app.config 檔案中加入以下應用程式設定,強制類別 SendKeys 使用新的實作。

<appSettings>

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

</appSettings>

若要強制 SendKeys 類別使用先前的實作,請改用 值 "JournalHook"

適用於