2012-03-20 125 views
0

我有一個關於Sendkeys Class的問題,因爲我想使用這個類來發送一些按鍵到活動的應用程序。 作爲第一步驟i要測試的{}輸入擊鍵,所以爲了實現這一我提出在vb.net簡單的應用程序2010在VB.NET中發送按鍵的簡單應用程序

Public Class Form1 
Public Shared data As String 
Private Sub SendintText(command As String) 
    Try 
     SendKeys.SendWait(command) 
    Catch e As Exception 
     MsgBox(e.StackTrace) 
    End Try 
End Sub 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    data = TextBox1.Text.ToString 
    SendingText(data) 
End Sub 

末級

當我試圖爲運行{Enter}我收到了infinit循環錯誤: 在System.Windows.Forms.dll中發生了類型爲「System.StackOverflowException」的未處理的異常 有人可以幫助我嗎?

以後編輯:在此期間我已經找到另一個例子

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    System.Threading.Thread.Sleep(2000) 
    SendKeys.SendWait("{ENTER}") 
End Sub 

如果在後臺我有兩個應用:

  1. 的Visual Studio
  2. 目標應用

哪有我專注於目標應用程序,因爲現在當我運行表單時,那個f orm成爲活動..有什麼解決方案從命令提示符調用winform與一些參數(我想發送的密鑰)?

後來EDIT2 強大的文本 我已經放棄了Windows窗體的想法,所以我finnally已在控制檯應用程序的簡單程序,用來模擬鍵盤...導入系統 進口System.Windows.Forms的

模塊模塊1

Sub Main(ByVal args() As String) 
    Dim data As String = args(0) 
    Console.WriteLine("You insert the : {0}", data) 
    System.Threading.Thread.Sleep(5000) 
    SendKeys.SendWait(data) 
End Sub 

前端模塊

通過在鄰的方式rder在需要\「測試\」的參數中使用雙引號......(我花了15分鐘才能找出),因此它可能是有用的... 再次感謝您的信息。

+0

當你點擊按鈕,它就會集中。那麼發送按鍵到按鈕的目的是什麼? – PraveenVenu 2012-03-20 15:38:35

+0

我只想測試發送密鑰(通過在文本框中輸入該密鑰)到應用程序...有沒有可能做到這一點? – Operagust 2012-03-20 16:15:55

+0

當然,你可以做到這一點。你看了?你搜索什麼? – UnhandledExcepSean 2012-03-20 19:06:17

回答

0

爲了sendkey到另一個應用程序,你需要首先激活按鈕單擊該應用程序,然後發送鍵

MSDN Link

// Get a handle to an application window. 
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)] 
public static extern IntPtr FindWindow(string lpClassName, 
    string lpWindowName); 

// Activate an application window. 
[DllImport("USER32.DLL")] 
public static extern bool SetForegroundWindow(IntPtr hWnd); 

// Send a series of key presses to the Calculator application. 
private void button1_Click(object sender, EventArgs e) 
{ 
    // Get a handle to the Calculator application. The window class 
    // and window name were obtained using the Spy++ tool. 
    IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator"); 

    // Verify that Calculator is a running process. 
    if (calculatorHandle == IntPtr.Zero) 
    { 
     MessageBox.Show("Calculator is not running."); 
     return; 
    } 

    // Make Calculator the foreground application and send it 
    // a set of calculations. 
    SetForegroundWindow(calculatorHandle); 
    SendKeys.SendWait("111"); 
    SendKeys.SendWait("*"); 
    SendKeys.SendWait("11"); 
    SendKeys.SendWait("="); 
} 
+0

的目的,謝謝你的例子,我已經閱讀之前,我發佈我的問題。 – Operagust 2012-03-20 16:55:04

+0

另外我注意到,總是當我運行這個窗體窗體應用程序,重點是窗體窗體,即使我有另一個應用程序打開... – Operagust 2012-03-20 18:11:47

+0

我無法在VB.NET中得到這個工作,因爲「SendWait不是SendKeys的成員「。有什麼建議麼? – Lou 2016-12-18 23:40:32

0

按鈕有焦點。按鈕點擊事件發送一個輸入按鍵。發送一個輸入會導致按鈕點擊事件觸發。無限循環。

您可以先禁用按鈕,並在sendkeys例程完成後重新啓用該按鈕以停止無限循環。

你可能想要做的是將焦點切換回想要發送擊鍵的應用程序。

+0

我已經嘗試設置焦點,但我收到了同樣的錯誤...也許我錯過了一些東西...我只想測試參數化sendkey ...這是這個簡單的應用 – Operagust 2012-03-20 16:18:33

相關問題