2011-07-07 30 views
3

我想模擬外部程序的鍵盤單擊。我嘗試了SendMessage,PostMessage,SendKeys,但它們不會將密鑰發送到一個特定的程序。所以我想嘗試SendInput和我已經下載了SendInput一個很好的包裝 - http://inputsimulator.codeplex.com/C#InputSimulator包裝 - 如何使用它?

我已經添加了裝配到我的項目,但我還不能開始使用任何功能...

我有什麼做? 什麼「使用」我應該添加?

+0

「什麼」使用「我應該添加」。如果你使用VS,只需在代碼InputSimulator中寫入,然後intellisence會建議你命名空間。不是嗎? – Tigran

回答

4

我相信你需要一個

Using WindowsInput; 

如果不是它,你可以在對象瀏覽器查看,看到的命名空間。只需右鍵單擊解決方案資源管理器中的引用並單擊瀏覽

+0

沒錯,就是這樣。我非常坦克:) – TreantBG

0

雖然我不能告訴你什麼使用您可能需要的指令,但我不確定此工具是否允許您將輸入發送到特定窗口。 頁面的「歷史記錄」部分中,您鏈接狀態:

它最初是在WpfKB(WPF觸摸屏鍵盤)項目中使用,以模擬真實的鍵盤輸入到活躍窗口寫入。

我知道這個問題的唯一解決方案涉及SendMessage,也許你可以進一步解釋問題出在哪裏?

2

您可以模擬鍵盤輸入一個程序是這樣的:

  • 帶給您想從user32.dll中發送鍵使用SetForegroundWindow前景

  • 使用該程序SendKeys.SendWait Method將實際密鑰發送到程序窗口

示例代碼(在測試之前啓動記事本):

using System; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace SendKeyboardInput 
{ 
    public class SendKey 
    { 
     [DllImport("user32.dll")] 
     public static extern bool SetForegroundWindow(IntPtr hWnd); 

     public void Send() 
     { 
      System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad"); //search for process notepad 
      if (p.Length > 0) //check if window was found 
      { 
       SetForegroundWindow(p[0].MainWindowHandle); //bring notepad to foreground 
      } 

      SendKeys.SendWait("a"); //send key "a" to notepad 
     } 
    } 
} 
1

我有同樣的問題,但我設法通過以下3個步驟

  1. 提取InputSimulator內容某處明智喜歡你 做項目URL

  2. 在Visual Studio中點擊Project-> Add Reference並瀏覽到 InputSimulator DLL文件

  3. 通過添加「使用 WindowsInput;」將WindowsInput命名空間添加到項目中