2011-12-01 230 views
7

我正在開發一個應用程序,它必須將一些鍵或鼠標事件發送到活動窗口。發送鼠標和鍵盤事件

我使用這個類: 鼠標

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

namespace Mouse 
{ 
    public static class VirtualMouse 
    { 
     // import the necessary API function so .NET can 
     // marshall parameters appropriately 
     [DllImport("user32.dll")] 
     static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo); 

     // constants for the mouse_input() API function 
     private const int MOUSEEVENTF_MOVE = 0x0001; 
     private const int MOUSEEVENTF_LEFTDOWN = 0x0002; 
     private const int MOUSEEVENTF_LEFTUP = 0x0004; 
     private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; 
     private const int MOUSEEVENTF_RIGHTUP = 0x0010; 
     private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; 
     private const int MOUSEEVENTF_MIDDLEUP = 0x0040; 
     private const int MOUSEEVENTF_ABSOLUTE = 0x8000;  

     // simulates movement of the mouse. parameters specify changes 
     // in relative position. positive values indicate movement 
     // right or down 
     public static void Move(int xDelta, int yDelta) 
     { 
      mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0); 
     } 

     // simulates movement of the mouse. parameters specify an 
     // absolute location, with the top left corner being the 
     // origin 
     public static void MoveTo(int x, int y) 
     { 
      mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0); 
     } 

     // simulates a click-and-release action of the left mouse 
     // button at its current position 
     public static void LeftClick() 
     { 
      mouse_event(MOUSEEVENTF_LEFTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0); 
      mouse_event(MOUSEEVENTF_LEFTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0); 
     }  
     public static void RightClick() 
     { 
      mouse_event(MOUSEEVENTF_RIGHTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0); 
      mouse_event(MOUSEEVENTF_RIGHTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0); 
     } 
    } 
} 

鍵盤

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

namespace Mouse 
{ 
    public static class VirtualKeyboard 
    { 
     [DllImport("user32.dll")] static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);  
     public static void KeyDown(System.Windows.Forms.Keys key) 
     { 
      keybd_event((byte)key, 0, 0, 0); 
     } 

     public static void KeyUp(System.Windows.Forms.Keys key) 
     { 
      keybd_event((byte)key, 0, 0x7F, 0); 
     } 
    } 
} 

這是我的測試代碼:

private void button1_Click(object sender, EventArgs e) 
{ 
    Thread.Sleep(2000);    
    VirtualMouse.Move(100, 100); 
    VirtualMouse.RightClick(); 
    VirtualKeyboard.KeyDown(System.Windows.Forms.Keys.A); 
    VirtualKeyboard.KeyUp(System.Windows.Forms.Keys.A); 
} 

鼠標的動作,但d不發送點擊。任何想法? 如何讓一個鍵繼續按下一段時間?我嘗試在KeyDown和KeyUp之間使用thread.sleep,但它不工作。

回答

6

有CodePlex上(微軟的開源網站)

的Windows輸入模擬器的一個開源項目(C#SendInput包裝 - 模擬鍵盤和鼠標)

http://inputsimulator.codeplex.com/

它有實例和真正簡單使用。

+3

鼠標仍然沒有工作... – Manu

0

你的定義應該是Uint32

private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x02; 
private const UInt32 MOUSEEVENTF_ABSOLUTE = 0x8000; 
private const UInt32 MOUSEEVENTF_LEFTUP = 0x04; 
private const UInt32 MOUSEEVENTF_RIGHTDOWN = 0x08; 
private const UInt32 MOUSEEVENTF_RIGHTUP = 0x10;